Center text vertically but not horizontally in specified box imagemagick - bash

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.

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.

Merging two transparent gifs as layers

I've found this page on the imagemagick forum: Merging 2 gifs, one is animated and transparent which links to the imagemagick docs on Animation Modification; specifically, the example here:
convert canvas_prev.gif -coalesce \
-gravity NorthEast -draw 'image over 5,5 0,0 "rose:"' \
-layers Optimize draw_over.gif
Here's my attempt. I have these two gifs (i might be feeling a little morbid). The white in the first is actually transparent.
when I run
convert eyes.gif -coalesce -draw ' image over 0,0 0,0 "trump.gif" ' combine.gif
I get this:
which is not the complete effect I desire. The trump animation is no longer playing at all.
I want to see something more like this (this is created in Phaser JS, but this gives me no way to export the result as a new image other than manually recording a screencast):
One way to do it, yet not sure if it's the best, is as follows:
convert both animations into sprite-sheets of same dimensions
paste one image over the another
cut result into smaller frames and convert it into animated GIF
The commands should look like this:
montage -background none t.gif -tile x1# -geometry +0+0 tt.png
montage e.gif[0-16,0-9] -tile x1# -geometry +0+0 ee.png
magick convert -delay 10 -loop 0 ee.png tt.png -coalesce -flatten \
-crop 150x150 +repage output.gif
Tricky part is the second line with eyes image. It has only 17 frames while skull has 27. So the sprite sheet size must be adjusted.
I works, however I am not quite happy with this as solution requires manual entering of some parameters (frames selection and output image dimensions).

ImageMagick - Add white transparent overlay to image

I need to take a normal image:
and add a white, transparent overlay so it looks something like this:
Don't pay attention to the text on the converted image or the fact that it's a cropped version of the original. I need to simply convert the top to the exact same image, just with this white, transparent overlay. I also need it to be a cli command.
Updated Answer
This is even easier
convert house.jpg -fill white -colorize 80% result.png
Original Answer
Something like this maybe...
convert house.jpg \( +clone -fill white -colorize 80% \) -composite result.png

Image background removal algorithms

I've lots of images with a white background and I need to remove it (make it transparent). I've already tried imagemagick convert and for some image that worked quite well, but still cannot be used on dark background.
I'm kind a noob in this branch of programming... Someone know some good algorithms to remove the background?
Plus, would be great if it could correctly process an image like this:
http://dev.addvert.it/cache/24eeab00e5987452d09fbeec0c7678d6_w472_h472_sc.jpg
The problem with convert diff is it erase a lot of the image, while the border algo doesn't touch the central part.
I totally understand how hard is for a calculator to do something like this and I'm pretty amazed with the results of convert, but if there's the chance of a better solution, why not ask? :D
You are not constrained to converting white pixels to transparent. You could maybe take the colour of the top-left corner pixel and make all pixels that colour transparent and it will work for black backgrounds too:
As suggested by #emcconville, the first of the following two options is more succinct:
convert towel.jpg -fill none -fuzz 2% -draw 'matte 0,0 replace' output.png
or, per my original,
convert towel.jpg -alpha on -fill none -fuzz 5% -draw 'color 0,0 replace' output.png
You may find this more readable...
convert towel.jpg -fuzz 5% \
-transparent $(convert towel.jpg -format "%[pixel:p{0,0}]" info:) \
output.png
In the second line, $(convert ...) just gets the colour of the top-left pixel and feeds that into the middle of the outer convert command as the colour to make transparent - but it is only doing the same thing as the first version.

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

Resources