securecoding
securecoding

Reputation: 2903

Find if an image has EXIF or not

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.

Upvotes: 1

Views: 3109

Answers (3)

mivk
mivk

Reputation: 14919

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

Upvotes: 1

will
will

Reputation: 1511

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)

Upvotes: 0

Sajjan Singh
Sajjan Singh

Reputation: 2553

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.

Upvotes: 0

Related Questions