6

I'm looking for a compute- and memory- efficient way to use ImageMagick in a Python program to retrieve the dimensions of batches of photos.

I first used the current favorite ImageMagick-Python package, Wand. I tried it and it felt slow. So I rigged up a test of it vs simply exec-ing ImageMagick using subprocess.check_output(). Subprocess was more than 10 TIMES faster.

My question: is that other people's experience? Is there a way to use Wand faster?

Wand took 1.2 seconds:

1.jpg: 3264x2448
2.jpg: 1600x1200
3.jpg: 1700x1101
4.jpg: 1600x1200
5.jpg: 3648x2736
6.jpg: 2789x1980
7.jpg: 2400x1600
8.jpg: 3648x2736
processed 8 files in 1.236s

Subprocess only took 0.1 seconds to retrieve the same files:

1.jpg: 3264x2448
2.jpg: 1600x1200
3.jpg: 1700x1101
4.jpg: 1600x1200
5.jpg: 3648x2736
6.jpg: 2789x1980
7.jpg: 2400x1600
8.jpg: 3648x2736
processed 8 files in 0.102s

The Wand code:

for filename in files:
  with wand.image.Image(filename=filename) as img: return img.width, img.height

The subprocess code:

subprocess.check_output(['identify', '-format', '%f:%w:%h\\n', 'path/to/imgs/*.jpg'])
# parse output by splitting each line on the ':'

I realize it's not apples to apples because I'm invoking ImageMagick once per file in Wand. However, I don't see any batch option in Wand, so I believe this is the best Wand can do for my scenario.

Thanks!

Dean Moses
  • 2,372
  • 2
  • 24
  • 36
  • The problem may be that `Wand` is using `ctypes` to provide the interface to `imagemagick`, which may be slow in this particular situation. Did you try to use [`PythonMagick`](http://www.imagemagick.org/download/python/) instead? – Bakuriu Sep 28 '13 at 08:02
  • PythonMagick was last revved Sept 2012 and only has a readme as docs, whereas Wand is extremely recent -- Sept 2013 -- and has beautiful docs. And PythonMagic requires Boost, which I doubt is available in my shared hosting environment. – Dean Moses Sep 28 '13 at 17:42

0 Answers0