def draw_baselines(self, img_in): img_out = img_in.copy() draw = ImageDraw.Draw(img_out) prev_floor = 0 w, h = img_out.size gap_color = ImageColor.getrgb('#ccccFF') lines = scrape.calc_lines(scrape.font_metrics(), 7) for i, (ceiling, base, floor) in enumerate(lines): # draw the baseline color = ImageColor.getrgb("#ffdddd") if not base: base = floor -2 color = ImageColor.getrgb("red") draw.line([(0, base), (w, base)], fill=color) # shade gap between the lines draw.rectangle((0, prev_floor, w, ceiling), fill=gap_color) prev_floor = floor +1 # shade final gap: draw.rectangle((0, prev_floor, w, h), fill=gap_color) # now draw the text back over the baselines: img_out = ImageChops.darker(img_in, img_out) # uncomment to zoom in for debugging # img_out = img_out.resize((w *2 , h* 2)) return img_out
def recognize(self, img): lines = scrape.calc_lines(scrape.font_metrics(), num_lines = 6) res = [] for (ceil, base, floor) in lines: line_img = img.crop((0, ceil, img.size[0], floor + 1)) res.append(scrape.str_from_img(line_img, self.font, self.predicate(line_img))) return "\n".join(res)