Centered on a black background

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
strantheman

Centered on a black background

Post by strantheman »

I did some extensive searching of these forums, but did not find anyone having trouble with -gravity centering as I am. Seems like a relatively simple and common task, and so I'm stumped as to why it is not working for me. I found a good resource that explains exactly how to do it, but it isn't working.

http://www.imagemagick.org/Usage/thumbnails/#pad

I'm using the command to resize an image to 50px wide, and then to center it on a black 75x75 background. Following the example from the above resource, this is my command:

Code: Select all

convert -size 200x200 tall.jpg -resize 50 -background black -gravity center -extent 75x75 tall_pad.jpg
What I end up with is a 75x75 image, but the original image is not centered, it is left justified.

Any help would be greatly appreciated. I really want to understand this very powerful tool.

nic
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Centered on a black background

Post by Bonzo »

Try this:

Code: Select all

convert -size 75x75 xc:black -size 200x200 tall.jpg -resize 50 -gravity center -composite tall_pad.jpg
-size 75x75 xc:black = Create the black background
-size 200x200 tall.jpg -resize 50 = Resize the image
-gravity center -composite = Put the second image ontop of the first
strantheman

Re: Centered on a black background

Post by strantheman »

Hi Bonzo! Thanks for your assistance, bro.

Your xc:black command was news to me; hadn't seen that before. Plus, your -composite command was probably what I was missing! I altered the -resize this time to have it constrain within 75x75 (I have some wide thumbs, and some tall) and I got exactly what I wanted! Here is the resulting command:

Code: Select all

convert -size 75x75 xc:black tall.jpg -resize 75x75 -gravity center -composite tall_pad.jpg
It works for both my tall graphics, centering them horizontally, and for my wide ones, centering them vertically.

Your help is much appreciated. My client indirectly thanks you!

p.s. I removed the -size 200x200 part, i've never been quite sure what that was supposed to do.

nic
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Centered on a black background

Post by Bonzo »

I am glad you got what you wanted nic.

The -size 200x200 will speed up the resizing of jpgs. I do not know how much it speeds it up and it can be left out.
I use php and put the original size of the image in there using getimagesize( ).
strantheman

Re: Centered on a black background

Post by strantheman »

Cool, thanks for the tip. Faster is faster; I do have that data in my database as well, so I might as well use it. :)

ill be around, see ya

nic
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Centered on a black background

Post by anthony »

More than likely the reason the first 'primary' IM examples solution failed is you have an old version of IM examples.
Bonzo's solution is equivalent to the fourth method given. Overlaying the thumbnail on an existing image. In this case the -size 200x200 xc:black.

In short I suggest you read the IM Examples, on Canvas creation which covers this type of thing.

I also suggest you look at 'Cutting and Bordering'...
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply