Error with extracting gif frames, and catching the error in python

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
netw1z
Posts: 1
Joined: 2016-04-03T12:16:04-07:00
Authentication code: 1151

Error with extracting gif frames, and catching the error in python

Post by netw1z »

I am using Version: ImageMagick 6.9.3-7 Q16 x86_64 2016-04-03

When I attempt to use convert to extract frames for a gif sometimes i get this with corrupt gifs:

convert -coalesce examine.gif video.png
convert: corrupt image `examine.gif' @ error/gif.c/ReadGIFImage/1368.
convert: no images defined `video.png' @ error/convert.c/ConvertImageCommand/3252.

Here is a link to an example image that chokes here:
http://www.image-share.com/upload/3207/277.gif

When I execute it from a subprocess in python - I can't seem to catch the error output:
command = ["convert" , "-coalesce", "-scene" , str(file_counter) , infile, outfile]
output,error = subprocess.Popen(command, universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=r'/home/mvgen/fixgifs').communicate()

Output and Error are generally blank or say NONE.

Anyone have some hints for a work around like this with broken gifs? Identify will give a frame count from even corrupt gifs.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Error with extracting gif frames, and catching the error in python

Post by fmw42 »

Proper IM 6 syntax has the -coalesce after reading the input image. Nevertheless, if the image is corrupt, IM will give an error message. I am not sure why IM says it is corrupt, since it plays fine in a browser.

I do not know Windows and you do not say what platform your are on, but in Unix, you can tell if there is an error by looking at the error code. 0 means success and 1 means failure.

Using null: to avoid writing any output, just for testing if an error.

Code: Select all

convert 277.gif -coalesce null:
convert: corrupt image `277.gif' @ error/gif.c/ReadGIFImage/1368.
convert: no images defined `null:' @ error/convert.c/ConvertImageCommand/3252.

Code: Select all

echo "$?"
1


Or

Code: Select all

convert 277.gif -coalesce null: || echo "error"
convert: corrupt image `277.gif' @ error/gif.c/ReadGIFImage/1368.
convert: no images defined `null:' @ error/convert.c/ConvertImageCommand/3252.
error


Sorry I do not know Python and from a subprocess, you may have to send the result to 2>&1

Code: Select all

convert 277.gif -coalesce null: 2>&1 || echo "error"
Post Reply