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

后端 未结 3 1661
迷失自我
迷失自我 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条回答
  •  抹茶落季
    2020-11-30 11:40

    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

提交回复
热议问题