How to check the correctness of all files inside jpegs directories (requires Linux, sh script)?

Ok, I have a directory (for example, with the name "/ photos") that has different directories (for example, / photos / wedding, / photos / birthday, / photos / graduation, etc. ) in which there are .jpg files. Unfortunately, some jpeg files do not work. I need to find a way to determine which files are broken. I found out that there is a tool called imagemagic that can help a lot. If you use it as follows:

identify -format '%f' whatever.jpg 

it prints the file name only if the file is valid, if it does not print something like "define: not a jpeg file: starts with 0x69 0x75` whatever.jpg '@ jpeg.c / EmitMessage / 232.". Thus, the correct solution is to find all files ending in ".jpg", apply "identify" to them, and if the result is just a file name - do nothing, and if the result is different from the file name - then save the file name where Something (for example, in the file "errors.txt").

Any ideas how I can do this?

+6
shell image-processing
source share
4 answers

You can put this in a bash script file or run it directly: find |grep ".jpg$" |xargs identify -format '%f' 1>ok.txt 2>errors.txt

+5
source share

One of the problems with identify -format is that it does not actually verify that the file is not corrupted, it just ensures that it is really jpeg.

To verify this, you need to convert something. But the conversion that comes with ImageMagick seems to silently ignore non-fatal errors in jpeg (for example, it is truncated.)

One thing that works is this:

 djpeg -fast -grayscale -onepass file.jpg > /dev/null 

If it returns an error code, the file has a problem. If not, thatโ€™s good.

There are other programs that can be used.

+11
source share

Short term version:

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

You may not need the same search options, but jpeginfo is the solution that worked for me:

find . -type f -iname "*.jpg" -o -iname "*.jpeg"| xargs jpeginfo -c | grep -E "WARNING|ERROR" | cut -d " " -f 1

as a script (as pointed out in this question)

 #!/bin/sh find . -type f \ \( -iname "*.jpg" \ -o -iname "*.jpeg" \) \ -exec jpeginfo -c {} \; | \ grep -E "WARNING|ERROR" | \ cut -d " " -f 1 

I was listed in jpeginfo for this http://www.commandlinefu.com/commands/view/2352/find-corrupted-jpeg-image-files and this is explained by mix find -o OR with -exec

+6
source share

This script will print the names of damaged files:

 #!/bin/bash find /photos -name '*.jpg' | while read FILE; do if [[ $(identify -format '%f' "$FILE" 2>/dev/null) != $FILE ]]; then echo "$FILE" fi done 

You can run it as is or as ./badjpegs > errors.txt to save the output to a file.

To break it down, the find finds *.jpg files in /photos or in any of its subdirectories. These file names are passed to the while loop, which reads them one at a time in the $FILE variable. Inside the loop, we get the result of identify using the $(...) operator and check if it matches the file name. If not, the file is bad and we print the file name.

This can be simplified. Most UNIX commands indicate success or failure in their exit code. If the identify command does this, you can simplify the script to:

 #!/bin/bash find /photos -name '*.jpg' | while read FILE; do if ! identify "$FILE" &> /dev/null; then echo "$FILE" fi done 

Here the condition is simplified to if ! identify; then if ! identify; then if ! identify; then , which means that "identified a failure?"

+2
source share

All Articles