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

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.

Related

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

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

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