4

I'm in the process of writing a library that will print scrolling text across an LED matrix hooked up to a Raspberry Pi.

I would like a function that takes a character, height and width and produces a matrix with the pixels that should be set in order to represent the specified character.

e.g.

def char_to_pixels(char, height, width):
    matrix = [width][height]

    # determine which pixels to set based on character supplied
    ...

    return matrix

So for a call to char_to_pixels('I', 6, 6) you might expect a matrix like the following:

|0|1|1|1|1|0|
|0|0|1|1|0|0|
|0|0|1|1|0|0|
|0|0|1|1|0|0|
|0|0|1|1|0|0|
|0|1|1|1|1|0|

I could try and define each of these for [A-Z] but I wondered whether there was a library out there that already did this kind of thing?

Alex
  • 5,364
  • 9
  • 54
  • 69

2 Answers2

9

Using PIL, this might be a solution. One difficulty, however, is that you do not get to directly control the width and height of the array. Instead you control the font and fontsize. If your intended usage is for a particular LED matrix, then this might be alright -- just find the right font and fontsize for your LED matrix.

Below I've modified jsheperd's answer so char_to_pixels returns a binary NumPy array. The display function converts the binary array to a character array just to make the result easier to see.

from __future__ import print_function
import string
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import numpy as np

def char_to_pixels(text, path='arialbd.ttf', fontsize=14):
    """
    Based on https://stackoverflow.com/a/27753869/190597 (jsheperd)
    """
    font = ImageFont.truetype(path, fontsize) 
    w, h = font.getsize(text)  
    h *= 2
    image = Image.new('L', (w, h), 1)  
    draw = ImageDraw.Draw(image)
    draw.text((0, 0), text, font=font) 
    arr = np.asarray(image)
    arr = np.where(arr, 0, 1)
    arr = arr[(arr != 0).any(axis=1)]
    return arr

def display(arr):
    result = np.where(arr, '#', ' ')
    print('\n'.join([''.join(row) for row in result]))

for c in string.uppercase:
    arr = char_to_pixels(
        c, 
        path='/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf', 
        fontsize=9)
    print(arr.shape)
    display(arr)
    print()

yields

(7, 7)
  ##   
  ##   
 # ##  
 # ##  
 ####  
#   ## 
## ####

(7, 7)
#####  
 ## ## 
 ## ## 
 ####  
 ## ## 
 ## ## 
#####  

(7, 7)
  #### 
 ##  # 
 ##    
 ##    
 ##    
 ##  # 
  #### 

(7, 7)
#####  
 ## ## 
 ## ## 
 ## ## 
 ## ## 
 ## ## 
#####  

(7, 6)
##### 
 ## # 
 ##   
 ###  
 ##   
 ## # 
##### 

(7, 5)
#####
 ## #
 ##  
 ### 
 ##  
 ##  
#### 

(7, 8)
   #### 
  ##  # 
 ##     
 ##     
 ## ####
  #  ## 
   #### 

(7, 7)
### ###
 ## ## 
 ## ## 
 ##### 
 ## ## 
 ## ## 
### ###

(7, 4)
####
 ## 
 ## 
 ## 
 ## 
 ## 
####

(7, 5)
 ####
  ## 
  ## 
  ## 
  ## 
# ## 
###  

(7, 7)
#### ##
 ##    
 ## #  
 ####  
 ## ## 
 ## ## 
#### ##

(7, 6)
####  
 ##   
 ##   
 ##   
 ##   
 ## # 
##### 

(7, 8)
##  ####
##  ### 
###  ## 
# ## ## 
# ## ## 
# ## ## 
# # ####

(7, 7)
### ###
 ### # 
 ### # 
 # ### 
 # ### 
 #  ## 
###  # 

(7, 7)
  ###  
 ## ## 
 ## ## 
 ## ## 
 ## ## 
 ## ## 
  ###  

(7, 5)
#### 
 ## #
 ## #
 ### 
 ##  
 ##  
#### 

(9, 8)
  ####  
 ##  ## 
 ##  ## 
 ##  ## 
 ##  ## 
 ##  ## 
  ####  
    ##  
     ## 

(7, 7)
#####  
 ## ## 
 ## ## 
 ####  
 ## #  
 ## ## 
#### ##

(7, 5)
 ### 
## # 
###  
 ##  
  ## 
# ## 
###  

(7, 6)
######
# ## #
  ##  
  ##  
  ##  
  ##  
 #### 

(7, 7)
#######
 ##  # 
 ##  # 
 ##  # 
 ##  # 
 ##  # 
  ###  

(7, 6)
### ##
##  # 
 ## # 
 ##   
 ###  
  ##  
  ##  

(7, 9)
###    ##
 ## ## # 
 ## ## # 
 ## ##   
  ## ##  
  ## ##  
  ## ##  

(7, 6)
######
 ## # 
 ###  
  ##  
  ### 
 # ## 
######

(7, 6)
######
 ## # 
 #### 
  ##  
  ##  
  ##  
 #### 

(7, 5)
#####
# ###
  ## 
 ##  
 ##  
##  #
#####

I've included the shape of the arrays above the characters so you can see if they would fit on the LED matrix.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

I guess this might help if you are using MAX7219: https://github.com/rm-hull/max7219 At least you can get an ideia