Search code examples
pythonimagemagicksubprocessimagemagick-convert

Imagemagick GIF Dimensions Repeat Over and Over


I am using Imagemagick identify in a Python script to determine an image's dimensions. When analyzing animated GIFs, the dimensions repeat again and again, e.g. 500 375500 375500 375500 375500 375500 375500 375500 375.

Why is this happening? How can I clean up this output into a simple width / height?

The Python script that produces this result:

#!/usr/bin/env python
import subprocess, logging

# identify -ping -format "%w %h" test.gif
dimensions = subprocess.check_output([
    r'C:\Program Files\ImageMagick-6.9.3-Q16\identify.exe',
    '-ping',
    '-format',
    '%w %h',
    "../www/content/test.gif"
])

# b string to utf-8 string
dimensions = dimensions.decode("utf-8")

logging.warning(dimensions)

And the output in the log is as follows:

WARNING:root:500 375500 375500 375500 375500 375500 375500 375500 375

Solution

  • Edit

    The comment on this answer is correct - the best way is to use image.gif[0].

    Original Answer

    The dimensions are repeated again and again because the animated gif has multiple frames. I figured it out when I noticed that each gif's dimensions were repeated different numbers of times.

    I changed '%w %h' to '%w %h;' and used split to isolate the first frame.

    dimensions = dimensions.decode("utf-8").split(';') #375 500