Guest User

Untitled

a guest
Dec 23rd, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. import cv2
  2.  
  3.  
  4. class TextFactory:
  5.  
  6.     dict = {}
  7.     char_maxheight = 0
  8.  
  9.     def __init__(self):
  10.         self.import_from_image()
  11.  
  12.     def import_from_image(self):
  13.  
  14.         alphabet = list('abcdefghijklmnopqrstuvwxyz0123456789 @')
  15.  
  16.         img = cv2.imread('bitmap.png', cv2.IMREAD_GRAYSCALE)
  17.         height, width = img.shape
  18.         print(height, width)
  19.  
  20.         y1 = 0
  21.         y2 = 0
  22.         x = 0
  23.  
  24.         def read_char(cc, yy1, yy2, xx):
  25.             print(cc, yy1, yy2, xx)
  26.  
  27.             array1 = []
  28.             for yyy in range(yy1, yy2+1):
  29.                 array2 = []
  30.                 for xxx in range(0, xx+1):
  31.                     if img[yyy, xxx] == 255:
  32.                         array2.append('██')
  33.                     else:
  34.                         array2.append('▒▒')
  35.                 array1.append(array2)
  36.  
  37.             self.dict[cc] = array1
  38.  
  39.         for char in alphabet:
  40.             while True:
  41.                 pixel = img[y2, 0]
  42.                 if 0 < pixel < 255:
  43.                     break
  44.                 y2 += 1
  45.             while True:
  46.                 pixel = img[y1, x]
  47.                 if 0 < pixel < 255:
  48.                     break
  49.                 x += 1
  50.             read_char(char, y1, y2-1, x-1)
  51.             if y2 - y1 > self.char_maxheight:
  52.                 self.char_maxheight = y2 - y1
  53.             y2 += 1
  54.             y1 = y2
  55.             x = 0
  56.  
  57.     def print_all(self, text):
  58.         char_list = list(text)
  59.         column_bitmap_list = [''] * self.char_maxheight
  60.         for char in char_list:
  61.             for idx, val in enumerate(self.dict[char]):
  62.                 column_bitmap_list[idx] += ''.join(val) + '▒▒'
  63.         for col in column_bitmap_list:
  64.             print(''.join(col))
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. text1 = TextFactory()
  73. text1.print_all('abc')
Advertisement
Add Comment
Please, Sign In to add comment