Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...