Created
March 18, 2013 11:41
-
-
Save oneyoung/5186622 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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