Use ImageMagick to place an image inside a larger canvas
Asked Answered
I

8

74

Getting started with ImageMagic and trying to find a way to do this... If an image is less than 50 pixels tall or 50 pixels wide, I'd like to place it (un-scaled) in the horizontal/vertical center of a new 50x50 pixel canvas on top of a white background - and save that as the new image. Anyone know if this is possible with ImageMagick? Thanks!

Impletion answered 24/11, 2009 at 2:10 Comment(1)
Which API are you using?Showcase
B
131

I used -extent to do this:

convert input.jpg -gravity center -background white -extent 50x50  output.jpg
Bayou answered 23/9, 2010 at 19:34 Comment(7)
as for me, gravity should be placed before -extent: convert bg.png -gravity center -extent 640x960 -background white ../[email protected]Dissemble
... and also -background has to be set before -extent. In above example it works as background is set white by defaultIquique
btw, if you want to add transparency use xc:transparent as the background parameterFlocculant
Can anyone explain how to do this with php? I tried this exact sequence as far as I can understand and my image always ends up on on the far left and top, not centered.Everest
@billynoah I don't know since I don't use php, but this seems like a separate question, why don't you post a new question for that.Bayou
I did this, and it does work, but my image I want to resize has a transparent background and I want to set the newly added background to another colour (not modifying the already transparent background). Any idea how I could accomplish that?Flesh
If you get an error saying something like color.xml not found, you can still use #RRGGBB color format instead of color names, like #000000 for black or #FFFFFF for white, etc.Bannister
P
10

I wanted to do the same, except shrink the image to 70% inside. I used this:

convert input.png -resize 70%x70% -gravity center -background transparent -extent 72x72 output.png

Not exactly what was requested but hopefully it will help someone ;).

Pearle answered 17/11, 2014 at 11:7 Comment(1)
That shrinks the image to 70% of its own size, then places it on the larger canvas. That may be what you want. But it does not shrink the image to 70% of the size of the canvas.Desired
B
6

I have once used this code to place an image in the center of a new canvas with white background. hope this will help you

convert -background white -gravity center your_image.jpg -extent 50x50 new_image.jpg
Blaineblainey answered 14/6, 2016 at 6:39 Comment(0)
L
4

See cutting and bordering for a huge number of examples. Here's one simple way you might do it:

convert input.png -bordercolor Black -border 5x5 output.png

Of course, you'll need to calculate the size of the border to add (if any) based on the dimensions of the input image. Are you using an ImageMagick API, or just the command line tools?

Lita answered 24/11, 2009 at 2:25 Comment(1)
That probably isn't the solution to the problem, but +1 since it helped me with creating 9patch images. :)Phelgon
B
3

I tried this:

convert test.jpg -resize 100x100 -background black -gravity center -extent 100x100 output.png
Bran answered 2/5, 2010 at 22:12 Comment(0)
S
2

You can use single composition to do this. So it would look something like this:

convert -size 50x50 xc:white null: ( my_image.png -coalesce ) -gravity Center -layers Composite -layers Optimize output.png

Solarism answered 1/2, 2010 at 9:48 Comment(0)
F
1

To modify the source image you need to use mogrify:

mogrify -gravity center -background white -extent 50x50  source.jpg
Feign answered 6/4, 2013 at 7:18 Comment(0)
B
1

If an image is less than 50 pixels tall or 50 pixels wide

In my case, the images were much larger than the destination canvas, and weren't square. So I resize them proportionally to fit inside. Example:

convert in.png -resize 46x46 -background none -gravity center -extent 50x50 out.png

The 46x46 limit ensures a 2 pixel margin minimum. Note that the above does not distort the image, e.g. a rectangle does not become a square.

I used background none for a transparent background, but you can choose a solid color instead.

Byproduct answered 6/7, 2022 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.