convert doesn't conserve size when scaling down and up

Viewed 35

In order to make applying blur faster I'm first scaling my image down and then scale it back up:

convert - -scale 10% -blur 0x2.5 -resize 1000% RGB:-

This works most of the time but sometimes the output resolution is slightly different from the original input. Is there a way to force the pipeline to be size-preserving?

2 Answers

You should be able to access the original geometry of the image via %G, so you can do:

convert input.jpg -scale 10% -blur 0x2.5 -resize '%G!' RGB:-

If you are using Windows, you probably want "%G!" in double rather than single quotes.

If you are using v7 ImageMagick, replace convert with magick.


I think you are getting errors because if you take 10% of 72 pixels (say), you will get a whole number of pixels, i.e. 7 pixels and then when you scale back up by a factor of 10 you'll get 70 rather than your initial 72.

If you are using Imagemagick 7, you can do the following:

magick input.jpg -set option:wd "%w" -set option:ht "%h" -scale "%[fx:wd/10]x%[fx:ht/10]" -blur 0x2.5 -resize "%[fx:wd]x%[fx:ht]\!" RGB:-

This stores the input width and height. Then uses the stored values to scale by 1/10 of those dimensions, then does the blur, then resizes exactly back to the origin input size. Note the ! that forces the resize to the exact dimensions.

or simpler without storing the input width and height:

magick lena.jpg -scale "%[fx:w/10]x%[fx:h/10]" -blur 0x2.5 -resize "%[fx:w]x%[fx:h]\!" lena_x.jpg
Related