简体   繁体   中英

Add lines of transparent pixels to image using command line

I have a few 15x15 PNGs that I need to convert to 18x18 PNGs.

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):

XXXX
XXXX
XXXX
XXXX

I want to turn it into something like this ( X being pixels of original image, T being new transparent pixels):

XXXXT
XXXXT
XXXXT
XXXXT
TTTTT

Is this possible using command-line tools (so that this can be automated), perhaps with something like imagemagick?

这将完成工作:

convert input_file -background transparent -extent '18x18' output_file

You can do that with ImageMagick's -append and +append operators. -append appends to the vertically (bottom), +append appends horizontally (right):

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.

But as usually is the case with IM: there are a thousand ways to reach the same goal.

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!). In this case, should you want to change the borders where you add your pixels, you can add the -gravity parameter to his command:

convert infile -background transparent -gravity southeast -extent '18x18' outfile

Other than south , you can also use north , northeast , east , ... and northwest . Not to forget center ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM