If I understand correctly, you want the image to look like someone took a screenshot of the ascii art, as it will look in a giant unlimited text editor.
- , . , . , , , , .
, .

.
import PIL
import PIL.Image
import PIL.ImageFont
import PIL.ImageOps
import PIL.ImageDraw
PIXEL_ON = 0
PIXEL_OFF = 255
def main():
image = text_image('content.txt')
image.show()
image.save('content.png')
def text_image(text_path, font_path=None):
"""Convert text file to a grayscale image with black characters on a white background.
arguments:
text_path - the content of this file will be converted to an image
font_path - path to a font file (for example impact.ttf)
"""
grayscale = 'L'
with open(text_path) as text_file:
lines = tuple(l.rstrip() for l in text_file.readlines())
large_font = 20
font_path = font_path or 'cour.ttf'
try:
font = PIL.ImageFont.truetype(font_path, size=large_font)
except IOError:
font = PIL.ImageFont.load_default()
print('Could not use chosen font. Using default.')
pt2px = lambda pt: int(round(pt * 96.0 / 72))
max_width_line = max(lines, key=lambda s: font.getsize(s)[0])
test_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
max_height = pt2px(font.getsize(test_string)[1])
max_width = pt2px(font.getsize(max_width_line)[0])
height = max_height * len(lines)
width = int(round(max_width + 40))
image = PIL.Image.new(grayscale, (width, height), color=PIXEL_OFF)
draw = PIL.ImageDraw.Draw(image)
vertical_position = 5
horizontal_position = 5
line_spacing = int(round(max_height * 0.8))
for line in lines:
draw.text((horizontal_position, vertical_position),
line, fill=PIXEL_ON, font=font)
vertical_position += line_spacing
c_box = PIL.ImageOps.invert(image).getbbox()
image = image.crop(c_box)
return image
if __name__ == '__main__':
main()
, , , , , , , .