def to_txt(self, sep="\n", tw=240, invert=False, threshold=200): # import pdb; pdb.set_trace() buf = io.BytesIO() self.print_png(buf) buf.seek(0) i = Image.open(buf) w, h = i.size ratio = tw / float(w) w = tw h = int(h * ratio) i = i.resize((w, h), Image.ANTIALIAS) i = i.convert(mode="L") can = Canvas() for y in range(h): for x in range(w): pix = i.getpixel((x, y)) if invert: if pix > threshold: can.set(x, y) else: if pix < threshold: can.set(x, y) for x, y, s in self.renderer.texts: can.set_text(int(x * ratio), int(y * ratio), s) return can.frame()
def test_set_text(self): c = Canvas() c.set_text(0, 0, "asdf") self.assertEqual(c.frame(), "asdf")