3

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

Community
  • 1
  • 1
Jake
  • 12,713
  • 18
  • 66
  • 96
  • After struggling with building pyexiv2 on my dev and web server systems I've decided to jump in and do it myself. I've almost finished a script that takes the EXIF chunk from one file and inserts it in another. Just working on changing width, height and orientation before writing it to the target file. I'll probably post a link to it once it's done. – Jake Nov 08 '10 at 22:03

1 Answers1

3

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))
Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55