"Python" python picture to character painting

Source: Internet
Author: User

#知识: #1) Pillow: Image processing #im = image.open (IMG)    open image #im.resize  set image size #image.nearest  Set image quality, lowest mass #im.getpixel  returns the pixel value at the given location # # #) Argparse: command-line parameter handling: It's plain to define the parameters followed by the script, which can be parsed automatically #创建解析对象 #parser =  argparse. Argumentparser () #add_argument: Add command-line arguments to follow #parser.add_argument (' file ')   #输入文件 #args =  Parser.parse_args ()   #获取参数 #pil Use the pillow Library, need to install, processing the image from pil import image# system comes with, processing command line parameters Import  argparse# creates a parse object parser = argparse. Argumentparser () #add_argument: Add command line Arguments parser.add_argument (' file ')   #输入文件parser. Add_argument ('-o ', '-- Output ')    #输出文件parser. Add_argument ('--width ', type = int,default=80)   # Output character draw parser.add_argument ('--height ', type = int,default = 80)   #输出字符画高args  =  parser.parse_args ()   #获取参数IMG  = args.fileWIDTH = args.widthHEIGHT =  Args.heightoutput = args.outputascii_char = list ("[email protected]%8&wm#*oahkbdpqwm ") #将256个灰度映射到10个字符上def  get_char (r,g,b,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)/lengthreturn ascii_char[int (gray/unit)]if __name__  ==  ' __main__ ': #打开图像对象 #im.mode  picture mode   im.size  picture size Im = image.open (IMG) # Resize: Set image size; Image.nearest: Set image quality, minimum quality im = im.resize ((width,height), image.nearest)   txt  =  "For i in range (HEIGHT): For j in range (WIDTH): # Im.getpixel returns the pixel value for the given position Txt += get_char (*im.getpixel (j,i)) txt +=  ' \ n ' print txtif  Output:with open (OUTPUT, ' W ')  as f:f.write (TXT) else:with open ("Output.txt", ' W ')  as  f:f.write (TXT)

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/20/472e7befd08be6c0786da2b4dfef60e0.png-wh_500x0-wm_3 -wmp_4-s_1652790371.png "title=" 1.PNG "alt=" 472e7befd08be6c0786da2b4dfef60e0.png-wh_ "/>

This article from "Kong Love to Eat fish" blog, declined reprint!

"Python" python 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.