Page 1 of 1

convert always has exit code 0 (success) even if failed

Posted: 2018-08-10T04:07:55-07:00
by fraz
I'm writing a script to recursively test JPGs for corruption using imagemagick to open and resize the image.
And on testing the error codes returned on a known bad jpg I always get a "0" success.

convert -resize 100 BNE4_0288.jpg /tmp/out.jpg
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST5 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST6 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST7 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST0 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST1 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
ETC!!
echo $?
gives me a 0.

if I check out.jpg it's truncated and mostly grey. Sure it was made, but I wouldn't call that a '0'.

Can this be assigned a non-zero error code?

Re: convert always has exit code 0 (success) even if failed

Posted: 2018-08-10T04:26:51-07:00
by fraz
Here's the example images:
https://www.dropbox.com/sh/bg3ht5k62ml0 ... 8qqTa?dl=0

The corruption seems to be random bytes/blocks corrupted by a bad USB flash drive.

I've also just tested it with a truncated image made using dd count=xxx and it also fails:
convert-im6.q16: Premature end of JPEG file `qqq.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: premature end of data segment `qqq.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
But echo $? returns 0 (success).

Re: convert always has exit code 0 (success) even if failed

Posted: 2018-08-10T04:28:51-07:00
by snibgo
Those are warnings. If you want to treat warnings as errors, see http://www.imagemagick.org/script/comma ... d-warnings

Re: convert always has exit code 0 (success) even if failed

Posted: 2018-08-10T04:35:49-07:00
by fraz
Ah, thanks.
That makes it work as expected.
convert -regard-warnings -resize 100 qqq.jpg out.jpg
Echo $? gives me a 1.

I would have thought this would be a more sensible default and -disregard-warnings should be the option.
Feel free to move this out of the bug section though.

Thanks again!

Re: convert always has exit code 0 (success) even if failed

Posted: 2018-08-10T04:56:45-07:00
by fraz
And for anyone looking for the final script. Here it is:

Code: Select all

#!/bin/bash

newline='
'

OIFS=$IFS
IFS=$newline
files=($(find . -iname "*.$1"))
IFS=$OIFS

for i in "${files[@]}"
do

echo -n "Testing: $i.."
convert -regard-warnings -resize 100 "$i" /dev/null
error=$?

if [ $error -ne 0 ]
then
  echo "this didn't work!!"
  echo "$i"
  echo "$i" >> ~/brokenJPGs.txt
  file "$i" >> ~/brokenFILE.txt
else
  echo "OK"

fi

done

wc ~/brokenJPGs.txt

exit 0
Make sure you pass the extension to test as the first argument. It produces a file called brokenJPGs.txt as a file list for further processing. And brokenFILE.txt which has the 'file' output to find misnamed things etc.

Re: convert always has exit code 0 (success) even if failed

Posted: 2018-08-10T05:31:50-07:00
by snibgo
IM raises an error when it can't read an image, or can't create an output image, and so on. If there is a problem with data or metadata, but IM can still process the image, it raises a warning.

Personally, I would like an extra level, so we might have:
3: can't process the image
2: can process the image, but it is probably wrong (eg "corrupt JPEG data")
1: can process the image, but it's probably okay (eg "ASCII value for tag "Make" contains null byte in value")
0: no problem at all