Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Xs 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?

like image 424
houbysoft Avatar asked Jan 15 '23 21:01

houbysoft


2 Answers

This will do the job:

convert input_file -background transparent -extent '18x18' output_file
like image 142
tzelleke Avatar answered Jan 18 '23 12:01

tzelleke


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...

like image 24
Kurt Pfeifle Avatar answered Jan 18 '23 12:01

Kurt Pfeifle