22 August, 2008

HowTo find inconsistency between image format and file extension

Sometimes image files have inconsistency between format and extension.
For example: .jpeg file saved as .png file.

I wrote simple script in order to find such files:
#!/bin/bash

process() {
while read; do
diff=`identify -format "%m %e" "$REPLY" | tr [A-Z] [a-z] | egrep -v "^(png png|gif gif|jpg jpg|jpeg jpg|jpeg jpeg)"`
test -n "$diff" && echo $diff $REPLY
done
}

find -type f -iregex ".*\.\(png\|gif\|jpg\|jpeg\)$" | process

It outputs information about real file format, file extension and filename in case of
difference between format and extension.
Like this:

jpeg png images/rating_0.png
png gif images/left.gif
gif png images/border_tr.png