Page 1 of 1

Resize : negative or zero image size

Posted: 2017-09-06T08:39:36-07:00
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 ?

Re: Resize : negative or zero image size

Posted: 2017-09-06T10:13:26-07:00
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.

Re: Resize : negative or zero image size

Posted: 2017-09-06T11:43:12-07:00
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 "%%".

Re: Resize : negative or zero image size

Posted: 2017-09-06T12:50:20-07:00
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.

Re: Resize : negative or zero image size

Posted: 2017-09-06T15:03:28-07:00
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.

Re: Resize : negative or zero image size

Posted: 2017-09-06T15:17:19-07:00
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?

Re: Resize : negative or zero image size

Posted: 2017-09-07T05:41:30-07:00
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.

Re: Resize : negative or zero image size

Posted: 2017-09-08T02:19:18-07:00
by Baalrukh
Thank you for the quick fix, it works fine for me now