ImageMagick can't handle mixed font rendering - imagemagick

This is an example from Imagemagick that will render the chinese characters in the file if using the ZenKaiUni font.
convert -background lightblue -fill blue -pointsize 48 -font ZenKaiUni label:#chinese_words.utf8 label_utf8.gif
I have a situation where a Chinese font is mixed with English and would like to render it with the above command. Imagemagick is rendering the English text with "?" symbols. Is it possible to provide an English font also to the same command above or any other way to render the mixed languages text ?
Thanks .

Imagemagick is not a text editor and so its text functions are basic. Later versions come with Pango and you may be able to use that.

Related

Escape [0] in ImageMagick label

I am generating a number of imagemagick commands to write out labels, one of the labels I need to write is the following:
[0]
whenever I get to the command with this label it does not work.
This is the specific command
convert -background white -fill black -font Arial -pointsize 24 label:[0] -rotate 90 16.png
when I run this in my Mac terminal I get the message
convert: no images defined `16.png' # error/convert.c/ConvertImageCommand/3282.
I have of course tried to escape with a backslash but neither
label:[0]
or
label:\[\0\]
or
label:[\0]
or
label:\[0\]
work, and each gives the same error. Any suggestions?
Try:
convert -pointsize 36 label:'\[0\]' image.png
The single quotes prevent the shell from seeing/interpreting the square brackets as a bracket expression and the backslashes prevent ImageMagick from believing they are part of an fx-like expression.

Generating Low Resolution Black and White Text with Image Magick

I need to generate low resolution black and white images of texts in ImageMagick. These images will be plotted in a small LED matrix. The text need to have 7 pixels of height.
For now, I'm using:
convert -negate -threshold 15% -font Courier -size 80x11 caption:'hello' out.bmp
Output image:
Even with the height being more than I need, due to low resolution and anti-aliasing correction, the letters are not pretty and symmetric. Has anyone did this and can help me out?
Version: ImageMagick 6.8.9-9 Q16 x86_64
The solution I found was to use an TrueType font. Just got a free font from the internet and used it in the size it was built for.
P.S.: Switched for OpenCV as well. My Python app generates images dynamically. The cost for invoking ImageMagick several times(could get close to hundred) per minute is too high.
Posting a snippet, hope it helps.
import cv2 as cv
from PIL import ImageFont, ImageDraw, Image
# Creates a black image with 80x10 size
img = Image.new('RGBA', (80, 10), (0,0,0,0))
draw = ImageDraw.Draw(img)
# Load TrueType font of height size 8
font = ImageFont.truetype("font.ttf", 8)
# Draw text using the loaded font
draw.text((0, 0), "Hello World!", font=font)
img.save("out.bmp")
Output Image:
I would be inclined to output the letters larger than required, then to trim any extraneous spare space so as to make the most of the available resolution, then resize down to your specific needs:
convert -size 320x32 -font Courier label:'hello' -trim +repage -resize 80x8 +write out.gif
Mark, I think he wants a binary result. But you have an excellent idea.
Let's take Mark's result, threshold and then scaling down to 8 pixels tall. This ImageMagick command seems to work better than my earlier post.
Mark's Output:
convert wcwuj.gif -threshold 60% +write thresh.gif -scale x8 result.gif
Threshold Result:
Scaled Result:
Perhaps making Mark's image much larger and choosing a better threshold will produce a better result.
You have not told us what version of ImageMagick nor platform and you do not show your result for us to see what might be wrong. Also your ImageMagick syntax is not proper, though ImageMagick 6 is rather forgiving.
This is what I get using ImageMagick 6.9.10.8 Q16 Mac OSX Sierra. The first output is 8 pixels tall and the second output is scaled by 1000% (10x).
This forum does not seem to convert bmp to a usable format for display, so I am using GIF in place of BMP. But my results look the same whether BMP or GIF
convert -size x8 -font Courier label:'hello' -negate -threshold 20% +write out.gif -scale 1000% out2.gif
I have tried changing threshold, but much larger or smaller values make it worse. A range from about 10-30% produces the same results.
I have also tried using -monochrome in place of -threshold and get the following:
convert -size x8 -font Courier label:'hello' -negate -monochrome +write out3.gif -scale 1000% out4.gif
You might try a dot-matrix type font. See https://www.1001fonts.com/digital+dot-matrix-fonts.html?page=1&items=10. I have not tried any of them.
You could try some of the old X11 fonts. These were hand-drawn rather than being rendered from a set of curves, so they look good at very small sizes.
For example, if I run xfontsel I get things like this (enlarged for clarity):
Take a look in /usr/share/fonts/X11/misc.

convert text with leading space to image using imagemagick

I'm trying to convert a text label into image using imagemagick's convert util following the guide at official page
I'm using the following command.
convert -background lightblue -pointsize 72 label:" spaces " label.gif
Although my text as leading whitespace, the output image has no space in the front. Spaces are getting trimmed. Trailing spaces have no issue though. I've tried the solution given at this SO question too in vain.
While I had typed this question and was waiting to post it, I figured out a way by guessing ;-)
The solution is simply to escape the first space with blackslash.
convert -background lightblue -pointsize 72 label:"\ spaces " spaces.gif

Imagemagick: Draw text in a bounding box with long ascenders and descenders

I've got a bit of a weird question and, after parsing through ImageMagick's extensive documentation for hours, I cannot find an answer.
I have a program that creates text boxes within larger images. My current solution uses label: and caption: to create a temporary image sized appropriately, then later composites this image into the larger image. This has worked for me up until now.
convert \
-size 321x93 \
-font Carolyna-Pro-Black-Regular \
-pointsize 43 \
-fill #808080 \
-gravity center \
label:"Carolyna Pro Black\nis an obnoxious font." \
/tmp/generator20140421-7999-157xadf.png
However, recently I added some new fonts, and these fonts are a pain to work with because they have rather large ascenders and descenders (loopy, curly bits above and below the text.) In the case of these fonts, these ascenders and descenders are supposed to overflow their bounding box. I've tried a few things, but none produce the desired effect:
Making the bounding box larger: It seems like this would work, but when the -gravity is set to North, the text moves all the way to the top and clips the top anyway.
Various forms of the -draw command: These don't give me the ability to use -gravity to align my text within the bounding box, and don't seem to work with multi-line text.
Not specifying a -size argument and cropping the image later. This might be the only option, but it will take a lot of manual computation to get it to align text correctly, and adds an extra step to the process.
Essentially, my problem is that I need the text aligning capabilities that label: provides, but I don't want my text to be unnecessarily cut off.
Any ideas?
The solution turned out to be as simple as adding a newline character before and after the text, and adding the height of the text to the top and bottom of the bounding box.

ImageMagick multiline text and background image

I'm learning to use ImageMagick, but I'm having trouble when I try to generate an image like the example below:
And for that, I'm using the following code:
convert original.jpg -size 460x caption:'This is a multiline caption, This is a multiline caption, This is a multiline caption.' result.jpg
This command generated two separate images, one with just the title. Can you help me?
PS: I'm using RubyOnRails with ImageMagick, but you can suggest examples with MiniMagick or Rmagick. Thanks.
This works in php and I can not see why it will not work o RubyOnRails:
convert original.jpg -size 460x -background transparent -fill black -pointsize 40 caption:'This is a multiline caption, This is a multiline caption, This is a multiline caption.' -gravity center -composite result.jpg

Resources