开发者

How can I restore EXIF data after resizing an image with PIL?

This question has been asked before, but it was answered a few years ago and the answer is refers to a broken link and is probably no longer the best method.

pyxiv2 looks like it would do th开发者_运维百科e task, but it has a lot of dependencies for a seemingly simple task.

I'd also like to know what values will no longer be valid for the resized image. Width and Height being the obvious ones.


Just in case someone would like to use piexiv2, here is a solution: image is resized using PIL library, EXIF data copied with pyexiv2 and image size EXIF fields are set with new size.

import pyexiv2
import tempfile
from PIL import Image


def resize_image(source_path, dest_path, size):
    # resize image
    image = Image.open(source_path)
    image.thumbnail(size, Image.ANTIALIAS)
    image.save(dest_path, "JPEG")

    # copy EXIF data
    source_image = pyexiv2.Image(source_path)
    source_image.readMetadata()
    dest_image = pyexiv2.Image(dest_path)
    dest_image.readMetadata()
    source_image.copyMetadataTo(dest_image)

    # set EXIF image size info to resized size
    dest_image["Exif.Photo.PixelXDimension"] = image.size[0]
    dest_image["Exif.Photo.PixelYDimension"] = image.size[1]
    dest_image.writeMetadata()

# resizing local file
resize_image("41965749.jpg", "resized.jpg", (600,400))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜