Page 1 of 1

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

Posted: 2017-01-25T07:19:05-07:00
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!

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

Posted: 2017-01-25T07:40:38-07:00
by snibgo
"-resize" does exactly what you ask for.

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

Posted: 2017-01-25T07:49:51-07:00
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

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

Posted: 2017-01-25T08:14:59-07:00
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

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

Posted: 2017-01-25T08:44:18-07:00
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

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

Posted: 2017-12-15T08:38:07-07:00
by pat
Hey guys,
how can I proceed in a similar situation, but with SVG file?

Thanks a lot,
P.

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

Posted: 2017-12-15T08:57:18-07:00
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.

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

Posted: 2017-12-15T09:01:53-07:00
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.

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

Posted: 2017-12-15T10:27:01-07:00
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.

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

Posted: 2017-12-16T02:26:41-07:00
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.

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

Posted: 2017-12-16T08:33:53-07:00
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.

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

Posted: 2017-12-16T09:11:29-07:00
by pat
Thank you :)

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

Posted: 2019-08-22T13:31:33-07:00
by Dward0
This is great! How would I resize the canvas (preserving the ratio) for a few hundred images in a folder?

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

Posted: 2019-08-22T13:50:16-07:00
by snibgo
Use mogrify (see http://www.imagemagick.org/script/mogrify.php ) or write a shell script.