Resizing the image with padding using convert on ubuntu

谁说胖子不能爱 提交于 2019-12-20 17:38:26

问题


I am using the convert command for resizing the image

There are two versions

Following is the first one, the resultant image maintains the aspect ratio but the image does not necessarily be of size nxn

 convert temp.jpg -resize nxn temp.jpg

The second version

 convert temp.jpg -resize nxn! temp.jpg

It does not preserve the aspect ratio.

What I want is to preserve the aspect ratio, and fill the rest of the image with a desired RGB value to acheive the size nxn

Any ideas?

Thanks


回答1:


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

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

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, with the padding evenly spaced around the sides, add in -gravity center like this:

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 has no chance of fitting properly in a square, as follows:

and we resize it with this:

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

we will get this



来源:https://stackoverflow.com/questions/29073639/resizing-the-image-with-padding-using-convert-on-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!