modify image and save with exif data

Vincent Vande Vyvre vincent.vandevyvre at swing.be
Mon Dec 17 01:39:31 EST 2012


Le 16/12/12 20:43, jwe.van.dijk at gmail.com a écrit :
> I want to resize an image but retain the exif data
> I now have:
> import Image
>
> img = Image.open('photo.jpg')
> img.thumbnail((800, 800), Image.ANTIALIAS)
> img.save('photo800.jpg', 'JPEG')
>
> The saved image photo800.jpg has no exif info anymore.
> I would so much like to have it retained in particular the exposure and gps data.
>
> I use Python 2.7 with PIL 1.17 on Ubuntu 12.04
> For exif reading/writing I use pyexiv2 0.3.2 but apparently you cannot add tags to an image that has none; only modifying existing ones seems to work.
>
> Thanks for any helpful suggestions,
> Janwillem
Hi,

If a tag is not already set you can create it with pyexiv2.

Example:
----------------------------------------------------------------------
self.data = pyexiv2.ImageMetadata(img.jpg)
self.data.read()

tag = "Exif.Image.ImageWidth"
try:
    self.data[tag].value = sizes[0]
except:
    self.data[tag] = pyexiv2.ExifTag(tag, sizes[0])
-----------------------------------------------------------------------

A more complete example:
http://bazaar.launchpad.net/~vincent-vandevyvre/oqapy/serie-1.0/view/head:/oqapy-1.0/metadata.py



-- 
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>



More information about the Python-list mailing list