Resizing image to a dimension

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
osnsubra
Posts: 7
Joined: 2014-08-28T10:22:50-07:00
Authentication code: 6789

Resizing image to a dimension

Post by osnsubra »

Hi, We have our images in PNG format with 750x1050 dimension. We want to resize the images to a dimension 180x340, and the following command is not converting to the exact dimension, rather its covnerting to 180x250.

Command used: convert -resize 180x340

We have also used various options like : –trim –geometry 180x340! -resize 180x340, which is stretching the image.

Pls let me know the right command to convert the image to this desired format. Appreciate your response.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing image to a dimension

Post by snibgo »

convert in.png -resize 180x340! out.png

See http://imagemagick.org/script/command-l ... php#resize , and the link to Image Geometry.
snibgo's IM pages: im.snibgo.com
osnsubra
Posts: 7
Joined: 2014-08-28T10:22:50-07:00
Authentication code: 6789

Re: Resizing image to a dimension

Post by osnsubra »

I have tried the following command., and its stretching the image to vertical. Basically its a Wine bottle image, i thought of attaching here but there is no option to attach image in this forum.

convert in.png -resize 180x340! out.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing image to a dimension

Post by snibgo »

Well, that's what you said you wanted.
We want to resize the images to a dimension 180x340, ...
You can upload files to somewhere like dropbox.com and paste the URL here.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing image to a dimension

Post by fmw42 »

180x340!
Using the ! tells IM to ensure that the size is exactly as specified even if it distort the image. If you want no distortion and aspect ratio preserved, either leave it off and pad or use ^ and crop or extent. See

http://www.imagemagick.org/script/comma ... p#geometry
http://www.imagemagick.org/Usage/thumbnails/#pad
http://www.imagemagick.org/Usage/thumbnails/#cut

see various options at
http://www.imagemagick.org/Usage/crop/
http://www.imagemagick.org/Usage/thumbnails/#creation

You can use -resize rather than -thumbnail
osnsubra
Posts: 7
Joined: 2014-08-28T10:22:50-07:00
Authentication code: 6789

Re: Resizing image to a dimension

Post by osnsubra »

Thanks for the response.
I have uploaded the original image with dimension 750x1050 in the following location -
https://www.dropbox.com/s/anjp9o6qbqeoi ... n.png?dl=0

The desired dimension 180x340, converted using Photoshop is uploaded here -
https://www.dropbox.com/s/0x0ncyjtkzsnt ... 0.png?dl=0

The dimension 180x340, converted using Imagemagick with command 'convert -resize 180x340!' uploaded here
https://www.dropbox.com/s/uheezikwl3fi3 ... 0.png?dl=0

The imagemagick converted image is stretched as its ignored the aspect ratio. I am looking for the right command that converts to desired format 180x340 with no distortion to the image. Thanks for your help
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing image to a dimension

Post by fmw42 »

Your original image has an aspect ratio of 750/1050=0.714285714285714. You want an image with aspect ratio of 180/340=0.529411764705882. These are two very different aspect ratios. There is no way to do that without distorting, padding or cropping. The best I can suggest is

Code: Select all

convert OriginalDimension.png -resize 180x340 -gravity center -background none -extent 180x340 im_padded.png
or

Code: Select all

convert OriginalDimension.png -resize 180x340^ -gravity center -background none -extent 180x340 im_cropped.png
The latter seems to match the PS result, but may not be what you want in all cases, since it may crop parts of the object and not just background as it does in this case.
osnsubra
Posts: 7
Joined: 2014-08-28T10:22:50-07:00
Authentication code: 6789

Re: Resizing image to a dimension

Post by osnsubra »

Thanks for the response.
On the 2nd command, is there a way it can be zoomed in, so that the size of the bottle can be little bigger. Below are the image links

The im_cropped.png
https://www.dropbox.com/s/5moln3h236max ... d.png?dl=0

PS converted image
https://www.dropbox.com/s/0x0ncyjtkzsnt ... 0.png?dl=0
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing image to a dimension

Post by fmw42 »

Try

Code: Select all

convert OriginalDimension.png -resize 180x340^ -resize X% -gravity center -background none -extent 180x340 im_cropped.png
where X is slightly larger than 100 as appropriate for the desired zoom.

Or just multiply 180 and 340 by X as a fraction (slightly larger than 1) and use those as

Code: Select all

convert OriginalDimension.png -resize WxH^ -gravity center -background none -extent 180x340 im_cropped.png
where W=180*X and H=340*X
Post Reply