how to speed up image converting process?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
Henry772
Posts: 1
Joined: 2019-02-11T02:12:43-07:00
Authentication code: 1152

how to speed up image converting process?

Post by Henry772 » 2019-02-11T02:15:39-07:00

I have a few thousand images to be processed so each millisecond counts. Each image is ~2-3Mb in size.

Source file fed to the converter: image.jpg

Files to be generated out of the source:

Code: Select all

orig_image.jpg      // original image
1024x768_image.jpg  // large image
250x250_image.jpg   // thumbnail 1
174x174_image.jpg   // thumbnail 2
While browsing different topics on imagemagick convert performance I got a feeling that a single command should be way faster than individual converts for each image size. Also a memory utilization was mentioned as a performance boost. (ImageMagick batch resizing performance)

Multiple command conversion (each command run via php's exec() in a loop):

Code: Select all

convert "image.jpg" \
    -coalesce -resize "1024x768>" +repage "1024x768_image.jpg"

convert "1024x768_image.jpg" \
    -coalesce \
    -resize "250x250>" \
    +repage \
    -gravity center \
    -extent "250x250" "250x250_image.jpg"

convert "1024x768_image.jpg" \
    -coalesce \
    -resize "174x174>" \
    +repage \
    -gravity center \
    -extent "174x174" "174x174_image.jpg"

mv image.jpg orig_image.jpg
Single command conversion incorporating ImageMagicks mpr:

Code: Select all

convert "image.jpg" -quality 85 -colorspace rgb -coalesce \
    -resize "1024x768>" \'
    -write "1024x768_image.jpg" \
    -write mpr:myoriginal +delete \
    mpr:myoriginal -coalesce \
    -resize "250x250>" \
    -gravity center \
    -extent "250x250" \
    -write "250x250_image.jpg" +delete \
    mpr:myoriginal -coalesce \'
    -resize "174x174>" \
    -gravity center \
    -extent "174x174" \
    -write "174x174_image.jpg"
After performance testing the results are somewhat unexpected. Single command convert in a loop finishes in 62 seconds while multiple command conversion executes in just 16 seconds?

Code: Select all

# convert -version
Version: ImageMagick 7.0.2-1 Q8 i686 2017-02-03 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in): bzlib freetype jng jpeg lzma png tiff wmf xml zlib
Also installed libjpeg-turbo jpg processing library but I cannot tell (don't know how to check) if ImageMagic is using it or the old libjpeg.

Any ideas how to speed up image converting process?

Edit: Don't know how to format it properly here on stackoverflow, but I just noticed that single line command had an argument "-colorspace rgb" and multiple line commands did not which actually results in such strange results where multiple commands are processed faster.

Removed the "-colorspace rgb" argument after which the MPR convert version works the best and gave additional boost in performance.

To sum it all up I ended up using this command:

Code: Select all

// MPR
convert "orig_image.jpg" -quality 80 -coalesce \
    -resize "1024x768>" \
    -write 1024x768_image.jpg \
    -write mpr:myoriginal +delete \
    mpr:myoriginal -resize "250x250>" \
    +repage -gravity center -extent "250x250" \
    -write "250x250_image.jpg" \
    -write mpr:myoriginal +delete \
    mpr:myoriginal -coalesce -resize "174x174>" \
    +repage -gravity center -extent "174x174" \
    -write "174x174_image.jpg"
imagemagick: ifacetimeapp

snibgo
Posts: 12010
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to speed up image converting process?

Post by snibgo » 2019-02-11T07:46:22-07:00

A purist would say that resizing should always be done in linear colorspace, "RGB". I assume your input is non-linear sRGB, and you want the outputs to be the same. If so, then you could "-colorspace RGB" the input, and "-colorspace sRGB" the outputs.

However, for ordinary photos, I doubt that you would see any difference from doing the resizing in sRGB, so no need for any "-colorspace" operations.


Your final version has:

Code: Select all

-write mpr:myoriginal +delete \
 mpr:myoriginal ...
You need to write the mpr so you can re-use it later. But you don't need to delete it then immediately re-read it. I suggest you remove "+delete mpr:myoriginal".

There may be a small performance improvement if you change the command to use "+clone" instead of mpr.
snibgo's IM pages: im.snibgo.com

Post Reply