`img.save()` not saving all `exif` data
What did you do?
- load a png image via
Image.open() - get the
exif_dataviaimg.getexif() exif.update([(36868, f'2022:07:01 00:00:00\0')])img.save(path, exif=exif_data)
What did you expect to happen?
For the Date taken attribute in Windows to change to 2022:07:01 00:00:00
What actually happened?
It did not change, it stayed at the original value
What are your OS, Python and Pillow versions?
- OS: Windows 10
- Python: 3.8.10
- Pillow: 9.1.0
def edit_image(path,name):
img = Image.open(path + "/" + name)
exif = img.getexif()
exif.update([(36868, f'2022:07:01 00:00:00\0')])
exif.update([(36867, f'2022:07:01 00:00:00\0')])
exif.update([(306, f'2022:07:01 00:00:00\0')])
exif.update([(271, f'TEST')])
print(exif)
img.save(path + '_1/' + name, exif=exif)
- I added tag 271 (capturing device maker) for testing as it was working.
- Added 36867 (DateTimeOriginal) to check if another DateTime was changeable, added 306 as it was shown when printing exif with the date that is shown in Date taken by Windows. Both 36867 and 306 didn't change the result.
- Already tried DD:MM:YYYY as it is my local format.
- The
print(exif)shows that the tags have changed in the object. - Already tried it without
\0but no success, i added it as it is stated here that it terminates with NULL.
If I run your code with one of our test images, hopper.png, and open the saved image with Pillow, I can see the new tags. If I use exiftool to inspect the saved file, I can also see the data.
Could you attach a copy of the original PNG? And for those of us who are not Windows users, what is the "Date taken" value that Windows is reporting?
Windows reads the exif tags like this. The tag 306 changes datetime, which was altered in the edited image to 1.12.2010. The other two tags (36867,36868) did not have an effect on the image (tested them individually).
.
I suspect the problem is that DateTimeOriginal/CreateDate are not top-level tags, but exist within the Exif IFD - https://www.awaresystems.be/imaging/tiff/tifftags/exififd.html
If I set them like so, does either of them become the "Date taken"?
from PIL import Image
im = Image.open("test.jpeg")
exif = im.getexif()
# DateTimeOriginal within the Exif IFD
exif.get_ifd(34665)[36867] = "2010:01:01 00:00:00"
# CreateDate within the Exif IFD
exif.get_ifd(34665)[36868] = "2011:01:01 00:00:00"
im.save("out.jpeg", exif=exif)
yes it now saves 2010 as the Date taken value, thanks for clarifying.