Smooth text edge with ImageMagick - imagemagick

I'm using ImageMagick ver 6.9.3-7 to write text over pictures. I've noticed that the same text with the same font looks much better in html canvas and I'm trying to make it looks the same.
This is how it looks in the html5 canvas:
and this is how it's looks with IM:
(enlarge the images to view the different)
This is how I'm creating the image with node.js:
gmFrame
.font("./assets/impact.ttf", fontSize)
.stroke("#000")
.strokeWidth(8)
.draw(`gravity center text ${position.x},${position.y} '${text}'`)
.stroke("transparent")
.fill("#fff")
.draw(`gravity center text ${position.x},${position.y} '${text}'`);
Is there something to do about it?

After investigating the only way to do it is with blur.
This is an example for how to add text with blur without writing another file
convert -quality 100 "DJ Pauly D.jpg" -resize 500x500 \( +clone -alpha transparent -pointsize 52 -font impact -stroke "#000" -strokewidth 8 -draw "gravity center text 0,0 'WRITE SOMETHING'" -stroke "transparent" -fill "#fff" -draw "gravity center text 0,0 'WRITE SOMETHING'" -blur 0x5 \) -composite r2.png

Related

align text to left with imagemagick

I am using ImageMagick to add text to an image. I used to -gravity Center flag to put the text in the center of the image. But I think this is resulting in the text being center aligned too. I want the text at the center of the image but left aligned. Here is an example of what I'm trying to have:
This is the output I'm getting:
Current output
This is what I want:
This is my requirement
How do I accomplish this? This is my first time using ImageMagick. Please guide me.
Here is one way to do that in Imagemagick 6. I specify the background color, the font color (fill), the font and the pint-size and gravity west (left side). I use label: to create the two lines of text with a new line between them. This creates a text image of the size needed to hold the text. Then I pad the image all around that to the final size with the text image in its center using the same background color.
convert -background black -fill white -font ubuntu -pointsize 28 -gravity west label:"This is line 1 of text\nThis is line 2 of text" -gravity center -extent 400x300 result.png
See
https://imagemagick.org/Usage/text/
https://imagemagick.org/Usage/crop/#extent
ADDITION
If you want to put the text over an existing image, then you do something similar, but in place of the extent, we composite the text image over the background image.
The following is Unix syntax. For Window, remove the backslashes \ before the parentheses.
Input:
convert lena.png \( -background black -fill white -font ubuntu -pointsize 21 -gravity west label:"This is line 1 of text\nThis is line 2 of text" \) -gravity center -compose over -composite result.png
Result:
Or if you do not want the black background, use "none" for the color.
convert lena.png \( -background none -fill white -font ubuntu -pointsize 21 -gravity west label:"This is line 1 of text\nThis is line 2 of text" \) -gravity center -compose over -composite result2.png

Add top and left borders with magick command

Is there a way to add 5 or any number of white/transparent pixels at the top and left borders of an image with the magick command in Linux?
Use the -splice operator. First make a solid magenta rectangle:
magick -size 100x50 xc:magenta image.png
Now splice on a yellow chunk (so you can see it) 10 wide and 20 tall:
magick image.png -background yellow -gravity northwest -splice 10x20 result.png
Change yellow to none for transparent pixels.
Change magick to convert for v6 ImageMagick.
If you just want to splice to the East side:
magick image.png -background yellow -gravity east -splice 10x east.png
If you just want to splice to the South side:
magick image.png -background yellow -gravity south -splice x10 south.png

How to position the -draw in imagemagick?

I am using imagemagick to draw a border on the top of an image.
THIS IS MY CODE:
convert source.jpg -stroke red -strokewidth 2 -fill transparent -draw \"roundrectangle 10,10 628,151 10,10\" source.jpg
This works fine but i need to be able to position the -draw where i want.
I tried to position the border like using -geometry like so:
convert source.jpg -stroke red -strokewidth 2 -fill transparent -geometry +5+15 -draw \"roundrectangle 10,10 628,151 10,10\" source.jpg
But this does not position it where i want. I also tried using -gravity and that doesn't work either!
Could someone please advise on this?
Thanks in advance.
Bonzo is correct, you cannot use -geometry with -draw.
In ImageMagick with -draw you can also translate to where you want the center to be and then specify +- distances to the corners from the center placement.
Suppose you have a 100x100 size box you want draw and you want it centered at 250,250, then
convert input.jpg -stroke red -strokewidth 2 -fill transparent -draw "translate 250,250 roundrectangle -50,-50 50,50 10,10" output.png
That makes it easier to draw the same size boxes at different locations.
see
http://www.imagemagick.org/Usage/draw/
http://www.imagemagick.org/script/magick-vector-graphics.php

Generating small text with Imagemagick is blurry

I'm trying to generate an image using Imagemagick to match a preview in the browser, but the text comes out blurry. Does anybody have any suggestions? Attached is an image with the Imagemagick one on top, and browser one on bottom, along with the IM code.
convert -density 288 -resize 25% -background white -fill black -strokewidth 0 -stroke white -font Rubik-Regular.ttf -pointsize 10 -gravity center label:'This is a TEST!' label_arial.gif
You might find it easier to start with caption which automatically sizes the text the best way to fill a given area. So, as your lettering is around 140x36 pixels, you would do:
convert -size 140x36 -gravity center caption:'This is a TEST!' label.gif

Imagemagick label issue

I am using imagemagick 6.2.8 and i want to add a label at the bottom of the image aligned on the right side.
this is my code:
convert image.png -gravity center -background "#f0f0f0" -font bgothm.ttf -pointsize 18 label:"text text" -append "append_image.png"
however the label is aligned to the left
How can i align the label to the right?
Edit:
I found this: Before IM v6.4.7 it was much more difficult to align appended images, and generally involved using a "-flop" for right alignment. Or using "-extent" or "-border" to adjust the image width for centered aligned appends.
But i never used imagemagick
To align bottom-right, use -gravity southeast.
convert image.png -gravity southeast -background "#f0f0f0" -font bgothm.ttf -pointsize 18 label:"text text" -append "append_image.png"
Edit: I have updated my solution to work with your ImageMagick version.
Do the double -flop trick. One flop to mirror the text, so it will be right aligned and one flop to mirror the append to make it readable:
convert image.png -background "#f0f0f0" -font bgothm.ttf \
-pointsize 18 label:"text text" -flop -append -flop "append_image.png"
Produces this image for me. Please note that the order of -flop -append -flop do matter!
But still, a better solution would be to upgrade ImageMagick to >= 6.4.7 and use -gravity SouthEast:
convert image.png -gravity SouthEast -background "#f0f0f0" -font bgothm.ttf \
-pointsize 18 label:"text text" -append "append_image.png"

Resources