Resize : negative or zero image size

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Baalrukh
Posts: 2
Joined: 2017-09-06T08:28:04-07:00
Authentication code: 1151

Resize : negative or zero image size

Post by Baalrukh »

I'm trying to resize a 1x14 image to 33% with the following command:
convert myImage.png -strip -resize 33% png32:output.png
but I get this error message :
convert: negative or zero image size `myImage.png' @ error/resize.c/ResizeImage/2897.
I tried both with ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 and 7.0.7-0 Q16 x86_64 2017-09-06

To give a little more context, the resize of the image is done while processing a batch of images, and I have no way to known beforehand that I may process a 1 pixel wide image.

Is there a way around this issue ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize : negative or zero image size

Post by fmw42 »

You can test the image size beforehand with a prior command and then do an if test.

Code: Select all

minsize=$(convert myImage.png -format "%[min(w,h)]" info:)
if [ $minsize -gt 1 ]; then
convert myImage.png -strip -resize 33% png32:output.png
done
In IM 7, you can probably do it all in one command line. For example this will ensure you have at least 1 pixel in width or height and the other dimension will be rounded to the 33% resize. Unix syntax

Code: Select all

resize=33
magick  myImage.png -strip \
-set option:wd "%[fx:(w==1)?1:round(w*$resize/100)]" \
-set option:ht "%[fx:(h==1)?1:round(h*$resize/100)]" \
-resize "%[wd]x%[ht]" png32:output.png
You can change the round to trunc or ceil or just leave it off and let IM do the rest.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Resize : negative or zero image size

Post by GeeMack »

Baalrukh wrote: 2017-09-06T08:39:36-07:00I'm trying to resize a 1x14 image to 33% with the following command:

[...]

To give a little more context, the resize of the image is done while processing a batch of images, and I have no way to known beforehand that I may process a 1 pixel wide image.

Is there a way around this issue ?
Using IM7 you can use something like this...

Code: Select all

magick myImage.png -strip -resize "%[fx:w*0.33>1?w*0.33:1]x%[fx:h*0.33>1?h*0.33:1]" png32:output.png
If 33% of the width or height is less than 1, it will resize it to 1. It won't attempt to resize it to a dimension less than one pixel, and you shouldn't get that error.

If you want it to resize one dimension even if the other dimension would be less than 1, put an exclamation point after the "-resize" dimensions like this...

Code: Select all

... -resize "%[fx:w*0.33>1?w*0.33:1]x%[fx:h*0.33>1?h*0.33:1]!" ...
The double quotes around the "-resize" dimensions are required to prevent Windows from interpreting the greater-than signs ">" as redirects. Also, in a Windows BAT script you'll need to change all the single percent signs "%" to doubles "%%".
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize : negative or zero image size

Post by snibgo »

Sometimes (eg "-trim") IM will merely warn that it can't make images with a dimension of 0, and makes that dimension 1.

Sometimes (eg "-resize") it refuses to do anything.

Consistency would be helpful.
snibgo's IM pages: im.snibgo.com
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Resize : negative or zero image size

Post by magick »

Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize : negative or zero image size

Post by fmw42 »

magick wrote: 2017-09-06T15:03:28-07:00 Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow.
What is the patch going to do? Limit -trim and -resize so that the smallest dimension will never be smaller than 1? Similarly for -thumbnail, -scale, -sample, -resample, etc?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Resize : negative or zero image size

Post by magick »

A geometry that includes the width or height with percent symbol appended (e.g. -resize 33%) now has a minimum value of 1. The patch does not affect offsets (e.g. 100%x100%+10-20). The patch may affect more than the just the -resize option.
Baalrukh
Posts: 2
Joined: 2017-09-06T08:28:04-07:00
Authentication code: 1151

Re: Resize : negative or zero image size

Post by Baalrukh »

Thank you for the quick fix, it works fine for me now
Post Reply