Python3----exercises (picture to character painting)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.