Braille

The Unicode Braille alphabet gives us an opportunity to set pixels in terminal. A Braille character is a 2x4 grid of dots.

The Drawille library takes care of that. I think it was originally written for Python.. but now there are versions for many programming languages.. including Ruby.

One of the examples shows how to convert an image to a stencil, using chunky_png.

So I wrote some short code to create a PNG-image with whatever text is comming from ARGV. Then ChunkyPNG walks the pixels and creates a Drawille Canvas.

Looks like this:

require 'drawille'
require 'chunky_png'

include ChunkyPNG

canvas = Drawille::Canvas.new


def create_image(str)
  system %(magick -background none -fill white -pointsize 32 label:"#{str}" out.png)
end

def draw canvas, img, xoffset=0
  (0..img.dimension.width-1).each do |x|
    (0..img.dimension.height-1).each do |y|
      c = %i(r g b).each.inject(0) {|cnt, col| cnt += Color.send(col, img[x,y]) }
      canvas.set(x+xoffset, y) if c > 100 
    end
  end
end

abort('Supply text') if ARGV.empty?
text = ARGV.join(' ')

create_image text
draw canvas, Image.from_file('./out.png')

puts canvas.frame

Works like this:

ruby braille.rb Tubsocka

tubsocka