Resize image using firmware using conversion on ubuntu

I use the convert command to resize the image

There are two versions.

Below is the first, the resulting image supports aspect ratio, but the image does not necessarily have size nxn

  convert temp.jpg -resize nxn temp.jpg 

Second version

  convert temp.jpg -resize nxn! temp.jpg 

It does not maintain aspect ratio.

I want to keep the aspect ratio and fill the rest of the image with the desired RGB value to get the size nxn

Any ideas?

thanks

+8
linux image imagemagick image-resizing
source share
1 answer

You need to use -extent to set the canvas size immediately after you have resized, and the newly created area will be filled with what you set -background to.

So, if you want the padding to be magenta, do the following:

 convert image.png -resize 100x100 -background "rgb(255,0,255)" -extent 100x100 out.png 

If you want your image to appear β€œin the middle” of the output image, if you are evenly spaced around the sides, add -gravity center as follows:

 convert image.png -resize 100x100 -gravity center -background "rgb(255,0,255)" -extent 100x100 out.png 

So, if we start with a wide blue image, that is 300x100 and we have no chance to choose the right square, as shown below:

enter image description here

and we will resize it as follows:

 convert image.png -resize 100x100 -gravity center -background "rgb(255,0,255)" -extent 100x100 out.png 

we get it

enter image description here

+18
source share

All Articles