def text_png(text, scale=3):
"""Returns an open png image file with text written
Only letters, numbers, -, >, and < are allowed. New lines are
indicated by /
"""
text = text.split('/')
text_out = ['']
for t in text:
text_line = ['1', '1', '1', '1', '1']
for letter in t:
for i in range(5):
text_line[i] += LETTERS[letter.upper()][i] + '1'
text_out += text_line
text_out += ['']
text_len = max([len(t) for t in text_out])
text_out = [line + (text_len-len(line))*'1' for line in text_out]
img = []
for line in text_out:
im_line = ''
for n in line:
im_line += scale*n
img += scale*[im_line]
png_file = NamedTemporaryFile('wb+', suffix='.png')
png_writer = png.Writer(len(img[0]), len(img), greyscale=True, bitdepth=1)
png_writer.write(png_file, img)
png_file.seek(0)
return png_file
评论列表
文章目录