EDIT: If you simply want to copy EXIF from one image to another, an easy was might be this
from PIL import Image
# load old image and extract EXIF
image = Image.open('orig.jpg')
exif = image.info['exif']
# load new image
image_new = Image.open('modi.jpg')
image_new.save('modi_w_EXIF.jpg', 'JPEG', exif=exif)
I found this example amongst others here. I noticed this method copied the image thumbnail (not the image information!) to the new file. You might want to set the thumbnail anew.
Another example from the same page seems to be even simpler:
import jpeg
jpeg.setExif(jpeg.getExif('orig.jpg'), 'modi.jpg')
I couldn't install the required module and test it, though, but it might be worth a try.
Concerning the EXIF
module:
You overwrite the image because img1
is not just the EXIF data but the entire image. You would have to do the following steps:
- load the image containing the EXIF info and get that information
- load the new image which lacks that info
- overwrite the new image's EXIF
- save the new image with EXIF
Something in the likes of:
f_path_1 = "path/to/original/image/with/EXIF.jpg"
f_path_2 = "path/to/new/image/without/EXIF.jpg"
f_path_3 = "same/as/f_path_2/except/for/test/purposes.jpg"
from exif import Image as exIm # I imported it that way bacause PIL also uses "Image"
with open(f_path_1, "rb") as original_image:
exif_template = exIm(original_image)
if not exif_template.has_exif:
Warning("No EXIF data found for " + f_path_1)
tags = exif_template.list_all()
with open(f_path_2, "rb") as new_rgb_image:
exif_new = exIm(new_rgb_image)
for tag in tags:
try:
exec("exif_new." + tag + "=exif_template." + tag)
except:
pass
with open(f_path_3, "wb") as new_rgb_image:
new_rgb_image.write(exif_new.get_file())
Note: For some reason EXIF
was only able to write some, but not all tags from the original image when I tried it.