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

后端 未结 3 1659
迷失自我
迷失自我 2020-11-30 11:15

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 s

3条回答
  •  -上瘾入骨i
    2020-11-30 11:55

    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
    

提交回复
热议问题