Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- class TextFactory:
- dict = {}
- char_maxheight = 0
- def __init__(self):
- self.import_from_image()
- def import_from_image(self):
- alphabet = list('abcdefghijklmnopqrstuvwxyz0123456789 @')
- img = cv2.imread('bitmap.png', cv2.IMREAD_GRAYSCALE)
- height, width = img.shape
- print(height, width)
- y1 = 0
- y2 = 0
- x = 0
- def read_char(cc, yy1, yy2, xx):
- print(cc, yy1, yy2, xx)
- array1 = []
- for yyy in range(yy1, yy2+1):
- array2 = []
- for xxx in range(0, xx+1):
- if img[yyy, xxx] == 255:
- array2.append('██')
- else:
- array2.append('▒▒')
- array1.append(array2)
- self.dict[cc] = array1
- for char in alphabet:
- while True:
- pixel = img[y2, 0]
- if 0 < pixel < 255:
- break
- y2 += 1
- while True:
- pixel = img[y1, x]
- if 0 < pixel < 255:
- break
- x += 1
- read_char(char, y1, y2-1, x-1)
- if y2 - y1 > self.char_maxheight:
- self.char_maxheight = y2 - y1
- y2 += 1
- y1 = y2
- x = 0
- def print_all(self, text):
- char_list = list(text)
- column_bitmap_list = [''] * self.char_maxheight
- for char in char_list:
- for idx, val in enumerate(self.dict[char]):
- column_bitmap_list[idx] += ''.join(val) + '▒▒'
- for col in column_bitmap_list:
- print(''.join(col))
- text1 = TextFactory()
- text1.print_all('abc')
Advertisement
Add Comment
Please, Sign In to add comment