Question

convert image.png -crop 256x256 +repage +adjoin out_%d.png

takes for a large image 20000x8000 aprox 114 seconds. (about 4000 256x256 pngs are generated)

Are there any imagemagick options to improve the speed of cropping?

Was it helpful?

Solution

One thing you can do is be sure to use a "Q8" build of ImageMagick rather than the default "Q16", if your original images have 8-bit samples. Each pixel occupies 8 bytes (16-bit R, G, B, A) even if it's just a black-and-white drawing. Using Q8 cuts that in half. You can change the behavior with the "-limit" option, to use more memory instead of disc.

The -limit option is described in the "options" documentation for ImageMagick.

You can improve the speed of PNG compression by using "-quality 1" which selects Huffman-only compression and the "sub" PNG filter. If you know that the image has fewer than 256 colors, "-quality 0" might be more effective (the time consumed will be about the same but the resulting file will be smaller).

OTHER TIPS

You could also consider libvips. It can do this kind of operation quickly and without using much memory.

I tried a benchmark. With a 20,000 x 8,000 pixel RGB PNG on this machine (four core / eight thread i7). I see:

$ vipsheader big.png 
big.png: 20000x8000 uchar, 3 bands, srgb, pngload
$ /usr/bin/time -f %M:%e convert big.png -crop 256x256 +repage +adjoin out_%d.png
2582228:61.78
$ echo out* | wc
      1    2528   31754

%M:%e means display peak memory and elapsed time, so that's 2.5gb of RAM and 62s of real time to make 2528 PNG tiles.

libvips has a command called dzsave (DeepZoom save) which can write a set of tiles in parallel, and will stream the image rather than loading the whole thing into memory. With a couple of options you can make it produce the same output as your convert command.

I see:

$ /usr/bin/time -f %M:%e vips dzsave big.png x --depth one --tile-size 256 --overlap 0 --suffix .png
161952:9.20
$ echo x_files/0/*.png | wc
      1    2528   49450

So 161mb of ram and 9.2s of real time to make the same 2528 files.

There's a chapter in the docs about dzsave explaining how to use it:

https://libvips.github.io/libvips/API/current/Making-image-pyramids.md.html

As Glenn says, most time is being spent in PNG encode and decode. libpng is very slow, and it uses deflate compression, which is even slower. If you switch to TIFF instead, it gets much quicker:

$ /usr/bin/time -f %M:%e vips dzsave big.tif x --depth one --tile-size 256 --overlap 0 --suffix .tif
163476:1.34

Now it's just 1.34s. JPG would be faster still (since there would be less IO), but I guess that wouldn't work well for floor plans.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top