Skip to content

Instantly share code, notes, and snippets.

@oneyoung
Created March 18, 2013 11:41
Show Gist options
  • Save oneyoung/5186622 to your computer and use it in GitHub Desktop.
Save oneyoung/5186622 to your computer and use it in GitHub Desktop.
from PIL import Image
import pyexiv2
def resize_image(src_path, dst_path, size):
# resize image
image = Image.open(src_path)
width, length = size
if image.size[0] < image.size[1]:
width, length = length, width
image.thumbnail((width, length), Image.ANTIALIAS)
image.save(dst_path, "JPEG")
# copy EXIF data
src_meta = pyexiv2.ImageMetadata(src_path)
src_meta.read()
dst_meta = pyexiv2.ImageMetadata(dst_path)
dst_meta.read()
src_meta.copy(dst_meta)
# set EXIF image size info to resized size
dst_meta["Exif.Photo.PixelXDimension"].value = image.size[0]
dst_meta["Exif.Photo.PixelYDimension"].value = image.size[1]
dst_meta.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment