当我试图使用PIL调整图像大小(缩略图)时,iptc数据将丢失。
我要做什么来保存缩略图中的iptc数据?
我在枕头库中看到了对IptcImagePlugin的支持,但这只是用于读取图像的iptc数据。如何在图像丢失后将iptc数据写入图像?
发布于 2022-01-14 08:56:10
也许可以做更多的工作,但您可以使用exiftool和ImageMagick提取并插入IPTC数据。
这意味着您应该能够使用exiftool使用Python实现它,方法是修改我的答案这里。或者使用魔棒,它是ImageMagick的Python绑定。如果做不到这一点,我想您可以使用Python模块。
使用exiftool,将IPTC数据从original.jpg复制到new.jpg。
exiftool -tagsfromfile original.jpg -IPTC:all new.jpg在Python中,应该是这样的(未经测试):
import exiftool
# If "exiftool" is not on your PATH, add the full path inside parentheses on next line
with exiftool.ExifTool() as et:
et.execute(b"-tagsfromfile original.jpg -IPTC:all", b"new.jpg")另一种方法是将IPTC数据提取到文件中,然后将这些值修补到您的PIL创建的缩略图中:
exiftool original.jpg -b -IPTC > saved.iptc
exiftool '-IPTC<=saved.iptc' new.jpg使用ImageMagick,解压缩IPTC文件extracted.iptc
magick original.jpg extracted.iptc现在插入到new.jpg中
magick new.jpg -profile extracted.iptc withprofile.jpg或者,将IPTC从original.jpg复制到new.jpg中,生成与上面相同的result.jpg,但一举没有创建临时文件并依赖bash外壳“进程替换”。
magick new.jpg -profile <(magick original.jpg iptc:-) result.jpghttps://stackoverflow.com/questions/70706419
复制相似问题