1

Is it possible to determine whether an image contains exif data or not? I tried using pyexiv2 as follows:

import pyexiv2 as pex
pex.metadata("test.jpg")
metadata.read()
print metadata.exif_keys

Now if there is no EXIF data, then the last line will print an empty list. Is this the only way to do it or can I do it in any other way.

securecoding
  • 2,763
  • 2
  • 15
  • 14

3 Answers3

1

A solution unrelated to Python, but which may be useful for those who, like myself, land here from a search.

Use exiftool directly in the shell:

exiftool -exif -if '$exif' $YourFile

The -Exif property given by exiftool obviously only exists if the file does have Exif metadata. The -if condition will give non-zero exit status if the property does not exist.

Depending on if the file contains an Exif part, it will output either something like

EXIF : (Binary data 23929 bytes, use -b option to extract)

or

1 files failed condition

To use it in a script, and just use the exit code, without any output from exiftool itself:

if exiftool -exif -if '$exif' "$YourFile" >/dev/null; then
    echo "Yes. Exif found in $YourFile"
else
    echo "No Exif in $YourFile"
fi
mivk
  • 13,452
  • 5
  • 76
  • 69
0

pyexiv2 is a good tool for manipulation of EXIF data. So if you're asking in terms of development, then you have the answer right there and I'm not sure exactly what you're looking for. Do you just want a tool to check by hand whether an image has EXIF data?

Then I'd recommend exif-py - really simply script that displays the data cleanly if it exists, and tells you if it does not.

Sajjan Singh
  • 2,523
  • 2
  • 27
  • 34
  • I'm basically looking to extract EXIF data from a set of images. If the image does not contain exif data then I'll move on to next image but if the image does contain EXIF then I process it. With this way, I think the only way is to use the method suggested by @Will Harrison – securecoding Apr 03 '13 at 02:31
  • @securecoding if you're going to use my way, can I get the check please? – will Apr 03 '13 at 14:45
0

Not entirely sure, as I've never used this module or played with images, for that matter. Can you not just do something like this? I looked at the documentation and it says that metadata.exif_keys is a list. It seems you would only have to check whether or not the list is empty.

if metadata.exif_keys:
    print(metadata.exif_keys)
will
  • 1,491
  • 1
  • 19
  • 28