1

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:

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

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • What's the real issue? Are you using a Raspberry Pi and trying to write on an LED matrix? Please be more specific about what you actually want to do. – Mark Setchell Aug 01 '18 at 19:21
  • The issue is that the letters are not properly formatted in the output image. The app will run on a SoM, but I'm running it on my developer station for now. – Rafael Nicolay Aug 01 '18 at 19:41
  • You may be able to get clearer text by defining your own font - it's not too hard... https://stackoverflow.com/q/2156572/2836621 – Mark Setchell Aug 01 '18 at 21:53

5 Answers5

3

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:

Output Image

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
1

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

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Problem is that after applying a threshold(pixels need to be white or black, or on or off) the letters aren't getting symmetric. Don't know if the approach I'm using will work. – Rafael Nicolay Aug 01 '18 at 20:26
1

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:

enter image description here

convert wcwuj.gif -threshold 60% +write thresh.gif -scale x8 result.gif


Threshold Result:

enter image description here

Scaled Result:

enter image description here

Perhaps making Mark's image much larger and choosing a better threshold will produce a better result.

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

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


enter image description here

enter image description here

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


enter image description here

enter image description here

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.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Sorry, I updated the question. And yes, something I will try is using other font. Don't know if the approach I chose will supply my needs. Maybe I should generate the image using another way, like opencv. Thanks. – Rafael Nicolay Aug 01 '18 at 20:29
  • I doubt OpenCV will create a better image. The main issue is that you are trying to directly create a tiny image that is too small for the point sizes available to your font. Mark Setchell's idea of creating a much larger image, then thresholding and then scaling the result down to your 8 px tall result, seems the best idea to me. But by all means try OpenCV as well. No harm in trying. To improve this, I think your best bet would be to find an LED type matrix font. – fmw42 Aug 01 '18 at 22:06
0

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):

enter image description here

Take a look in /usr/share/fonts/X11/misc.

jcupitt
  • 10,213
  • 2
  • 23
  • 39