Dusan
Dusan

Reputation: 3488

Resize image with Python and keep EXIF and XMP metadata

Its been asked a lot of times how to resize an image and keep the existing exif data. I'm able to do that easily with PIL:

from PIL import Image

im = Image.open("image.jpeg")
exif = im.info['exif']
# process the image, for example resize:
im_resized = im.resize((1920, 1080), resample=PIL.Image.LANCZOS)
im_resized.save("resized.jpeg", quality=70, exif=exif)

I was wondering is there a way to keep the XMP metadata from the original image? There's a lot of GPS data in the XMP which I would love to keep in the resized version.

Upvotes: 4

Views: 1123

Answers (2)

Gringo Suave
Gringo Suave

Reputation: 31940

In Pillow 11.0 one can now copy XMP the same as Exif:

new_img.save(
    filename, 
    exif=old_img.info.get('exif'), 
    xmp=old_img.info.get('xmp'),
)

In this release, XMP data appears to be limited to a single 64k segment.

Upvotes: 0

AmigoJack
AmigoJack

Reputation: 6174

Not even the successor Pillow can read XMP (and IPTC). Also you're not really keeping anything - you create a whole new file and add a copy of the other EXIF data (including now potentially invalid information like width/height).

JFIF files aren't black magic - their file format can be parsed rather easily; XMP is most likely found in an APP1 segment (just like EXIF). In rare cases it spans over multiple segments. You could first use PIL/Pillow to create your file and then modify it - inserting additional segments is trivial and needs no additional work.

Upvotes: -1

Related Questions