Example source experimental Building website, nothing can be more around, thank you in this experiment building selfless sharing
1 fromPILImportImage2 ImportArgparse3 4 """5 Description:6 convert picture pixels to letters redraw output7 """8 9Ascii_char = List ("[Email protected]%8&wm#*oahkbdpqwmzo0qlcjuyxzcvunxrjft/\| () 1{}[]?-_+~<>i!li;:,\ "^". ")Ten OneParser =Argparse. Argumentparser () A -Parser.add_argument ('file') -Parser.add_argument ('- o','--output') theParser.add_argument ('--width', type=int,default=80) -Parser.add_argument ('--height', type=int,default= 80) - -args =Parser.parse_args () + -IMG =Args.file +WIDTH =Args.width AHEIGHT =Args.height atOUTPUT =Args.output - - defGet_char (R,g,b,alpha = 256): - ifAlpha = =0: - return ' ' -Length =Len (Ascii_char) inGray = Int (0.2126 * r + 0.7152 * g + 0.0722 *b) -Unit = (256.0 + 1)/length to returnAscii_char[int (gray/unit)] + - defMain (): theim =Image.open (IMG) *im =im.resize ((width,height), image.nearest) $TXT =""Panax Notoginseng forIinchRange (HEIGHT): - forJinchRange (WIDTH): theTXT + = Get_char (*Im.getpixel ((j,i))) +TXT + ='\ n' A Printtxt the + if __name__=="__main__": -Main ()
Store the image to be converted in the current directory where the file is located, execute the command directly: Python convert.py test.png
Python Small project Practice convert pixel picture to character metalized