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

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: 10256
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 » 2017-01-25T07:40:38-07:00

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

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

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 » 2017-12-15T08:38:07-07:00

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

Thanks a lot,
P.

snibgo
Posts: 10256
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 » 2017-12-15T08:57:18-07:00

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

@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: 10256
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 » 2017-12-15T10:27:01-07:00

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

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

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 » 2017-12-16T09:11:29-07:00

Thank you :)

Post Reply