# code from the lab building https://www.shiyanlou.com/courses/
#encoding =utf-8
#/usr/bin/env python
From PIL import Image
Import Argparse
Parser = Argparse. Argumentparser ()
Parser.add_argument (' file ')
Parser.add_argument ('-O ', '--output ')
Parser.add_argument ('--width ', Type=int, default=50)
Parser.add_argument ('--height ', Type=int, default=50)
args = Parser.parse_args ()
IMG = Args.file
Width= Args.width
height= Args.height
output= Args.output
Ascii_char = List ("[Email protected]%8&wm#*oahkbdpqwmzo0qlcjuyxzcvunxrjft/\| () 1{}[]?-_+~<>i!li;:,\ "^". ")
def Get_char (R, B, G, alpha=256):
If Alpha ==0:
Return '
length = Len (Ascii_char)
gray= Int (0.2126 * r + 0.7152*g + 0.0722*b)
Unit = (256.0 +1)/length
Return Ascii_char[int (Gray/unit)]
if __name__ = = ' __main__ ':
im = Image.open (IMG)
im = Im.resize ((WIDTH, HEIGHT), image.nearest)
txt = ""
For I in Range (HEIGHT):
For j in Range (WIDTH):
TXT + = Get_char (*im.getpixel ((J, I))
txt + = ' \ n '
If OUTPUT:
With open (OUTPUT, ' W ') as F:
F.write (TXT)
Else
With open (' Output.txt ', ' W ') as F:
F.write (TXT)
A simple case of Python Pillow