我正在编写一个脚本来将元数据保存到 jpg 图像并为 Wordpress 调整它们的大小。它一直进展顺利,使用 jpegs 和 pngs,直到我尝试在使用 PIL.Image.thumbnail() 调整 jpg(但不是 png)大小后直接在图像上使用它
我试图将它全部压缩成一个易于阅读的课程,以帮助任何可以帮助回答问题的人。主要类是 MetaGator(元缓解器),它将属性“标题”和“描述”写入文件中相应的元数据字段。
import PIL
from PIL import Image
from PIL import PngImagePlugin
from pyexiv2.metadata import ImageMetadata
from PIL import Image
import os
import re
from time import time, sleep
class MetaGator(object):
"""docstring for MetaGator"""
def __init__(self, path):
super(MetaGator, self).__init__()
if not os.path.isfile(path):
raise Exception("file not found")
self.dir, self.fname = os.path.split(path)
def write_meta(self, title, description):
name, ext = os.path.splitext(self.fname)
title, description = map(str, (title, description))
if( ext.lower() in ['.png']):
try:
new = Image.open(os.path.join(self.dir, self.fname))
except Exception as e:
raise Exception('unable to open image: '+str(e))
meta = PngImagePlugin.PngInfo()
meta.add_text("title", title)
meta.add_text("description", description)
try:
new.save(os.path.join(self.dir, self.fname), pnginfo=meta)
except Exception as e:
raise Exception('unable to write image: '+str(e))
elif(ext.lower() in ['.jpeg', '.jpg']):
try:
imgmeta = ImageMetadata(os.path.join(self.dir, self.fname))
imgmeta.read()
except IOError:
raise Exception("file not found")
for index, value in (
('Exif.Image.DocumentName', title),
('Exif.Image.ImageDescription', description),
('Iptc.Application2.Headline', title),
('Iptc.Application2.Caption', description),
):
print " -> imgmeta[%s] : %s" % (index, value)
if index in imgmeta.iptc_keys:
imgmeta[index] = [value]
if index in imgmeta.exif_keys:
imgmeta[index] = value
imgmeta.write()
else:
raise Exception("not an image file")
def read_meta(self):
name, ext = os.path.splitext(self.fname)
title, description = '', ''
if(ext.lower() in ['.png']):
oldimg = Image.open(os.path.join(self.dir, self.fname))
title = oldimg.info.get('title','')
description = oldimg.info.get('description','')
elif(ext.lower() in ['.jpeg', '.jpg']):
try:
imgmeta = ImageMetadata(os.path.join(self.dir, self.fname))
imgmeta.read()
except IOError:
raise Exception("file not found")
for index, field in (
('Iptc.Application2.Headline', 'title'),
('Iptc.Application2.Caption', 'description')
):
if(index in imgmeta.iptc_keys):
value = imgmeta[index].value
if isinstance(value, list):
value = value[0]
if field == 'title': title = value
if field == 'description': description = value
else:
raise Exception("not an image file")
return {'title':title, 'description':description}
if __name__ == '__main__':
print "JPG test"
fname_src = '<redacted>.jpg'
fname_dst = '<redacted>-test.jpg'
metagator_src = MetaGator(fname_src)
metagator_src.write_meta('TITLE', time())
print metagator_src.read_meta()
image = Image.open(fname_src)
image.thumbnail((10,10))
image.save(fname_dst)
metagator_dst = MetaGator(fname_dst)
metagator_dst.write_meta('TITLE', time())
print metagator_dst.read_meta()
sleep(5)
metagator_dst = MetaGator(fname_dst)
metagator_dst.write_meta('TITLE', time())
print metagator_dst.read_meta()
print "PNG test"
fname_src = '<redacted>.png'
fname_dst = '<redacted>-test.png'
metagator_src = MetaGator(fname_src)
metagator_src.write_meta('TITLE', time())
print metagator_src.read_meta()
image = Image.open(fname_src)
image.thumbnail((10,10))
image.save(fname_dst)
metagator_dst = MetaGator(fname_dst)
metagator_dst.write_meta('TITLE', time())
print metagator_dst.read_meta()
sleep(5)
metagator_dst = MetaGator(fname_dst)
metagator_dst.write_meta('TITLE', time())
print metagator_dst.read_meta()
我用来测试它的代码在main中,并给出以下输出:
JPG test
-> imgmeta[Exif.Image.DocumentName] : TITLE
-> imgmeta[Exif.Image.ImageDescription] : 1429683541.3
-> imgmeta[Iptc.Application2.Headline] : TITLE
-> imgmeta[Iptc.Application2.Caption] : 1429683541.3
{'description': '1429683541.3', 'title': 'TITLE'}
-> imgmeta[Exif.Image.DocumentName] : TITLE
-> imgmeta[Exif.Image.ImageDescription] : 1429683541.31
-> imgmeta[Iptc.Application2.Headline] : TITLE
-> imgmeta[Iptc.Application2.Caption] : 1429683541.31
{'description': '', 'title': ''}
-> imgmeta[Exif.Image.DocumentName] : TITLE
-> imgmeta[Exif.Image.ImageDescription] : 1429683546.32
-> imgmeta[Iptc.Application2.Headline] : TITLE
-> imgmeta[Iptc.Application2.Caption] : 1429683546.32
{'description': '', 'title': ''}
PNG test
{'description': '1429683546.32', 'title': 'TITLE'}
{'description': '1429683546.83', 'title': 'TITLE'}
{'description': '1429683551.83', 'title': 'TITLE'}
如您所见,在 JPG 测试中,它在普通文件上运行良好,但在 PIL 调整图像大小后根本无法运行。.PNG 不会发生这种情况,它使用 PIL 保存元数据,这会让我认为问题出在 pyexiv2 上。
我应该尝试什么?任何的意见都将会有帮助。
谢谢你。