Page 1 of 1

How to force resize an image (convert command)

Posted: 2012-06-29T16:45:01-07:00
by hakermania
Hello again. Second topic same day, but I want to finish with this image conversion thingy I'm doing.

I am using this command:

Code: Select all

convert -quality 100 -resize 500x500 "input.jpg" "output.png"
If I've understood correctly, the '-resize' argument takes as its own argument: 'width'x'height'

So, having this in mind, shouldn't the output.png image have height 500px and width 500px as well?
My generated output.png image, however, seems to have 500x313 (!)
It seems that somehow the convert command respects the original's image division: IMAGE_WIDTH/IMAGE_HEIGHT or something like this...

How do I force it not to?

Thanks in advance for any answers!

Re: How to force resize an image (convert command)

Posted: 2012-06-29T18:54:38-07:00
by fmw42
First your syntax is not correct, but should still work under IM 6. It is the IM 5 syntax.

Correct syntax would be

convert "input.jpg" -resize 500x500 -quality 100 "output.png"

The issue you are having is that IM will not change the aspect ratio, so that it normally resizes to match the larger dimension and keeps the smaller dimension in proportion.

Your choices are to 1) center crop to the smaller dimension 2) pad to the larger dimension with the image in the center or 3) force IM to changed the aspect ratio which will distort the image. For the latter use

convert "input.jpg" -resize 500x500! -quality 100 "output.png"
or
convert "input.jpg" -resize 500x500\! -quality 100 "output.png"
or
convert "input.jpg" -resize "500x500!" -quality 100 "output.png"

whichever works on your system.

For the other two options, see

http://www.imagemagick.org/Usage/thumbnails/#cut
http://www.imagemagick.org/Usage/thumbnails/#pad

and
http://www.imagemagick.org/script/comma ... p#geometry

Re: How to force resize an image (convert command)

Posted: 2012-06-30T00:53:49-07:00
by hakermania
Thanks! It worked perfectly! :D
The different approaches of ! (\! "! !) have to do with whether I'm using bash, sh, ksh etc?

Re: How to force resize an image (convert command)

Posted: 2012-07-04T18:39:36-07:00
by anthony
hakermania wrote:Thanks! It worked perfectly! :D
The different approaches of ! (\! "! !) have to do with whether I'm using bash, sh, ksh etc?
As long as the '!' is actually passed to ImageMagick, it can see and act on it.

In BASH '!' needs quoting from command line, but does not actually need it from a script. However escaping the character does not hurt in BASH, is safer, and avoids any 'do I need it' confusion. So just always do it. Bash itself uses '!' for "History Substitution" from previous typed commands, which is why it only effect 'typed in' commands from in interactive source.

ASIDE.....
Same story goes for other 'meta-charcters' like '*' '?' '>' '<' '\' and so on, including quotes and almost any other punctuation, when you want that character actually passed to ImageMagick for processing.

NOTE: IM will also expand '*' and '?' in arguments IF they match a filename on disk. IMv6 did this expansion to ALL arguments (not just read filenames). IMv7 has limited this to just input filenames to avoid possible confusion, and I will be looking for a security option to allow you to turn of some filename processing aspects (so IM can more easily handle literial filenames)