Advertisement
OtterSou

im2braille

Jan 22nd, 2021
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. def im2braille(im,thres=128):
  4.     # converting PIL.Image image to braille pattern string
  5.     w = (im.width+2)//3
  6.     h = (im.height+4)//5
  7.     # convert to braille
  8.     im = im.convert('L')
  9.     res = [[0x2800 for c in range(w)] for r in range(h)]
  10.     ofs = {(0,0):1,(0,1):2,(0,2):4,(1,0):8,(1,1):16,(1,2):32,(0,3):64,(1,3):128}
  11.     for r in range(im.height):
  12.         for c in range(im.width):
  13.             if im.getpixel((c,r)) >= thres:
  14.                 res[r//5][c//3] |= ofs.get((c%3,r%5),0)
  15.     return '\n'.join(''.join(chr(i) for i in resr) for resr in res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement