1

Challenge: I need to parse the output from imagemagick's "identify" command to detect the presence of a particular string. If it doesn't exist, then I echo the filename to the screen.

Background: I have roughly 100k thumbnail images (Example thumbnail that needs to be analyzed). I need to inspect each thumbnail to ensure it was rendered correctly. Luckily, "rendered correctly" can be determined by the presence of red pixels in the thumbnail. Using imagemagick's "identify" command, I can output the colormap which will contain the line, "(255, 0, 0) #FF0000 red".

Need: Using a bash script, I can easily get all the file names and iterate through them. I need to figure out how to silently parse through the output and look for a string match and echo the filename if not found.

Specifically, what I'm looking to do is create a script that does the following:

1) finds all the *.png files in a particular directory.

2) for each file, run "identify -verbose" and silently pass the output to...

3) analyze the output to see if the string "(255, 0, 0) #FF0000 red" exists.

3a) if it does, I continue silently to the next file.

3b) if it does not, I echo the file name.

Ultimately, I'd be left with a short list of thumbnails that did not contain any red pixels and require manual inspection.

Any help creating this script would be most appreciated.

0

To find files which don't have red pixels:

for i in *png; do identify -verbose "$i" | gawk -e "{print \"$i\", \$0}" | fgrep -q '(255,  0,  0) #FF0000 red' || echo $i; done

To find files which have red pixels:

for i in *png; do identify -verbose "$i" | gawk -e "{print \"$i\", \$0}" | fgrep -q '(255,  0,  0) #FF0000 red' && echo $i; done

The gawk command adds the filename to each line of output from the identify command. Using fgrep rather than grep just looks for exact text matches (quicker). The -q flag to fgrep tells it not to output any lines; just to set the status to success or failure.

The script should be safe for filenames which contain spaces.

  • Thanks so much, @nickcrabtree. What about the inverse? I want to print the name only if the string doesn't appear. – sstringer Oct 10 '15 at 17:03
  • There, should be fixed now. A bit neater code too. – nickcrabtree Oct 10 '15 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.