您好我正在处理一些代码,其中图像将被压缩并将评论从旧图像传输到新压缩图像。压缩工作顺利,但新图像中的评论传输不起作用,并且没有显示在图像的属性 - >评论中。
我还使用 [pyexiv2] https://github.com/LeoHsiao1/pyexiv2进行元数据读写
在第一次运行时它工作正常,但在以后的运行中,评论不再显示在图像属性 -> 评论中。我试过查看代码,也许我只是不小心编辑了一些东西,但似乎并非如此。
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 *****')