[英]Add lines of transparent pixels to image using command line
I have a few 15x15 PNGs that I need to convert to 18x18 PNGs. 我有几个15x15的PNG,我需要转换为18x18的PNG。
However, I don't want to simply scale the entire image up. 但是,我不想简单地缩放整个图像。
What I want is basically keep the exact old image, but "pad" it with lines of invisible/transparent pixels. 我想要的基本上是保留精确的旧图像,但用不可见/透明像素线“填充”它。
So let's say the original image looked like this (imagine the X
s are pixels): 所以让我们说原始图像看起来像这样(想象X
是像素):
XXXX
XXXX
XXXX
XXXX
I want to turn it into something like this ( X
being pixels of original image, T
being new transparent pixels): 我想把它变成这样的东西( X
是原始图像的像素, T
是新的透明像素):
XXXXT
XXXXT
XXXXT
XXXXT
TTTTT
Is this possible using command-line tools (so that this can be automated), perhaps with something like imagemagick? 这可能是使用命令行工具(这样可以自动化),也许像imagemagick?
这将完成工作:
convert input_file -background transparent -extent '18x18' output_file
You can do that with ImageMagick's -append
and +append
operators. 您可以使用ImageMagick的-append
和+append
运算符来实现。 -append
appends to the vertically (bottom), +append
appends horizontally (right): -append
追加到垂直(底部), +append
追加到水平(右):
convert \
-background transparent \
15x15.png \
null: null: null: +append \
null: null: null: -append \
18x18.png
The null:
picture is an ImageMagick built-in: it represents a 1 transparent pixel. null:
图片是内置的ImageMagick:它代表1个透明像素。
But as usually is the case with IM: there are a thousand ways to reach the same goal. 但正如IM的情况一样:有一千种方法可以达到同一目标。
Should you ever need to add the transparent parts to the left and top (instead of the right and bottom), or should you want to add the pixels to just one, or to three or to four (instead of only two edges, it should be obvious to you how to modify my command. 您是否需要将透明部分添加到左侧和顶部(而不是右侧和底部),或者您是否要将像素添加到一个,或三个或四个(而不是仅两个边缘,它应该很明显你如何修改我的命令。
Maybe you find Theodros' answer more intuitive (+1 on for it!). 也许你会发现Theodros的答案更直观(+1就可以了!)。 In this case, should you want to change the borders where you add your pixels, you can add the -gravity
parameter to his command: 在这种情况下,如果要更改添加像素的边框,可以将-gravity
参数添加到其命令中:
convert infile -background transparent -gravity southeast -extent '18x18' outfile
Other than south
, you can also use north
, northeast
, east
, ... and northwest
. 除了south
,您还可以使用north
, northeast
, east
,......和northwest
。 Not to forget center
... 不要忘记center
......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.