Resize image in Python without losing EXIF data

后端 未结 11 2004
野的像风
野的像风 2020-12-02 19:06

I need to resize jpg images with Python without losing the original image\'s EXIF data (metadata about date taken, camera model etc.). All google searches about python and i

11条回答
  •  情深已故
    2020-12-02 19:19

    You can use pyexiv2 to copy EXIF data from source image. In the following example image is resized using PIL library, EXIF data copied with pyexiv2 and image size EXIF fields are set with new size.

    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))
    

提交回复
热议问题