Speed Up Resize Operation?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
ecomp2016
Posts: 8
Joined: 2016-08-03T10:09:19-07:00
Authentication code: 1151

Speed Up Resize Operation?

Post by ecomp2016 »

Hi All,

Like my previous thread dealing with caption, is there a way for ImageMagick to resize an image more quickly and efficiently? Of all the measurements I've taken, the resize operation takes about 400MS which is longer than everything else being done in my app using ImageMagick.

I need to optimize for speed and efficiency.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Speed Up Resize Operation?

Post by Bonzo »

What code are you currently using, platform and language?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Speed Up Resize Operation?

Post by fmw42 »

Try -scale rather than -resize. Or add -filter point before -resize. Or use -thumbnail. If you have lots of meta data or profiles, the -thumbnail will remove them. You could also add -strip after reading your input and before -resize.
ecomp2016
Posts: 8
Joined: 2016-08-03T10:09:19-07:00
Authentication code: 1151

Re: Speed Up Resize Operation?

Post by ecomp2016 »

Scale appears to be several times faster and appears to produce similar results. Thanks fmw42.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Speed Up Resize Operation?

Post by snibgo »

"-sample" is even quicker, but may not give you acceptable results.
snibgo's IM pages: im.snibgo.com
ecomp2016
Posts: 8
Joined: 2016-08-03T10:09:19-07:00
Authentication code: 1151

Re: Speed Up Resize Operation?

Post by ecomp2016 »

Yeah, sample wasn't respecting the image aspect ratio? I am resizing 400 x 400 in my example, but the picture is not square. When I use resize or scale, the image is resized to a height of 400 and a width of 283 which is correct for its aspect. Sample does 400 x 400 without preserving the aspect?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Speed Up Resize Operation?

Post by snibgo »

"-sample" should give the same size output as "-resize" or "-scale", given the same input image and geometry argument.

What version IM are you using?
snibgo's IM pages: im.snibgo.com
ecomp2016
Posts: 8
Joined: 2016-08-03T10:09:19-07:00
Authentication code: 1151

Re: Speed Up Resize Operation?

Post by ecomp2016 »

You're right. It looks like a bug in the Magick.NET wrapper. When I run ImageMagick from the commandline sample does maintain the aspect ratio. I wonder how Magick.NET could even be overwriting this default sample behavior?

Sample doesn't produce great results, but man is it fast! It takes 6 milliseconds to sample resize. Scale takes 200MS, and Resize takes 500+ depending on the image. Is there anything between scale and sample? 200MS is still kind of a long time for this operation.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Speed Up Resize Operation?

Post by fmw42 »

Is there anything between scale and sample? 200MS is still kind of a long time for this operation.
Not that I know. You could try -filter point -thumbnail, but I suspect it will be longer than -scale.

What percent size reduction are you making?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Speed Up Resize Operation?

Post by snibgo »

Sorry, I don't use MagickNET. I use SampleImage() at the MagickCore level, which works fine.

Sampling is very fast because it doesn't calculate new colours for output pixels. It simply picks the colour of an input pixel. For some purposes, eg zooming in to an image to get "big pixels", or flat-colour graphics, it's the right tool.

I'm not aware of anything between scale and sample. There may be another way to increase performance, eg we sometimes resize then crop when it's faster to crop first and then resize.
snibgo's IM pages: im.snibgo.com
Post Reply