Convert: centering a "best fit" resized image

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
JoshP
Posts: 1
Joined: 2011-09-26T17:07:47-07:00
Authentication code: 8675308

Convert: centering a "best fit" resized image

Post by JoshP »

I'm trying to resize a picture and put it on top of a black background using this command
Let's say somePic.jpg does not fit the ratio of 300x100, I would expect it to be centered within the slot allocated to it, but with the this command its left aligned.

Code: Select all

convert -size 300x250 -quality 80.0 xc:black \( -gravity Center 'somePic.jpg' -resize 300x100 -repage 0x0+0+110 \) -flatten 'newPic.jpg'
I've had luck doing this with this style command:

Code: Select all

convert "somePic.jpg" -resize 144x82 -size 144x82 xc:#343434 +swap -gravity center -composite "newPic.jpg"
Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert: centering a "best fit" resized image

Post by fmw42 »

try

convert somePic.jpg -resize 144x82 -background black -gravity center -extent 144x82 newpic.jpg

see
http://www.imagemagick.org/Usage/crop/#extent
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Convert: centering a "best fit" resized image

Post by anthony »

There are two composition positioning methods in IM
  • geometry offsets with gravity placement typically two images only
  • layering of multiple images with canvas size, and page offsets (relative to a origin)
which method is used by what composition operator depends on that operator. Only one operator understands both and that is -layer composite, which uses geometry/gravity one for global placement, and layer/page for individual placement relative to global placement.

See IM examples, Compositing Images, Positioning The Overlay Image
http://www.imagemagick.org/Usage/compose/#geometry

The section immediatally above lists the composition operators and what positioning methods they use (if any)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply