Create text mask using ImageMagick - 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

Related

How to rotate text with imagemagick?

I have a long image source pic and I want to add text to every subimage, but when add text to the book, I failed, the final image and code:
convert src.jpg -gravity north -pointsize 53 -fill white -annotate +0+520 "I love you,don't leave me " -annotate +0+1146 "Do not be a programmer,ok?" -annotate +0+1772 "fuck u!" -annotate +0+3024 "too simple " -draw 'translate 340,3340 rotate 21 text 0,0 "Javascript Action"' final.jpg
Can any one help? Thanks
For your example, you should use -annotate for all the text placements and rotations in Imagemagick rather than using -draw for the last one. This will keep your last text from going off the image. Try this:
convert src.jpg -gravity north -pointsize 53 -fill white -annotate +0+520 "I love you,don't leave me " -annotate +0+1146 "Do not be a programmer,ok?" -annotate +0+1772 "xxxx x" -annotate +0+3024 "too simple " -gravity north -annotate 21x21+0+3340 "Javascript Action" final1.jpg
See https://imagemagick.org/script/command-line-options.php#annotate
Please do not use vulgarisms in examples.
If you want to have text wrap, then you need to use caption: to create a new image with transparent background for each text. Then composite the new text image onto your background image at the desired locations. You can control when it wraps, by the -size Wx argument. Here I use the full width of your image, that is, -size 1000x.
convert src.jpg \
-pointsize 53 -background none -fill white -size 1000x \
\( -gravity center caption:"I love you,don't leave me" \) -gravity north -geometry +0+420 -compose over -composite \
\( -gravity center caption:"Do not be a programmer,ok?" \) -gravity north -geometry +0+1046 -compose over -composite \
\( -gravity center caption:"xxxx x" \) -gravity north -geometry +0+1672 -compose over -composite \
\( -gravity center caption:"too simple" \) -gravity north -geometry +0+2924 -compose over -composite \
\( -gravity center caption:"Javascript Action" -rotate 21 \) -gravity north -geometry +0+3240 -compose over -composite \
final2.jpg
Or to make it wrap earlier than the width of the your image, I set -size 500x
convert src.jpg \
-pointsize 53 -background none -fill white -size 500x \
\( -gravity center caption:"I love you,don't leave me" \) -gravity north -geometry +0+420 -compose over -composite \
\( -gravity center caption:"Do not be a programmer,ok?" \) -gravity north -geometry +0+1046 -compose over -composite \
\( -gravity center caption:"xxxx x" \) -gravity north -geometry +0+1672 -compose over -composite \
\( -gravity center caption:"too simple" \) -gravity north -geometry +0+2924 -compose over -composite \
\( -gravity center caption:"Javascript Action" -rotate 21 \) -gravity north -geometry +0+3240 -compose over -composite \
final3.jpg
See
https://imagemagick.org/Usage/text/#caption
https://imagemagick.org/Usage/layers/#convert
ADDITION:
To blur the text, just add -blur after caption:
convert src.jpg \
-pointsize 53 -background none -fill white -size 500x \
\( -gravity center caption:"I love you,don't leave me" -blur 0x3 \) -gravity north -geometry +0+420 -compose over -composite \
\( -gravity center caption:"Do not be a programmer,ok?" -blur 0x3 \) -gravity north -geometry +0+1046 -compose over -composite \
\( -gravity center caption:"xxxx x" -blur 0x3 \) -gravity north -geometry +0+1672 -compose over -composite \
\( -gravity center caption:"too simple" -blur 0x3 \) -gravity north -geometry +0+2924 -compose over -composite \
\( -gravity center caption:"Javascript Action" -blur 0x3 -rotate 21 \) -gravity north -geometry +0+3240 -compose over -composite \
final4.jpg

Using Imagemagick to add 2 text strings with drop shadow to a background

I am trying to place 2 annotated texts onto a background in different locations and give the text font shadow. A single text works fine, but when I add the second, it blurs the background and the rest of the image.
Here is the image results: https://imgur.com/a/VW09KIy
The first command works as expected:
convert ~/.backgrounds/White.jpg -font Bitter -pointsize 72 -annotate +130+170 'Anthony' -blur 0x4 -fill white -annotate +125+165 'Anthony' font_shadow_fuzzy.jpg
The second messes everything up and I need to know how I fix it.
convert ~/.backgrounds/White.jpg -font Bitter -pointsize 72 -annotate +130+170 'Anthony' -blur 0x4 -fill white -annotate +125+165 'Anthony' -annotate +230+270 'Anthony' -blur 0x4 -fill white -annotate +225+265 'Anthony' font_shadow_fuzzy.jpg
The problem you are having with your Imagemagick command is that the second -blur affects the first text. So you need to separate the processing for each text and put it on a transparent background. Then flatten the two results onto a white background.
The following works for me with Imagemagick 6.9.10.9 Q16 Mac OSX Sierra:
convert \
\( -size 600x400 xc:none -font ubuntu -pointsize 72 -fill black -annotate +130+170 'Anthony' -blur 0x4 -fill white -annotate +125+165 'Anthony' \) \
\( -size 600x400 xc:none -font ubuntu -pointsize 72 -fill black -annotate +230+270 'Anthony' -blur 0x4 -fill white -annotate +225+265 'Anthony' \) \
-background white -flatten \
font_shadow_fuzzy.jpg
Uploading does not seem to be working at this time, so here is a link to the result:
http://www.fmwconcepts.com/misc_tests/font_shadow_fuzzy.jpg
You did not say how big your white background image was. So if I assume it is 600x400, then I will create one and modify the command. You could use any other background, but need to know how big it is for the transparent background to your text.
convert -size 600x400 xc:white white.jpg
convert white.jpg \
\( -size 600x400 xc:none -font ubuntu -pointsize 72 -fill black -annotate +130+170 'Anthony' -blur 0x4 -fill white -annotate +125+165 'Anthony' \) \
\( -size 600x400 xc:none -font ubuntu -pointsize 72 -fill black -annotate +230+270 'Anthony' -blur 0x4 -fill white -annotate +225+265 'Anthony' \) \
-flatten \
font_shadow_fuzzy2.jpg
Or, for any arbitrary background that is larger than where you want to place your text, you can find the dimensions and do the processing as follows:
infile="white.jpg"
dim=`convert "$infile" -format "%wx%h" info:`
convert "$infile" \
\( -size $dim xc:none -font ubuntu -pointsize 72 -fill black -annotate +130+170 'Anthony' -blur 0x4 -fill white -annotate +125+165 'Anthony' \) \
\( -size $dim xc:none -font ubuntu -pointsize 72 -fill black -annotate +230+270 'Anthony' -blur 0x4 -fill white -annotate +225+265 'Anthony' \) \
-flatten \
font_shadow_fuzzy3.jpg
One other way to do this is to make two small text images with your shadow as separate images on a white background. The black blurred text would have and offset of +5+5 and the white unblurred text would have offsets of +0+0. Do this for both images, then composite them onto your background white image at the desired offsets using -geometry for the offsets.

ImageMagick: inter-line spacing with captions creates cutoff's

I'm using the following command to create a caption. But I want to control the interline spacing as well. When I set the value to negative (to get the lines closer to eachother), the first line gets it's capital letters cut off. I tried added a "border", but that just adds a border - doesn't add "padding". Is there a way to add "padding"?
convert templates/input.pdf -font test.ttf
-size 1245x283! -background red -fill white -gravity northwest
-interline-spacing -25
caption:"Interline-Spacing NOT WORKING\r\nwith Captions"
-geometry +129+129 -composite output/temp.png
In ImageMagick, you can set the -gravity center, then use -trim +repage, if you want to remove excess around the text and then add any amount of border back using -border or -extent if you know the desired output size.
Try this using unix syntax:
convert \( -size 2115x560 xc:skyblue \) \( -size 1245x283! -background red -fill white -gravity center -font arial -interline-spacing -25 caption:"Interline-Spacing NOT WORKING\r\nwith Captions" -trim +repage -background red -extent 1245x283! \) -gravity northwest -geometry +129+129 -composite temp.png
or this in Windows:
convert ( -size 2115x560 xc:skyblue ) ( -size 1245x283! -background red -fill white -gravity center -font arial -interline-spacing -25 caption:"Interline-Spacing NOT WORKING\r\nwith Captions" -trim +repage -background red -extent 1245x283! ) -gravity northwest -geometry +129+129 -composite temp.png
If you want the white text left justified, then use -gravity west before -extent and -gravity west before the caption:
convert \( -size 2115x560 xc:skyblue \) \( -size 1245x283! -background red -fill white -gravity west -font arial -interline-spacing -25 caption:"Interline-Spacing NOT WORKING\r\nwith Captions" -trim +repage -background red -gravity west -extent 1245x283! \) -gravity northwest -geometry +129+129 -composite temp2.png
Replace your input image in place of my -size 2115x560 xc:skyblue and your font for mine.
I would guess the interline spacing is from the base line of the text up. Lower letters will protrude below the base line. I suppose it is more of a "line height"
The obvious solution is to either increase the spacing to allow for capital letters or do not use capital letters.
A setting of -15 seems to work for what you are doing.

ImageMagick draw rectangle with special corners

Hi I would like to create a mask image with "special corners" I am calling them special because I don't really know how to call them in english here is what I would like to achieve:
what I am using now is
convert xc:black -size 300x300 -fill white -draw "roundrectangle 3,3,296,296,5,5"
but this gives me rounded corners. Thank you in advance for any suggestions.
Here's one way of doing it.
convert -size 300x300 xc:none \
-shave 10 -bordercolor black -border 10 \
-fill black -draw "polyline 0,0 30,0 0,30" \
\( +clone -flip \) -gravity north -composite \
\( +clone -flop \) -gravity south -composite -background white -flatten result.png
That says... "Draw a rectangle your full size and transparent, shave 10 pixels off all round and add a 10 pixel black border (easier than doing the maths and making a 280x280 and adding 10 on each side). Draw a triangle in the top-left. Copy the whole shape and flip it and draw it on top of the original. Copy the whole shape and flop it and draw it again on the original. Now make all the transparent areas white."
Here is another way - maybe a little easier. Draw the original square, then copy it, enlarge it by square-root(2) (i.e. 141%), thicken the borders, rotate 45 degrees and composite it onto itself. Kinda depends how your brain works!
magick -size 300x300 xc:none -shave 10 -bordercolor black -border 10 \
\( +clone -scale 142% -shave 30 -border 30 -rotate 45 \) \
-gravity center -composite -background white -flatten result.png
Here is a link to a page showing how to do what you want along with other effects https://www.imagemagick.org/Usage/thumbnails/#rounded
This is the code from the page:
convert thumbnail.gif -alpha set -compose DstOut \
\( -size 20x15 xc:none -draw "polygon 0,0 0,14 19,0" \
-write mpr:triangle +delete \) \
\( mpr:triangle \) -gravity northwest -composite \
\( mpr:triangle -flip \) -gravity southwest -composite \
\( mpr:triangle -flop \) -gravity northeast -composite \
\( mpr:triangle -rotate 180 \) -gravity southeast -composite \
corner_cutoff.png
You should check out the examples as there is another method you could use for smaller images.

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