MCPcopy Create free account
hub / github.com/Topdu/OpenOCR / render_normal

Method render_normal

openrec/preprocess/cam_label_encode.py:65–114  ·  view source on GitHub ↗
(self, font, text)

Source from the content-addressed store, hash-verified

63 self.font.oblique = False
64
65 def render_normal(self, font, text):
66 # get the number of lines
67 lines = text.split('\n')
68 lengths = [len(l) for l in lines]
69
70 # font parameters:
71 line_spacing = font.get_sized_height() + 1
72
73 # initialize the surface to proper size:
74 line_bounds = font.get_rect(lines[np.argmax(lengths)])
75 fsize = (round(2.0 * line_bounds.width),
76 round(1.25 * line_spacing * len(lines)))
77 surf = pygame.Surface(fsize, pygame.locals.SRCALPHA, 32)
78
79 bbs = []
80 space = font.get_rect('O')
81 # space = font.get_rect(' ')
82 x, y = 0, 0
83 for l in lines:
84 x = 2 # carriage-return
85 y += line_spacing # line-feed
86
87 for ch in l: # render each character
88 if ch.isspace(): # just shift
89 x += space.width
90 else:
91 # render the character
92 ch_bounds = font.render_to(surf, (x, y), ch)
93 # ch_bounds.x = x + ch_bounds.x
94 # ch_bounds.y = y - ch_bounds.y
95 x += ch_bounds.width + 5
96 bbs.append(np.array(ch_bounds))
97
98 # get the union of characters for cropping:
99 r0 = pygame.Rect(bbs[0])
100 rect_union = r0.unionall(bbs)
101
102 # get the words:
103 # words = ' '.join(text.split())
104
105 # crop the surface to fit the text:
106 bbs = np.array(bbs)
107 surf_arr, bbs = crop_safe(pygame.surfarray.pixels_alpha(surf),
108 rect_union,
109 bbs,
110 pad=5)
111 surf_arr = surf_arr.swapaxes(0, 1)
112
113 # self.visualize_bb(surf_arr,bbs)
114 return surf_arr, bbs
115
116 def __call__(self, data):
117 data = super().__call__(data=data)

Callers 1

__call__Method · 0.95

Calls 1

crop_safeFunction · 0.85

Tested by

no test coverage detected