60

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!

Mike
  • 721
  • 1
  • 8
  • 6

7 Answers7

112

I used -extent to do this:

convert input.jpg -gravity center -background white -extent 50x50  output.jpg
Jared
  • 2,525
  • 2
  • 23
  • 30
  • 12
    as for me, gravity should be placed before `-extent`: `convert bg.png -gravity center -extent 640x960 -background white ../default@2x.png` – Cfr Sep 07 '12 at 13:41
  • 6
    ... and also `-background` has to be set before `-extent`. In above example it works as background is set white by default – theta Sep 30 '12 at 04:00
  • 4
    btw, if you want to add transparency use xc:transparent as the background parameter – janos Apr 28 '13 at 10:07
  • 1
    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. – billynoah Jun 01 '14 at 10:04
  • @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. – Jared Jun 02 '14 at 17:46
  • 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? – Adrian Jun 15 '15 at 19:20
  • 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. – Eric Gopak Aug 16 '20 at 15:29
9

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 ;).

gleenn
  • 618
  • 6
  • 15
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
Vinu vasudev
  • 109
  • 2
  • 8
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?

Adam Rosenfield
  • 360,316
  • 93
  • 484
  • 571
  • That probably isn't the solution to the problem, but +1 since it helped me with creating 9patch images. :) – unexist Aug 05 '13 at 13:02
3

I tried this:

convert test.jpg -resize 100x100 -background black -gravity center -extent 100x100 output.png
Nicolás Ozimica
  • 8,562
  • 5
  • 34
  • 49
Josh
  • 31
  • 2
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

Douglas Sellers
  • 612
  • 1
  • 7
  • 13
1

To modify the source image you need to use mogrify:

mogrify -gravity center -background white -extent 50x50  source.jpg
user1309871
  • 257
  • 3
  • 11