Sunday, May 6, 2012

How to check a jpg is corrupted?

The best way to analyze if a jpeg is not corrupted is to open it, but some times, it happens, that you have a bunch of files to check.

To perform it, you can run jpeginfo which will display an error message if there is a problem. To find all the images:

find . -name "*jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR"

A small explanation is needed here:


  • find . -name "*.jpg" : we look in the current folder for all the files containing jpg extension
  • For each of these images we run jpeginfo -c "filename" (filename is automatically replacing the the curly brackets)
  • From the previous result, we get all the strings containing WARNING or ERROR. Be aware that if the file name contains this string, it will be displayed


In my case, the images were automatically generated so I do not care and I would actually love to delete them:

find . -name "*.jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR" | awk '{print $1}' | xargs rm
In this command, we use the same result as before:
  • but we take only the first item of each line (which is the filename)
  • We remove all of them using: xargs rm

No comments:

Post a Comment