1 ImportArgparse2 fromPILImportImage3 4 defParse_param ():5Parser = Argparse. Argumentparser ()#command Line input parameter handling6 7Parser.add_argument ("Input_file")#input File8Parser.add_argument ("Out_file")#Output File9 TenParser.add_argument ("--width", Type=int, default=50)#output character Draw width OneParser.add_argument ("--height", Type=int, default=50)#High Output character drawing A -args = Parser.parse_args ()#Get Parameters -width, height, input_file, out_file =args.width, Args.height, Args.input_file, Args.out_file the returnwidth, height, input_file, out_file - - defGet_char (R, G, B, alpha=256): - " " + gray/256 = X/len (Ascii_chra) - + " " A ifAlpha = =0: at return " " -Gray = (2126 * r + 7152 * g + 722 * b)/10000 - #map 256 grayscale to 70 characters -Ascii_char = List ("[Email protected]%8&wm#*oahkbdpqwmzo0qlcjuyxzcvunxrjft/\| () 1{}[]?-_+~<>i!li;:,\ "^". ") -x = Int ((Gray/(Alpha + 1.0)) *Len (Ascii_char)) - returnAscii_char[x] in - defwrite_file (out_file_name, content): toWith open (Out_file_name,"W") as F: + f.write (content) - the defMain (file_name="test.jpg", width=80, height=80, out_file_name="Out_file"): *Text ="" $im =Image.open (file_name)Panax Notoginsengim =im.resize (width, height), image.nearest) - forIinchRange (height): the forJinchRange (width): +Content =Im.getpixel ((J, i)) AText + = Get_char (*content) theText + ="\ n" + Print(text) -Write_file (out_file_name, text)#the characters are drawn to the file $ $ - if __name__=='__main__': - Main () the - Wuyi the Picture test: -Http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png
To be perfected ....
Python3----exercises (picture to character painting)