How to rotate text with imagemagick? - 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

Related

How to handle multiple elements with imagemagick cli?

We need to put 'information bar's to thousands of image files. For like a week or so i'm trying to learn imagemagick but i just couldn't figure this many elements out so i wanted to ask for a help here.
I get the idea of '-/+append'ing elements and swapping between them but when it comes to 3x3 matrix cells and text/image mixings, i just can't do it. As an example, i can get the 3 rows appended and a column next to it but i can't get to the next step of 'appending 2 more rows together then put them as a column block again' because when i try, all those append gets right or bottom as a whole image.. Well, you will get the idea when you see my brief image below..
magick.exe -size 150x100 -gravity center caption:"txt2" caption:"txt3" caption:"txt4" \
-append -size 94x294 xc:white -border 3 -swap 0,1 \
+append outoutout.jpg
FYI, height/width of rows/columns are there just for example not important.. And here comes MSPaint skills:
You have to create each section separately using parenthesis processing. Then if you want append them appropriately. Alternately, you can create a background image and compose ... -composite each image into its correct location.
Here is an example in ImageMagick using the second method.
Unix Syntax:
magick -size 400x400 xc:white \
\( barn.jpg -crop 400x200+0+0 +repage \) \
-geometry +0+0 -compose over -composite \
\( -size 100x200 xc:white -shave 5x5 -bordercolor black -border 5 \) \
-gravity northwest -geometry +0+200 -compose over -composite \
\( -size 100x200 -background white -gravity center -fill black \
-font Candice label:"Text1\n\nText2\n\nText3" \) \
-gravity northwest -geometry +100+200 -compose over -composite \
\( -size 100x200 -background skyblue -gravity center -fill red \
-font Arial label:"First_line\n\n\nSecond_line" \) \
-gravity northwest -geometry +300+200 -compose over -composite \
result.png
See for example:
parenthesis processing
appending
convert ... -composite

imagemagick nested gravity centering

I am trying to composite two images with gravity, and then position them within a larger image at a geometry.
When I try
magick -size 1045x837 xc:blue \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 -gravity center \) -composite \) -geometry +26+53 -composite test-y.png
I get
and when I do
magick -size 1045x837 xc:blue \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 \) -composite \) -geometry +26+53 -composite test-x.png
I get
I think this involves clone and related, maybe similar to this answer, but I just can't find the combo.
What do I need to do, to get the white centered within the red, and geometrically placed in the upper-left corner?
It's not clear exactly what you want, but I think you are falling foul of the fact that -gravity is a "setting". As such, it remains set until changed, so you probably want this, where I reset the gravity to NorthWest before the final composite:
magick -size 1045x837 xc:blue \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 -gravity center \) -composite \) -gravity northwest -geometry +26+53 -composite result.png
You might find -extent a simpler way to fill out the white to a given size using a red background:
magick -size 1045x837 xc:blue \( -size 275x417 xc:white -resize 345x437 -background red -gravity center -extent 345x437 \) -gravity northwest -geometry +26+53 -composite result.png

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.

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

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