Add lines of transparent pixels to image using command line - macos

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?

This will do the job:
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...

Related

Application/script for merging images, applying color, black & white filter in batch?

I'm making achievements for my game, and I would like to make an automated process to save a lot of time instead of doing it manually. (I'm using Windows)
The inputs of this process would be
one rectangular opaque background image (i.e. 200x200 PNG)
Color A, (i.e. in hex)
Color B, (i.e. in hex)
and multiple same sized, rectangular, transparent foreground images. (i.e. 200x200 PNG)
And the process would do this for every foreground image:
Start with the background image.
Merge the foreground image with Color A filter applied, and also with an offset from the middle.
Merge the foreground image with Color B filter applied, in the middle.
Save the result in a file.
Apply a black & white filter and save the result in a file.
So the output would be for example multiple 200x200 PNG images, each of it having a colored and a black & white version
I think there must be an application capable of this.
But if not, is there a way to quickly do this in some programming/script language for example in Python?
Here's the best you can hope for without providing any images or expected results. I am using the following as background, image A and image B:
Then using ImageMagick, like this:
magick background.png -gravity center \
\( a.png -fill "#ff00ff" -tint 70% -resize 64x64 -geometry -30-40 \) -composite \
\( b.png -fill "#00ff00" -tint 85% -resize 100x100 -geometry +0+0 \) -composite \
-write coloured.png \
-colorspace gray grey.png
You'll get:
If using Windows, you'll need to:
replace any \ at end of line with caret ^
remove any \ before opening and closing parentheses ( and )
change any single quotes to double quotes
mess around with any % signs using some arcane Microsoft rules about doubling them up to make sure it really does what you asked it in batch files.

Imagemagick - How to create an annotation that would fit its size into any image?

I need to write a bash script that would annotate a given image using imagemagick. The problem is, that image could be any size and its annotation should look nearly same on any image.
Output result should look like this on any image:
So there are some points I'd like to figure out:
How to draw a transparent rectangle with a text, which will adjust its size depending on image size?
How to place this rectangle at the bottom of an image like in the example?
Using ImageMagick version 6 or 7 you can make a label sized to fit any input image, with a semi-transparent background, and composite it at the bottom of the input image to get the result you describe. Here is a command with IM 6 that does it...
convert input.png -set option:size %[w]x \
-fill white -background "#00000080" \
\( label:"This is my text." \
-virtual-pixel background -distort SRT "0.8 0" \
-virtual-pixel none -distort SRT "0.8 0" \) \
-gravity south -composite result.png
That uses the width of the input image "%[w]" to set the width of the label. It sets the text color to white and the background to semi-transparent black, "#00000080".
Inside the parentheses it creates your label. It uses "distort SRT" to scale the label down a bit to pull it in from the edges. Then it scales the label down a bit more to add some transparent space around it.
After the label is created it sets the gravity to "south" and composites the label onto the input image. It finishes by writing the output file.
Using IM 7 you'll need to change "convert" to "magick". For Windows change the continued line backslashes "\" to carets "^" and get rid of the backslashes that escape the parentheses.
Edited to add: Normally you'd use "-size WxH" ahead of making a "label:" to constrain it within particular dimensions. I used "-set option:size" instead because it allows for using percent escapes like "%[w]" with IM 6. That way the label dimensions are relative to any input image width.

Center text vertically but not horizontally in specified box imagemagick

I'm trying to generate image from text. Requirements are: text left aligned, vertically centered, big as possible, max. resolution 1920x1080.
This is what I have:
convert -background white -fill black \
-font "fonts/DejaVuSansMono.ttf" \
-size 1920x1080 label:'Text \nloooooooooooooooooooooooooong text\nand another' \
-gravity West image.png
It works pretty well, but it doesn't center it to vertically.
Looks like this:
https://imgur.com/rJLxJO2.png
But I would like it to look like this: https://imgur.com/IVrydty.png
I could use -size x1080 and put that image into center of a blank white image using second command, but that doesn't make sure it is not too wide.
Try setting the gravity before creating the label....
convert -background white -fill black -size 1920x1080 \
-gravity west \
label:'Text \nloooooooooooooooooooooooooong text\nand another' \
image.png
Edited to add: When a setting is supposed to affect a particular operation, in almost every case with ImageMagick the setting should come ahead of the operation. IM version 6 is somewhat forgiving in that regard, but as with your example, often the order of the command is important. IM version 7 is much more strict.

Making half an image transparent from the command line

I don't really know where to start with this one. I'm trying to do something that I thought would be relatively simple to accomplish with imagemagick, but I don't know the exact command to start with. I need to draw a line through an image, and then make everything above the line transparent in the image, and make everything below the line, the orignal image. What would be the best way to accomplish this using imagemagick?
So what I've come up with for now is to crop the image, and then resize it to the original size, but with a transparent background. The command I use is this, but it always comes out black. I'm not understanding why.
convert -background none -gravity south out.png -resize 400x200 -extent 400x400 result.png
Thanks for all of the help!
Here's a fairly easy way to do it. First, enable an alpha channel in case your image doesn't have one, then select the alpha channel for modification by the following -fx command. In there, if the current j is greater than half the height of the image, make the alpha layer opaque, else transparent. Easier than it sounds!
So, using this start image:
convert bean.jpg -alpha on -channel A -fx "j>h/2?1:0" result.gif
Or, the other way:
convert bean.jpg -alpha on -channel A -fx "j<h/2?1:0" result.gif
Or the other, other way:
convert bean.jpg -alpha on -channel A -fx "i<w/2?1:0" result.gif
Or, if you are feeling particularly silly on a Friday morning...
convert bean.jpg -alpha on -channel A -fx "hypot(i,j)/400-0.8" result.gif

Size variation in a Imagemagick montage

I try to have random space between my images in an Imagemagick montage.
My original script is:
montage images/* -mode Concatenate -tile 12x -geometry +10+10 -background white tile.png
…and I need something like:
montage images/* -mode Concatenate -tile 12x -geometry +($RANDOM%200+0)+($RANDOM%200+0) -background white tile.png
Is this possible?
The -geometry is only evaluated once at the start, so I don't think your intended approach is going to work. You can either grab the 12 images and total up their widths plus 11 randomly sized gaps and then create a canvas big enough to lay your images out on, or make a bunch of spacer blocks to move your images right and down by random amounts.
So, this is a rough starting point of how you might build something up maybe... I know you don't want the background yellow, but I am setting it that way so you can see what I am doing. I am basically laying out a red, a green and a blue image in a horizontal strip, but adding randomly sized spacer blocks in and I have coloured these in varying shades of grey so you can see them.
convert -background yellow \
red.png xc:gray20[100x100] \
\( xc:gray40[80x80] green.png -append \) \
xc:gray60[25x25] \
\( xc:gray80[200x200] blue.png -append \) \
+append tile.png
Each time an image and a spacer appear together in parentheses, that is basically a spacer ABOVE the image with the image itself -appended BELOW. Each time a spacer occurs on its own, that is basically to the right of the preceding image, and at the end, the +append joins all the blocks up side-by-side.
To understand what I am doing, you need to appreciate that -append places images top-to-bottom, one above the other and that +append places images side-by-side next to each other.
I am suggesting you create a script to build the command up.
Basically, you could make a strip like this, then another one of 12 and use -append to stick them together. Obvioulsy you want to make the background, and all the spacers, white.

Resources