Change canvas size and fit original image inside it, preserving aspect ratio.
Change canvas size and fit original image inside it, preserving aspect ratio.
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.
Any help with this will be greatly appreciated!
Thank you developers for a great program!
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.
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.
-
- 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.
"-resize" does exactly what you ask for.
snibgo's IM pages: im.snibgo.com
-
- 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.
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
Re: Change canvas size and fit original image inside it, preserving aspect ratio.
@snibgosnibgo 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
Works like a charm! You are my hero!!!
Re: Change canvas size and fit original image inside it, preserving aspect ratio.
Hey guys,
how can I proceed in a similar situation, but with SVG file?
Thanks a lot,
P.
how can I proceed in a similar situation, but with SVG file?
Thanks a lot,
P.
-
- 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.
Do the same thing, but with the name of your SVG file.
If you want a better answer, you need to provide more details.
If you want a better answer, you need to provide more details.
snibgo's IM pages: im.snibgo.com
Re: Change canvas size and fit original image inside it, preserving aspect ratio.
@snibgo
Wow, thanks for such fast reply. I did the same thing, however, the result is blurred and looks like a bitmap.
Takes such effect:
I tried also:
But it doesn't take any effect.
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
I tried also:
Code: Select all
magick convert *.svg -resize 128x128 -gravity center -extent 128x128 *.svg
-
- 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.
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:
Choose a suitable density number. Before the output, resize down if you want, and extent.
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
snibgo's IM pages: im.snibgo.com
Re: Change canvas size and fit original image inside it, preserving aspect ratio.
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.
-
- 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.
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.
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
Re: Change canvas size and fit original image inside it, preserving aspect ratio.
This is great! How would I resize the canvas (preserving the ratio) for a few hundred images in a folder?
-
- 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.
Use mogrify (see http://www.imagemagick.org/script/mogrify.php ) or write a shell script.
snibgo's IM pages: im.snibgo.com