Change canvas size and fit original image inside it, preserving aspect ratio.

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
core_69
Posts: 3
Joined: 2017-01-25T06:17:18-07:00
Authentication code: 1151

Change canvas size and fit original image inside it, preserving aspect ratio.

Post by core_69 »

Hi,

It's my first post.

I've searched for this and found only "-border" and "-extend" options, but they don't give you the actual control over canvas size.
May be I need to use a combination of a some basic options.
So let's say my original image is 300x200 in size. What I need is a command-line, which will resize canvas to a given size and fit original image inside it, preserving aspect ratio.
So if I specify new canvas size 900x900 the original image should become 900x600 and be center-aligned inside 900x900 canvas.
Again if I specify canvas size to 500x100, the original image should become 150x100 and be center-aligned inside 500x100 canvas.

Image

Any help with this will be greatly appreciated!

Thank you developers for a great program!
Last edited by core_69 on 2017-01-25T08:04:38-07:00, edited 2 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by snibgo »

"-resize" does exactly what you ask for.
snibgo's IM pages: im.snibgo.com
core_69
Posts: 3
Joined: 2017-01-25T06:17:18-07:00
Authentication code: 1151

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by core_69 »

snibgo wrote: 2017-01-25T07:40:38-07:00 "-resize" does exactly what you ask for.
Sorry, I didn't described my question well, I'll edit it now.
Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by snibgo »

Okay. You need both "-resize" and "-extent", such as:

Code: Select all

convert in.png -resize 900x900 -background Black -gravity center -extent 900x900 out.png
snibgo's IM pages: im.snibgo.com
core_69
Posts: 3
Joined: 2017-01-25T06:17:18-07:00
Authentication code: 1151

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by core_69 »

snibgo wrote: 2017-01-25T08:14:59-07:00 Okay. You need both "-resize" and "-extent", such as:

Code: Select all

convert in.png -resize 900x900 -background Black -gravity center -extent 900x900 out.png
@snibgo
Works like a charm! You are my hero!!! :-D
pat
Posts: 4
Joined: 2017-12-15T08:32:45-07:00
Authentication code: 1152

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by pat »

Hey guys,
how can I proceed in a similar situation, but with SVG file?

Thanks a lot,
P.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by snibgo »

Do the same thing, but with the name of your SVG file.

If you want a better answer, you need to provide more details.
snibgo's IM pages: im.snibgo.com
pat
Posts: 4
Joined: 2017-12-15T08:32:45-07:00
Authentication code: 1152

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by pat »

@snibgo
Wow, thanks for such fast reply. I did the same thing, however, the result is blurred and looks like a bitmap.

Code: Select all

magick mogrify *.svg -resize '128x128' -gravity center -extent '128x128' *.svg
Takes such effect:
Image

I tried also:

Code: Select all

magick convert *.svg -resize 128x128 -gravity center -extent 128x128 *.svg
But it doesn't take any effect.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by snibgo »

First, I suggest you don't use "magick mogrify" or "magick convert". Just use "magick". This needs the input file, then do the processing, then write the output.

Secondly, when you read the SVG, it is rasterised (converted to pixels) at whatever density you have specified, if any. If you then enlarge those pixels, it will look bad. Better is to use a suitable density. For example:

Code: Select all

magick -density 300 in.svg out.png
Choose a suitable density number. Before the output, resize down if you want, and extent.
snibgo's IM pages: im.snibgo.com
pat
Posts: 4
Joined: 2017-12-15T08:32:45-07:00
Authentication code: 1152

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by pat »

Can you elaborate snibgo? Can't I just extend the SVG file to square & gravity to center? I don't want to convert my file to PNG yet.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by snibgo »

IM will convert the SVG vector file to pixels (a raster image). You can extend that etc if you want, but if you write the output to SVG, the SVG will contain an embedded raster image.

This is probably not what you want. IM is not a vector editor. For that, Inkscape is a better tool. SVG is a text format, so you can also edit it with a text editor, or the "sed" tool, or whatever.
snibgo's IM pages: im.snibgo.com
pat
Posts: 4
Joined: 2017-12-15T08:32:45-07:00
Authentication code: 1152

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by pat »

Thank you :)
Dward0
Posts: 1
Joined: 2019-08-22T13:26:41-07:00
Authentication code: 1152

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by Dward0 »

This is great! How would I resize the canvas (preserving the ratio) for a few hundred images in a folder?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change canvas size and fit original image inside it, preserving aspect ratio.

Post by snibgo »

Use mogrify (see http://www.imagemagick.org/script/mogrify.php ) or write a shell script.
snibgo's IM pages: im.snibgo.com
Post Reply