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!
Wand
is usingctypes
to provide the interface toimagemagick
, which may be slow in this particular situation. Did you try to usePythonMagick
instead? – Biased