Use ImageMagick to place an image inside a larger canvas

前端 未结 7 689
萌比男神i
萌比男神i 2021-01-30 02:46

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 horizo

相关标签:
7条回答
  • 2021-01-30 03:31

    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

    0 讨论(0)
  • 2021-01-30 03:32

    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?

    0 讨论(0)
  • 2021-01-30 03:35

    To modify the source image you need to use mogrify:

    mogrify -gravity center -background white -extent 50x50  source.jpg
    
    0 讨论(0)
  • 2021-01-30 03:36

    I used -extent to do this:

    convert input.jpg -gravity center -background white -extent 50x50  output.jpg
    
    0 讨论(0)
  • 2021-01-30 03:42

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

    0 讨论(0)
  • 2021-01-30 03:42

    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
    
    0 讨论(0)
提交回复
热议问题