我使用来自PngImagePlugin
模块的PngImageFile
和PngInfo
将元数据存储在枕头中,方法是遵循Jonathan Feenstra在回答中给出的代码:How do I save custom information to a PNG Image file in Python?
代码是:
from PIL.PngImagePlugin import PngImageFile, PngInfo
targetImage = PngImageFile("pathToImage.png")
metadata = PngInfo()
metadata.add_text("MyNewString", "A string")
metadata.add_text("MyNewInt", str(1234))
targetImage.save("NewPath.png", pnginfo=metadata)
targetImage = PngImageFile("NewPath.png")
print(targetImage.text)
>>> {'MyNewString': 'A string', 'MyNewInt': '1234'}
现在,我想删除以前添加到图像中的附加元数据,即文本字符串。如何删除先前添加的PNG图像上的元数据
在复制
targetImage
时,text
属性似乎没有被保留。因此,如果在运行时需要没有附加元数据的图像,只需复制一份即可另一方面,您可以再次保存
targetImage
,但不使用pnginfo
属性。打开后,text
属性存在,但为空。也许,在save
调用中,pnginfo=None
是隐式设置的下面是一些演示代码:
相关问题 更多 >
编程相关推荐