Metadata extraction from PNG images

我的未来我决定 提交于 2020-01-04 21:36:30

问题


How to extract metadata from a image like this website? I have used exev2 library but it gives only limited data as compared to this website. Is there some more advanced library?

I have already tried hacoir-metadata Python library.

Also how does Windows extract details of image (the one we see from properties)?


回答1:


PNG files are made up of blocks, most of which are IDAT blocks which contain compressed pixel data in an average PNG. All PNG's start with a IHDR block and end with an IEND block. Since PNG is a very flexible standard in this way, it can be extended by making up new types of blocks--this is how animated Animated PNG works. All browsers can see the first frame, but browsers which understand the types of blocks used in APNG can see the animation.

There are many places that text data can live in a PNG image, and even more places metadata can live. Here is a very convenient summary. You mentioned the "Description tag", which can only live in text blocks, so that it was I'll be focusing on.

The PNG standard contains three different types of text blocks: tEXt (Latin-1 encoded, uncompressed), zTXt (compressed, also Latin-1), and finally iTXt, which is the most useful of all three as it can contain UTF-8 encoded text and can either be compressed or decompressed.

So, your question becomes, "what is a convenient way to extract the text blocks?"

At first, I thought pypng could do this, but it cannot:

tEXt/zTXt/iTXt

Ignored when reading. Not generated.

Luckily, Pillow has support for this - humorously it was added only one day before you asked your original question!

So, without further ado, let's find an image containing an iTXt chunk: this example ought to do.

>>> im = Image.open('/tmp/itxt.png')
>>> im.info 
{'interlace': 1, 'gamma': 0.45455, 'dpi': (72, 72), 'Title': 'PNG', 'Author': 'La plume de ma tante'}

According to the source code, tEXt and zTXt are also covered.

For the more general case, looking over the other readers, the JPEG and GIF ones also seem to have good coverage of those formats as well - so I would recommend PIL for this. That's not to say that the maintainers of hacoir-metadata wouldn't appreciate a pull request adding text block support though! :-)




回答2:


You can try this pre-alpha solution by Daniel Chesterton. I am not sure is it just what you want or is it a part of the wanted solution, but I believe you can sort it out by playing with it.

https://github.com/dchesterton/image




回答3:


I found this code buried in a Pillow pull request

from PIL import PngImagePlugin
info = PngImagePlugin.PngInfo() # read PNG data
info.add_text("foo", "bar") # write PNG data
img.save(filenew, "png", pnginfo=info)


来源:https://stackoverflow.com/questions/24983135/metadata-extraction-from-png-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!