Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve exif data of image with PIL when resize(create thumbnail)

When I try to resize (thumbnail) an image using PIL, the exif data is lost.

What do I have to do preserve exif data in the thumbnail image? When I searched for the same, got some links but none seem to be working.

from PIL import  Image
import StringIO

file_path = '/home/me/img/a.JPG'
im = Image.open( file_path)
THUMB_SIZES = [(512, 512)]
for thumbnail_size in THUMB_SIZES:
    im.thumbnail( thumbnail_size, Image.ANTIALIAS)
    thumbnail_buf_string = StringIO.StringIO()
    im.save('512_' + "a", "JPEG")

The orginal image has exif data, but image im(512_a.JPEG) doesn't.

like image 912
Jisson Avatar asked Jun 11 '13 11:06

Jisson


People also ask

How do you preserve EXIF data?

EXIF data is only preserved when syncing the photo back to your computer directly. Emailing the photo will not save EXIF data. Additionally, please ensure that the Location Services is turned on for Snapseed in the iOS Settings.

What does PIL resize do?

Resizing Images using Pillow (PIL) Pillow is one of the most popular options for performing basic image manipulation tasks such as cropping, resizing, or adding watermarks. Install the latest version of Pillow with pip . Pillow provides the resize() method, which takes a (width, height) tuple as an argument.

Does resized image have Exif data in PIL?

The resized image has no exif data. Note: I haven't done this myself yet, but to my knowledge, PIL only allows to read exif tags but cannot write them. You will probably need gexiv2 or pyexiv2 to write the tags to your thumbnails.

What is PiL thumbnail in Python?

Python PIL | Image.thumbnail () Method Last Updated : 19 Jul, 2019 PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image.

Can PIL write Exif tags to thumbnails?

Note: I haven't done this myself yet, but to my knowledge, PIL only allows to read exif tags but cannot write them. You will probably need gexiv2 or pyexiv2 to write the tags to your thumbnails. UPDATE: I got curious and tried it myself :D If i got you right, you just want to copy the metadata without further modifications.

What is Exif data in digital photos?

Digital photos have embedded within their files various pieces of information about the image: the camera make and model, shutter speed and aperture, the date and time etc. This is called Exif data (an acronym for “Exchangeable image file format”) and can be read and displayed by suitable software.


3 Answers

I read throught some of the source code and found a way to make sure that the exif data is saved with the thumbnail.

When you open a jpg file in PIL, the Image object has an info attribute which is a dictionary. One of the keys is called exif and it has a value which is a byte string - the raw exif data from the image. You can pass this byte string to the save method and it should write the exif data to the new jpg file:

from PIL import Image

size = (512, 512)

im = Image.open('P4072956.jpg')
im.thumbnail(size, Image.ANTIALIAS)
exif = im.info['exif']
im.save('P4072956_thumb.jpg', exif=exif)

To get a human-readable version of the exif data you can do the following:

from PIL import Image
from PIL.ExifTags import TAGS

im = Image.open('P4072956.jpg')
for k, v in im._getexif().items():
    print TAGS.get(k, k), v
like image 64
Gary Kerr Avatar answered Oct 14 '22 01:10

Gary Kerr


In my project, i met the same issue with you. After searching Google, I found piexif library. It help to Pilow save exif data to thumbnails.

You can use the source code below:

from PIL import  Image
import piexif
import StringIO


file_path = '/home/me/img/a.JPG'
im = Image.open( file_path)

# load exif data
exif_dict = piexif.load(im.info["exif"])
exif_bytes = piexif.dump(exif_dict)

THUMB_SIZES = [(512, 512)]
for thumbnail_size in THUMB_SIZES:
    im.thumbnail( thumbnail_size, Image.ANTIALIAS)
    thumbnail_buf_string = StringIO.StringIO()
    # save thumbnail with exif data
    im.save('512_' + "a", "JPEG", exif=exif_bytes)

Note: I am using python 3.4 and ubuntu 14.04

like image 28
Nguyen Sy Thanh Son Avatar answered Oct 14 '22 00:10

Nguyen Sy Thanh Son


import pyexiv2
from PIL import  Image

file_path = '/home/../img/a.JPG'
metadata = pyexiv2.ImageMetadata(file_path)
metadata.read()
thumb = metadata.exif_thumbnail
thumb.set_from_file(file_path)
thumb.write_to_file('512_' + "a")
thumb.erase()
metadata.write()

Now I open the image using (Patch Image Inspector) , I can see the exif data

like image 2
Jisson Avatar answered Oct 14 '22 00:10

Jisson