Image magick padding font - imagemagick

I am trying to put a label with a transparent background on an image, which works, but there is an issue with padding. The text is too close to the top of the transparent background. I wanted to add some padding to it, but I can't seem to get it to work. I know I could probably do it with draw for the background instead, but the length of the label changes all the time. I guess I could estimate the size I need based on character length, but is there a more simple way in image magick?
I tried to search for an option to maybe change line height or something, but just found spacing between words, chars, or lines instead.
Any ideas?
Convert input.jpg -fill white -undercolor 'rgba(0, 0, 0, .75)' -font Helvetica -pointsize 48 -gravity south -annotate +0+35 " Blah blah blah " output.jpg

You could use label to create your label, then splice some extra lines on top, then composite that onto your image. It is a bit ugly but not too complicated.
convert -background 'rgba(0, 0, 0, .75)' \
-fill white -font Helvetica -pointsize 48 \
label:" Blah blah blah " -splice 0x10 \
input.jpg \
+swap -gravity south \
-geometry +0+10 -composite result.jpg
Or you can do it the other way around and preserve the metadata:
convert input.jpg \
\( -background 'rgba(0, 0, 0, .75)' -fill white \
-font Helvetica -pointsize 48 \
label:" Blah blah blah " -splice 0x10 \
\) -gravity south -geometry +0+10 -composite result.jpg

This can also be a solution for adding a padding on all sides of a transparent text image
Use:
-gravity southeast -splice 20x20 -gravity northwest -splice 20x20
This solution works good for me
Without Padding:
convert -background "rgba(0, 255 ,0, 0.7)" \
-font "Lobster-Regular.ttf" -pointsize "60" \
-fill "#FFFFFF" label:"PROMOTE ANYTHING" \
heading.png
With Padding:
convert -background "rgba(0, 255 ,0, 0.7)" \
-font "Lobster-Regular.ttf" -pointsize "60" \
-fill "#FFFFFF" label:"PROMOTE ANYTHING" \
-gravity southeast -splice 20x20 -gravity northwest -splice 20x20 \
heading.png

Related

Add a tile watermark to a part of the image only

Usually, I add watermark text on documents with something like this:
convert -size 700x360 xc:none -fill "#00000020" -font "Liberation-Sans-Bold" -pointsize 60 \
-gravity NorthWest -draw "text 0,75 \"${3}\"" \
-gravity SouthEast -draw "text 0,75 \"${3}\"" \
miff:- |\
composite -tile - "${1}" "${2}"
It spreads a tile with text over the whole image.
Now I need to add a watermark that excludes a specific area on the document.
My question is:
How to limit "operating area" only to a specific rectangular image region defined as x,y,width,height?
On the other hand: Is it possible and how to exclude a specific rectangular region and spread my tiles only outside of it?
You can do that as follows in Imagemagick by creating a tiled watermark image of the size you want and then compositing over the background image.
Here I create the tile image (mpr:wm) . Then tile it out with a transparent background using -draw "color 0,0 reset". Then I composite that over the background image at the desired upper left corner (+32+32). This is all done in one convert command line without needing to pipe.
In:
in="mandril3.jpg"
out="mandril3_wm.jpg"
text="Testing"
convert -size 64x64 xc:none -fill black \
-font "candice" -pointsize 14 \
-gravity NorthWest -draw "text 0,5 '$text'" \
-gravity SouthEast -draw "text 0,5 '$text'" \
-write mpr:wm +delete \
-size 128x128 xc:none -tile mpr:wm -draw "color 0,0 reset" \
$in +swap -geometry +32+32 \
-compose over -composite \
$out
Out:

Use Imagemagick to create a bar with text at top of image and a different text bar at the bottom

Trying to create a solid 1920X1920 transparent canvas, then put a 1920X420 colored bar at the top with centered text and the same at the bottom with different centered text.
What I am doing now, when it gets created creates the 1920X1920 transparent image and puts the bar at the top with the text, but the bar at the bottom is non-existent.
convert -size 1920x1920 xc:"transparent" -size 1920x420 -font
Din-Condensed-Bold -pointsize 84 canvas:#800000 -fill black -
gravity center caption:"TEXT FOR THE BOTTOM" -gravity south
-composite -size 1920x420 -font Din-Condensed-Bold -pointsize 84
canvas:#800000 -fill black -gravity center caption:"TEXT FOR THE
TOP" -gravity north -composite newfile.png
A few things...
The main issue is that you set the background colour for a label with -background rather than canvas:.
Note that -font, -size, -pointsize and -fill are settings, as such they only need setting once and then they remain set until changed, so you don't need to keep repeating them.
Your command then becomes like this:
convert -size 1920x1920 xc:yellow \
-size 1920x420 -font "AppleChancery" -pointsize 84 -background "#800000" -fill black \
-gravity center caption:"TEXT FOR THE BOTTOM" -gravity south -composite \
-gravity center caption:"TEXT FOR THE TOP" -gravity north -composite result.png
Very Spanish, don't you think? :-)
In case you were actually looking for -undercolor:
convert -size 1920x1920 xc:yellow \
-size 1920x420 -font "AppleChancery" -pointsize 84 -background magenta -undercolor cyan -fill black \
-gravity center caption:"TEXT FOR THE BOTTOM" -gravity south -composite \
-gravity center caption:"TEXT FOR THE TOP" -gravity north -composite result.png
You say you want some "breathing space" around the edges, so I am adding a further example. I make the red and yellow boxes slightly smaller than the magenta background and position the top one inset 5 pixels from the top and 5 from the left using -geometry. Likewise, I set gravity to SouthWest and position the yellow box 2 pixels from the right edge and 25 from the bottom:
convert -size 100x100 xc:magenta -size 90x20 \
-gravity northwest xc:red -geometry +5+5 -composite \
-gravity southeast xc:yellow -geometry +2+25 -composite result.png

Create text mask using ImageMagick

I need to create an image from some background and text, like:
I try
convert -size 320x100 xc:transparent \
-font Arial -pointsize 72 -tile "bg.jpg" \
-annotate +28+68 'Some text' clear.png
but this does not work.
Use the following command:
magick -gravity center input.jpg ( +clone -threshold 101% -fill white -font berlin-sans-fb-bold -pointsize 124 -draw "text 0,0 Hello" ) -alpha off -compose copyopacity -composite -trim output.png

ImageMagick: redraw a text without "-draw" for only outward stroke

I try to create a text image with ImageMagick, where is the stroke expands only outward. I found a solution, where I should use the "-draw" command, but with it I would need the size of my image, but I don't know it in advance.
The command below should be upgrated. Somehow I would need to draw the text again on it, without strokewidth:
convert -background none -fill white -pointsize 100 -stroke red \
-strokewidth 20 label:text stroke.png
#leu's solution almost good, but the positioning of the new text isn't in the good position. My result was that above, and I don't know, where I took a mistake:
My suggestion: combine label:"Some text" with -annotate "Some text". Like this:
#myfont="Arial-Black-Standard"
#myfont="Tahoma"
#offset="-0-0"
#offset="-20-10"
#offset="-30-10"
#offset="-10+10"
myfont="Tahoma"
offset="+10+10"
convert -respect-parentheses \
\( \
-font "${myfont}" \
-pointsize 180 \
-strokewidth 18 \
-fill blue \
-stroke blue \
-background none \
label:"Test text" \
\) \
-gravity center \
-font "${myfont}" \
-pointsize 180 \
-fill white \
-annotate "${offset}" "Test text" \
result${offset}.png
Play with the offset=... variable (also with the point sizes and stroke widths) to get closer to what you want. Here are some of my results:
However, like #MarkSetchell, I do not fully comprehend what you want to achieve. An explanation of what you mean by "outward stroke" that I do understand is missing...
yes: draw the same text onto your pic - maybe by piping output of your command to another convert
convert -background none -fill white -pointsize 100 -stroke red \
-strokewidth 20 label:text png:- \
| convert -fill white -pointsize 100 -stroke none \
-draw 'text 10,82 "text"' - stroke.png
the trick is to correctly place the second string. x-position is half the strokewidth, but y-position seems to be font- and pointsize-dependant. My approach was to first place both strings over each other when strokewidth was set to 0:
convert -background none -fill white -pointsize 100 -stroke red \
-strokewidth 0 label:text png:- \
| convert -fill white -pointsize 100 -stroke none \
-draw 'text 0,72 "text"' - stroke.png
then you have to add half the strokewidth to this y-position as well.
a bit of trial-and-error to get the correct y-position, but it seems to work for any string.
== edit ==
of course, we don't need to do trial-and-error. But instead we can use ImageMagick's power. Just perform the following steps (in this example within Bash):
#!/bin/bash
# read text from command line (or use "Test Text")
text=${1:-"Test Text"}
# strokewidth
sw=20
# pointsize
ps=120
# font
font=Arial
# result file
result="stroke.png"
# do some calc
sw_half=$(expr $sw / 2)
convert -background none -font $font -fill white -pointsize $ps -stroke red -strokewidth $sw label:"$text" $result
convert -background none -font $font -fill white -pointsize $ps -stroke none label:"$text" png:- | composite -geometry +${sw_half}+${sw_half} - $result $result
You can get the list of fonts available on your system by
convert -list font
The idea is the same as above: draw the text twice and draw one onto the other while using an offset of half the strokewidth.
The results all look like the following

Appending a label of variable width to one with a fixed size

I am trying to use an Imagemagick command to create a single-letter text label, give it a shadow, place it on the left side of a fixed-size canvas area, and then append this it another label of fixed height but unknown width. So, the desired result is a single letter on the left side of the final transparent PNG, and another label set about 100px to the right of the origin, e.g. this mockup:
I have all of this working in the following command, except that the shadowed text label is not in a fixed size box (should be 100px by 25px). Here's the result:
I think that what I need to do is to turn off the -trim option somehow, but I'm not sure how to do that. +trim is not a valid option, and +repage doesn't do it.
convert \
\( -background transparent \
\( -gravity west -fill lavender -font Constantia.ttf \
-pointsize 12 label:'x' -trim \
\( +clone -background black -shadow 100x3+0+0 -channel A -level 0,50% \
+channel \) \
+swap +repage -gravity center -composite \) \
-size 100x25 -gravity west \) \
\( -size x25 -fill black -background transparent -font MyriadPro-Semibold.otf \
-pointsize 15 label:'Long legend for x' -gravity west \) \
+append -strip legend_test.png
(The trim option is needed to get the height down to 25px--the shadow operation generates too large a vertical extension otherwise. EDIT: And I guess I was wrong above--even w/o the -trim anywhere in the command, the fixed-size image I'm hoping for doesn't work out.)
Hm, looks like there isn't much of a community for ImageMagick left here. I posted to the ImageMagick phpBB board and was able to put together an answer. Briefly:
The -trim option is not an option that affects every image after it is invoked. Instead, it is essentially a command that executes immediately.
The -extent option can be used to increase or decrease an image to a desired size. Apparently the -size command, which I used in my original code, can only be used to set the initial size of an image, not to change the size of an image that already exists.
Here is the final working command:
convert \
\( -background transparent -extent 100x25 -gravity west \
\( -fill lavender -font Constantia.ttf \
-pointsize 12 label:'x' -trim -extent 100x25 -gravity west \
\( +clone -background black -shadow 100x3+0+0 -channel A -level 0,50% \
+channel \) \
+swap +repage -gravity center -composite \) \
-background transparent -extent x25 \) \
\( -size x25 -fill black -background transparent -font MyriadPro- Semibold.otf \
-pointsize 15 label:'Long legend for x' -gravity west \) \
+append -strip legend_test.png

Resources