LeoHsiao1 / pyexiv2

Read and write image metadata, including EXIF, IPTC, XMP, ICC Profile.
GNU General Public License v3.0
196 stars 39 forks source link

How to catch "bad metadata" vs no metadata #123

Closed fire-eggs closed 8 months ago

fire-eggs commented 9 months ago

I have some images which have invalid metadata. I need to be able to distinguish between:

  1. An image which has invalid metadata
  2. An image which doesn't have a particular metadata key
  3. An image which does have a particular metadata key
  4. A file path which is not a valid image

How might I do this using pyexiv2?

Thank you!

LeoHsiao1 commented 9 months ago

Hi pyexiv2 can accomplish these four tasks. But its answer may be different from other software. There are popular specifications about image metadata, such as EXIF, IPTC, XMP. But not all companies' software strictly adheres to these specifications. For example: pyexiv2 thinks that an image's metadata has a formatting error and cannot be parsed. But another program can parse the metadata. In summary, if you need to be strict about determining whether metadata is valid or not, then you need to delve into the metadata specification or even write your own code.

LeoHsiao1 commented 9 months ago

If you don't need to strictly consider metadata specifications, you can use the following options:

  1. When you open an image file, if the path to the file is wrong, or if the metadata is in the wrong format, pyexiv2 will throw an exception, which you can handle with the try catch statement.

    >>> import pyexiv2
    >>> path = 'test/1.jpg'
    >>> img = pyexiv2.Image(path)
    RuntimeError: test/1.jpg: Failed to open the data source: No such file or directory (errno = 2)
    >>> path = 'test/1.txt' 
    >>> img = pyexiv2.Image(path)                     
    RuntimeError: test/1.txt: The file contains data of an unknown image type
  2. If the image is successfully opened, you can determine whether a metadata key exists:

    if img.read_exif().get('key1'):
    ...
fire-eggs commented 8 months ago

Thank you for the samples!