Imagemagick convert: use Pango to format captions in auto generated slides - parsing

I use this code to auto generate slides from a .txt file where I wrote captions this way:
CAPTION 1
CAPTION 2
...
CAPTION N
This is the script I use
#!/bin/bash
i=0
# loop through files
while IFS= read -r p; do
# if it's not an empty line
if ! [ -z "$p"]; then
# echo line
echo "$p";
convert -background none -font Trebuchet-MS -fill white -pointsize 60 -gravity center -draw "text 0,300 'pango:$p'" slide_template.png slides/slide-$i.png
i=$((i+1))
fi;
# pass input
done <$#
slide_template.png is simply an empty (transparent) 1920x1080 png.
I pass my .txt file this way:
$ sh my_script.sh my_file.txt
And it generates my slides in /slides.
Now I'd like to use some format code into my slides, like
MY <b>CAPTION</b> 1
MY <i>CAPTION</i> 2
...
MY CAPTION N
But I can't understand how to use pango in my previous code. I need to reposition my caption line centered, 300 pixels from the bottom.
If I use:
convert -background none -font Trebuchet-MS -fill white -pointsize 60 -gravity center -draw "text 0,300 '$p'" slide_template.png slides/slide-$i.png
I get:
If I use this line:
convert -background none -font Trebuchet-MS -fill white -pointsize 60 -gravity center pango:"$p" slide_template.png slides/slide-$i.png
I get TWO files (why?), where the first one is correctly parsed but cropped to the text size:
And the second one is the background. Filenames this way are slide-0-0.png and slide-0-1.png

Solved: I need to pipe one image to another.
The first contains the formatted code, the second overlays the piped data onto the background.
#!/bin/bash
i=0
# loop through files
while IFS= read -r p; do
# if it's not an empty line
if ! [ -z "$p"]; then
convert -background none -font Trebuchet-MS -fill white -pointsize 60 -gravity center -size 1920x300 pango:"$p" png:- | convert slide_template.png png:- -geometry +0+800 -composite slides/slide-$i.png
i=$((i+1))
fi;
# pass input
done <$#

Related

Captioning all frames of a gif

I have a simple bash script to caption still images (jpg, png...) but it completely fails when given an animated gif. The error is convert: unable to write pixel cache '/tmp/magick-[random chars]': No space left on device # error/cache.c/WritePixelCachePixels/5854. There are many questions similar to mine, however they use specific coordinates to determine where the text should be placed (at least, I think). My script creates a dynamically sized caption area for the text to be written to. The image is appended to the bottom of the caption area (or the caption area is prepended to the top of the image, however you want to think about it.)
#!/bin/bash
input=$1
caption=$2
output=$3
wd=`convert $input -format "%w" info:`
convert \( -size ${wd}x -background white -gravity north -fill black -font FuturaBT-ExtraBlackCondensed -pointsize $(($wd/17)) \
caption:"$caption" \) \
$input \
-append $output
You can also do this in Imagemagick.
convert anim.gif -coalesce \
-gravity north -background white \
-splice 0x18 -font Arial -pointsize 12 -annotate +0+0 'THIS IS A TEST OF CAPTIONING TEXT' \
-layers Optimize anim3.gif
In Imagemagick you will need to create a text image from your caption and then use -layers composite to apply it to your animation.
Input GIF Animation:
Imagemagick 6 Unix Syntax:
convert \( anim.gif -coalesce \) null: \( -size 100x -background white -gravity north -fill black caption:"THIS IS A TEST OF CAPTIONING TEXT ON AN ANIMATION" \) -compose over -layers composite -layers optimize anim2.gif
For Windows, remove the \s
For Imagemagick 7, change convert to magick.

Get geometry of text on an image

I'd like to draw a text onto an image in a way like this:
convert -quality 100 -font Oswald-Regular -pointsize 515 -fill black -draw "text 1339.0,1099 'some text'" /tmp/ascript.png /tmp/ascript.png
and I need to know the dimensions of the text with the above parameters (size, font, text). How can I get that?
I tried something like this:
convert -size 5000x1500 xc:lightblue -font Oswald-Regular -pointsize 515 -fill none -undercolor white -annotate +20+100 'some text' -trim info:
but it's giving false result:
xc:lightblue XC 1834x250 5000x1500+19+0 16-bit sRGB 0.010u 0:00.000
.
What is the proper way (or a working way) to get the dimension of a drawn image based on this 3 parameters (font, size, text)?
I'm not strictly binded to ImageMagick, it can be any command line tool for the Linux shell, however, the text will be drawn by convert.
There are a couple simple ways to get the dimensions using ImageMagick with commands like this...
convert -size 5000x1500 xc:lightblue -font Oswald-Regular -pointsize 515 \
-fill none -undercolor white -annotate +20+100 'some text' \
-format "%[#]\n" info:
That uses the FX escape "%#" as the formatting string for the "info:" output. It will show IM's calculation of the after-trim width, height, horizontal offset, and vertical offset like "WxH+X+Y".
This similar command just gives the width and height of the trimmed text...
convert -size 5000x1500 xc:lightblue -font Oswald-Regular -pointsize 515 \
-fill none -undercolor white -annotate +20+100 'some text' \
-trim +repage -format "%[w]x%[h]\n" info:
That will trim the text, reset the paging geometry with "+repage", then output a string showing "WxH".
––– Edited to Add –––
I tried your image with_text.png with these commands. The output immediately follows each command...
convert with_text.png -format "%[#]\n" info:
1807x389+512+115
convert with_text.png -trim +repage -format "%[w]x%[h]\n" info:
1807x389
Those were tested with IMv6.8.9-9 on ubuntu bash on Windows 10. If you use that actual image and those commands, I'm not sure why you would get different results.

Chain 2 imagemagick commands

I would like to add a logo and some text to an image.
I can achieve this through the following:
// Add logo
composite -geometry +10+20 logo.png input.jpg \
output_with_logo.jpg
// Add text
convert output_with_logo.jpg -font Arial -pointsize 20 \
-draw "fill black text 20,50 'Test'" \
final.jpg
However I'm wondering if I can chain these 2 commands together so I can work from the same source file at once, instead of saving out staged versions of the image.
I've tried:
convert -font Arial -pointsize 20 \
-draw "fill black text 20,50 'Test'" \
-composite -geometry +10+20 input.jpg logo.png \
final.jpg
However this creates 2 "Test" strings on the image
Like this:
convert input.jpg logo.jpg -geometry +10+20 -composite -font Arial -pointsize 20 -draw "fill black text 20,50 'Test'" final.jpg
Rather than use the composite command which won't let you add text, use the convert command and its -composite operator which does the same thing. So, I am saying:
composite A.jpg B.jpg result.jpg = convert A.jpg B.jpg -composite result.jpg
Then, once you have done the compositing, you can add the text afterwards - exactly as you had it.

Start path relative to corner of image

I have written the following script which uses the ImageMagick* convert utility to append axis labels to an existing image.
LEFT_="l -30,0 +2,+2 -6,-2 +6,-2 -2,+2 z"
RIGHT_="l 30,0 -2,+2 +6,-2 -6,-2 +2,+2 z"
convert -size 240x160 pattern:SMALLFISHSCALES \
-pointsize 16 -fill black -background white \
-gravity SouthEast -splice 0x20 \
-draw "translate 40,0 text 0,0 'Time' stroke red path 'm 5,2 $RIGHT_'" \
-gravity NorthWest -splice 20x0 \
-draw "rotate +90 translate 40,-10 text 0,0 'Value' path 'm -5,2 $LEFT_'" \
example.png
Which produces the following image:
This is almost exactly what I am after, except that the red arrow is out of place. I expected the red arrow to appear next to the Time label, since its start point is specified as a relative position in the same draw command. Unfortunately, it looks like the -gravity option is affecting the text primitive, but not the path primitive.
Is there a way to reference the SouthEast corner, or the Time text label when specifying the start position of the red arrow? I can't use absolute coordinates, because the size of the image varies.
*ImageMagick 6.7.8-9 on CentOS 7
Updated Answer
Maybe you can make Unicode text arrows like this then they will be affected by gravity...
perl -e 'binmode(STDOUT,":utf8"); print "Time ... \x{2192}\x{2191}";'|
convert -font TimesNewRoman -pointsize 36 label:#- arrows.png
Depending on your OS, the following may do as a replacement for the Perl above...
printf "%b" "\u2192" | convert ...
Original Answer
I am not at all familiar with paths, but I can suggest a way to achieve what you want that doesn't use gravity at all, and maybe that will help.
Rather than use -splice, you can clone your original image and crop it to the size you planned to splice on, and then -append the strips that label the axes. It is easier to show you the command than explain it!
convert -size 240x160 pattern:SMALLFISHSCALES \
\( +clone -crop x20+0+0 -fill blue -colorize 100% \) \
-append \
\( +clone -crop 20x+0+0 -fill red -colorize 100% \) \
+swap +append result.png
I have filled the x-axis blue, but remove that and add whatever labelling and arrows you need, and I filled the y-axis red, but likewise remove that and add labelling and arrows - rotating as necessary.
Two tricky things to note...
-append will append the second image below the first
+append will append the second image to the right of the first, so I +swap beforehand to put it on the left side.

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

Resources