使用ubuntu上的转换使用填充调整图像大小 [英] Resizing the image with padding using convert on ubuntu

查看:127
本文介绍了使用ubuntu上的转换使用填充调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用convert命令调整图像大小

I am using the convert command for resizing the image

有两个版本

以下是第一个,结果图像保持纵横比但图像不一定是大小 nxn

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

第二个版本

 convert temp.jpg -resize nxn! temp.jpg

它不保留宽高比。

我想要的是保留纵横比,并用所需的RGB值填充图像的其余部分,以实现 nxn

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

任何想法?

谢谢

推荐答案

您需要使用 -extent 在调整大小后直接设置画布的大小,并且新创建的区域将填充您设置的任何内容 -background to。

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

如果您希望图像在输出图像的中间出现,则填充均匀分布在两侧附近,添加 -gravity center ,如下所示:

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

所以,如果我们从宽的蓝色图像开始,即300x100,并且无法在正方形中正确拟合,如下所示:

So, if we start with a wide blue image, that is 300x100 and has no chance of fitting properly in a square, as follows:

我们用此调整大小:

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

我们会得到这个

这篇关于使用ubuntu上的转换使用填充调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆