Page 1 of 1

Crop to fill on a canvas with different aspect ratio

Posted: 2013-06-27T08:31:58-07:00
by holden
I've been struggling to find a nice simple way to do this- IM will fit an image onto a canvas of a different aspect ratio leaving white space, which is something I need to do at certain times, but I also need to do this: http://www.pasteall.org/pic/54347
I just can't find a non-hacky or consistent way to do it- I feel as though I am missing a powerful command somewhere. The image needs to be centered on the canvas as I tried to show in the link. Thanks

Re: Crop to fill on a canvas with different aspect ratio

Posted: 2013-06-27T09:15:16-07:00
by snibgo
Suppose the target width is %W% and the target height is %H%. This (Windows) script does what I think you want:

Code: Select all

convert in.png ^
  -resize "%W%x%H%^^" ^
  -gravity Center ^
  -crop %W%x%H%+0+0 ^
  out.png

Re: Crop to fill on a canvas with different aspect ratio

Posted: 2013-06-27T09:39:21-07:00
by holden
Creating my diagram got me thinking- so far this seems to work also:

Code: Select all

-size 2100x1500 xc:white ( foo.jpg -resize 2100x -gravity center +repage ) -composite out.jpg
Does this look like a proper solution? (Pixel sizes are just an example)

Re: Crop to fill on a canvas with different aspect ratio

Posted: 2013-06-27T09:58:59-07:00
by fmw42
-size 2100x1500 xc:white ( foo.jpg -resize 2100x -gravity center +repage ) -composite out.jpg
I would put the -gravity outside the parens and I don't think you need +repage, especially since jpg does not support virtual canvas and resize does not create one.

convert -size 2100x1500 xc:white ( foo.jpg -resize 2100x ) -gravity center -composite out.jpg

Re: Crop to fill on a canvas with different aspect ratio

Posted: 2013-06-27T10:06:22-07:00
by snibgo
Yup, that seems to do the same thing. Loads of ways to skin a cat.

(I should probably have had "+repage" before out.png.)