IMAGEMAGICK: Filling when label contains blanks - imagemagick

I am using imagemagick to create some simple graphics using the Dymo font. Here is an example:
convert -background White -fill DarkRed -font Dymo -pointsize 72 label:"DYMO FONT" -trim name.png
This command creates a file that looks like this:
I would like the red to fill all the way across, so that the image looks like a single label. I plan to use this on a page with a black background, which makes it look even worse.
I have played around with this for a while with no luck. Help would be appreciated.
Version: ImageMagick 6.9.2-7 Q16 x86_64 2015-12-06
O/S: Fedora 23

I don't know why it does that, but you can generate the text you want by replacing the space with a UTF non-breaking space and sending that to the stdin of convert and asking -label to read its text from the "file" called stdin:
printf "DYMO\xc2\xa0FONT" |
convert -background white -fill DarkRed -font DYMO -pointsize 72 label:#- result.png
Add -trim just before the output filename if you want the extraneous white space trimmed off from around the edges.
If you had more complicated text and didn't want to do that for all spaces, you could replace spaces using a short piece of Perl or sed to do it for you...
echo -n "Text with lots of spaces." | sed 's/ /\xC2\xA0/g' | convert -background white -fill DarkRed -font dymo -pointsize 72 label:#- -trim label.png

Related

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.

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

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 <$#

Apply watermark with text / image using GraphicsMagick

I need to be able to apply watermark on an image, using either text or another image.
Getting it to work with an image was quite straight forward, using the following command:
gm composite -dissolve 15 -tile logo.png image.jpg wmark_tiled.jpg
Now, I also want to be able to use text for watermarking (in other cases). The only thing that I found close to it, is a command from ImageMagick tutorial which is the following :
convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw "text 10,10 'copyrigh text'" -gravity SouthEast -draw "text 5,15 'copyright text'" miff:- | gm composite -tile - image.jpg copyrightImage.jpg
Although its not working in GM, and I would rather avoid using pipe as it causes some headaches managing it from Java.
What I thought I could do, is generate an image from a given text, and then use that image as logo.png is used on my first command. Although I cannot find how to generate an image out of text, all I find is putting text on top of an image.
Ideally I will generate a transparent image with the text, and from what I see modifying font/color etc should be quite flexible.
Any suggestions on how to achieve this, or any better solutions are welcome.
(I added imagemagick tag as the interfaces are often same/similar)
I'm not sure I fully understand your query, so apologies if I've misunderstood, but are you trying to create a transparent image, with some text in the corner? If so, would this not work?
convert -background transparent -fill grey -font Calibri -size 140x80 -pointsize 14 -gravity southeast label:'copyright text' output.png
Obviously adjusting the font, pointsize, output image name etc. That would create the following:
http://oi42.tinypic.com/14j1bvp.jpg
P.S. that was written for ImageMagick. I don't know how GM differs or whether it would still work.
To be able to get the watermark text into the same image I had to use the -annotate parameter.
So Moogle's code snippet would look like this in my case:
convert original_image.jpg -background transparent -fill grey -font Calibri -size 140x80 -pointsize 14 -gravity southeast -annotate +0+0 'copyright text' output.jpg

Add gradient overlay under a photo label using imagemagick

I'm trying to convert a bunch of photos using imagemagick. However, I hadn't figured out how to overlay an image with gradient and write some text on it. I know the text part though:
convert IMG_8408.jpg \
-font URWChanceryMediumI \
-pointsize 250 \
-draw "gravity south
fill black text 0,40 'Some text stuff here'" \
test.jpg
Is there a way to add a white gradient to the bottom? Note, that the image size may vary.
What I have:
What I want:
I picked the colors so that it's clearly visible what I want to achieve
You can achieve desired output with 3 commands:
a. create the upper part of your image (a solid rectangle with your selected background color):
convert -size 640x200 xc:#A02B2B background.jpg
b. create another image containing the text over a gradient:
convert -size 640x110 gradient:#A02B2B-#126B27 -pointsize 25 -draw "gravity south fill black text 0,40 'Some text stuff here'" text.jpg
c. combine the images to obtain the final output:
montage background.jpg text.jpg -tile 1x2 -geometry +0+0 output.jpg
Note: I modified text creation parameters in step 2 to keep the command short, but you can add back your original settings
Use the following command:
magick -size 640x310 -define gradient:vector="0,107 0,0" gradient:"#a02b2b-#126b27" -flip -gravity south -font script-mt-bold -pointsize 48 -annotate +0+24 "Some text stuff here" output.png

How to shade text using imagemagick?

I am wondering how to add a shaded copyright text at the bottom of a jpeg. Currently I simply use:
convert input.jpg -font /usr/share/fonts/truetype/pointfree/pointfree.ttf -pointsize 15 -fill white -gravity SouthEast -strokewidth 3 -annotate +0+5 " #blabla.com " "output.jpg"
The problem with this is that when the background is light the text disappears.
I am aware that I could add a flag like
-undercolor '#00000080'
but I find this rather obtrusive, so looking for a better solution that make text's visiblily more independent of the background color. Note: image sizes are different so I can not hardcode the text's coordination.
Try this:
convert input.jpg -font /usr/share/fonts/truetype/pointfree/pointfree.ttf -pointsize 15 -gravity SouthEast -strokewidth 3 -fill black -annotate +2+7 " #blabla.com "-fill white -annotate +0+5 " #blabla.com " "output.jpg"
There was a post on the imagemagick forum from amember which took into consideration the colour under the text and produced a white or black watermark to suite. I was looking for it the otherday and could not find it. It was a batch script from memory.
I had to add the comment here as I was getting some message I did not understand about notifications when trying to add a comment.
Sorry I missed out a space here " #blabla.com "-fill should be " #blabla.com " -fill The command line is very long and would be easier to work with if you split it onto different lines.
Ok, finally I found a good batch solution:
for FILE in *.jpg; do convert $FILE -gravity southeast -stroke '#000C' -strokewidth 2 -annotate 0 'blabla.com' -stroke none -fill white -annotate 0 'blabla.com' $FILE; done

Resources