Home

Awesome

speedtest-resize

Compare various Image resize algorithms for the Go language

I am writing a web gallery called gonagall in Go (https://github.com/fawick/gonagall). For that, I need a efficient solution for scaling and resizing a lot of images (mostly JPGs) to generate thumbnails and bandwidth-friendly sized copies from high-resolution original photo files.

In this project I compare the speed of a few selected image resizing algorithms with each other as well as with ImageMagick and GraphicsMagick. The competitors are

Installation

To run the tests go get the source and compile/run it:

$ go get -u github.com/fawick/speedtest-resize -tags all
$ cd $GOPATH/src/speedtest-resize
$ go run main.go <jpg file folder>

Alternatively, call the go command (or the compiled binary) from the image folder without supplying a parameter

$ cd <jpg file folder>
$ go run $GOPATH/src/speedtest-resize/main.go

A the package requires different 3rdparty libraries to be installed, you can use build tags to control what libraries to use. The following build tags are available:

TagDescription
opencvInclude lazywei/go-opencv in the tests.
imagickInclude gographics/imagick in the tests.
vipsInclude DAddYE/vips in the tests.
fastjpegInclude camlistore/fastjpeg in the tests.
allAn alias for opencv imagick fastjpeg vips.
nopureDon't include the Pure Golang packages
noexecDon't run the tests that execute other programs.

The default go get without any tags will try the packages that are pure go and the external programs but not use any non-Go library.

Benchmark

Im my test scenario all of these tools/packages are unleashed on a directory containing JPG photo files, all of which have a resolution of 5616x3744 pixels (aspect ratio 2:1, both landscape and portrait).

For each tool/package and for all files, the total time for loading the original file, scaling the image to a thumbnail of 150x100 pixels, and writing it to a new JPG file is measured. In the end, the total runtime for processing the 10 first files and the average time per file is printed for each tool/package.

The scenario is run on a Intel(R) Pentium(R) Dual T2390 @ 1.86GHz running Ubuntu 14.04. Here are the results:

TableTime (avg.)Size (avg.)Pure Go
vipsthumbnail0.120s0.065%
ImageMagick_thumbnail0.326s0.242%
vips0.339s0.100%
magickwand_box1.148s0.538%
ImageMagick_resize2.316s0.626%
rez_bilinear2.913s0.053%X
Nfnt_NearestNeighbor3.498s0.057%X
imaging_box4.734s0.057%X
gift_box4.746s0.057%X

Yet another scenario ran by lazywei, 2.5GHz Intel Core i5, Mac OS X 10.9.1:

TablesAverage time per file
magickwand_box155.371531ms
imaging_Box463.459339ms
Nfnt_NearestNeighbor1.436507946s
OpenCv97.353041ms

Yet another scenario ran by bamiaux, 3.3GHz Intel Core i5, win 7:

TablesAverage time per file
rez_bilinear148ms
imaging_Box243ms
Nfnt_NearestNeighbor233ms

A new scenario ran by nono, 3.4GHz Intel Core i7, Ubuntu 16.10:

TableTime (file avg.)Size (file avg.)Pure Go
ImageMagick_thumbnail0.057s0.361%
vips0.070s0.260%
epeg0.079s0.207%
fastjpeg0.082s0.186%
opencv0.110s0.597%
vipsthumbnail0.115s0.441%
GraphicsMagick_thumbnail0.172s0.427%
magickwand_box0.190s0.575%
T-REZ0.204s0.323%
rez_bilinear0.349s0.140%X
x_image_draw0.370s0.160%X
imaging_box0.439s0.146%X
gift_box0.440s0.146%X
Nfnt_NearestNeighbor0.447s0.146%X
bild_resize0.515s0.206%X
ImageMagick_resize0.568s0.542%

So, what is to learn from that? While all of the currently existing pure-Go-language solutions do a pretty good job in generating good-looking thumbnails, they are much slower than the veteran dedicated image processing toolboxes. That is hardly surprising, given that both ImageMagick and GraphicsMagick have been around for decades and have been optimized to work as efficient as possible. Go and its image processing packages are still the new kids on the block, and while they work pretty neat for the occasional tweak of an image or two, I rather not use them as the default image processor in gonagall yet.

I was surprised to find that GraphicsMagick was slower than ImageMagick in my test scenario, as I expected it to be exactly the other way around with GraphicsMagick's fancy multi-processor algorithms.

While the imagick Wrapper is written in Go, it uses CGO bindings of the C MagickWand API. It outperforms the pure-Go approaches (five times faster than http://github.com/disintegration/imaging) but it still slower than calling ImageMagick in an external process. Of the above 1.13 seconds, only around 275 millisecs were used for resizing and saving an individual file, while over 850 ms were used by simply loading the file. I wonder how much optimization can still be done in the imagick loading routines.

Holy cow! vipsthumbnail is blazing fast.