Hello I'm working on some code where the image will be compress and transfer the comments from the old image to newly compress image. the compression works smoothly but the transfer of comments in the new image doesn't work and is not showing in the properties -> comments of the image.
I'm also using [pyexiv2] https://github.com/LeoHsiao1/pyexiv2 for the metadata read and write
On the first run it works fine but on the later runs the comments is not showing in the Image properties -> comments anymore. I've tried looking at the code maybe I just edited something accidentally but that doesn't seem to be the case.
import glob2
from PIL import Image, ImageDraw, ImageFont
import os
import pyexiv2
def getImgPaths():
image_paths = glob2.glob('images/**/*.jpg')
return image_paths
# ? copy metadata from original to new
def moveMetaData(base_image, new_image):
print('***** Moving meta-data *****')
i = pyexiv2.Image(base_image)
meta_comment = i.read_exif()
# ? print comment
print("initial metacomment : " + meta_comment["Exif.Photo.UserComment"])
# ! encode comment to new image
new_image_encode = pyexiv2.Image(new_image)
# print("New Image Initial Tags: " + str(new_image_encode))
new_image_encode.modify_exif(
{"Exif.Photo.UserComment": meta_comment["Exif.Photo.UserComment"]})
o = new_image_encode.read_exif()
print("Inserted Comment to new image : " + o["Exif.Photo.UserComment"])
def compressImage(image_path):
im = Image.open(image_path)
image_name = os.path.basename(os.path.normpath(image_path))
print('Input file size : ', im.size)
print('Input file name : ', image_name)
print('Input Image Size : ', os.path.getsize(image_path))
out_parent_dir = os.path.join('out', image_path.split(image_name)[0])
print(out_parent_dir)
newpath = os.path.join('out', image_path)
if not os.path.exists(out_parent_dir):
os.makedirs(out_parent_dir)
im.save(os.path.join(out_parent_dir, image_name),
optimize=True, quality=50)
print('Success ' + image_path)
# ? copy metadata to new image
moveMetaData(image_path, newpath)
print('***** Compression Started *****')
for image_path in getImgPaths():
try:
compressImage(image_path)
except Exception as e:
print(e)
with open('logs/errors.txt', 'a') as fh:
print(str(e) + " : " + str(image_path), file=fh)
pass
print('***** Compression Ended *****')