Python图片转换成字符画

今天看见一个很有意思的脚本,用py实现把一副图片转换成字符画

Code:

usage:python pic2str.py xxx.jpg

# coding:utf-8  

#  为一张图片生成对应的字符集图片  

import Image  
import argparse  

# 命令行输入参数处理  
parser = argparse.ArgumentParser()  

parser.add_argument('file')     # 输入文件  
parser.add_argument('-o', '--output')   # 输出文件  
parser.add_argument('--width', type=int, default=140) # 输出字符画宽  
parser.add_argument('--height', type=int, default=90) # 输出字符画高  

# 获取参数  
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;:,\"^`'. ")  


# 将256灰度映射到70个字符上  
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'  

    print txt  

    # 字符画输出到文件  
    if OUTPUT:  
        with open(OUTPUT,'w') as f:  
            f.write(txt)  
    else:  
        with open("output.txt", 'w') as f:  
            f.write(txt)  

试着转换了下头像233

                                                         $                   .     '                                                   
                                       .       $                 @                  @                                                       
                                       ^$`       $$       B       ^     .^   $      @     .                                                 
                                        .$"        <$      @      @      $   $       $          `                                           
                                  $       [email protected]'      ..$      $     '$    .$   $      '@    #      $                                          
                              $'.   $       $B'       $$.    $     @    .$   $.      $    $      $                                          
                                $   '$'       @@$^      8`    `     $'   @   ^         ^  $      $                                          
                                  $$.'[email protected]     .$$      ^@   .8 `   $.    $   B       $   @      $     '                                    
                                    8$. $ '      $$^     '$    $     $   .$   B       $   @      $     $                                    
                       @.             $. @$       '%       $^  [email protected]   $.  .%   $       @'  @      $     B.                                   
                         $$            $  `$      '`$      $$   $'    $     . 'B      $   @.     $      '       .                           
                           $$           $$` B        @     `$"  $>   ^B.  '$'  $.     $    '     $             $`                           
                             .$%         $% "$"    $  $     $B   $    @$   B   B     B    .      $      '                                   
                               ^'%$       $$ `$     $  $.    @@  [email protected] .  $  `$.  "$   .$    .$     $            $                             
                                   $$$B     @'`[email protected]    h  @`    @.  $  ` 'B  $B   $    $     $     :     $     .$.                            
                                      [email protected]    '$.`$$   .`  $'   B$   [email protected]  $   @   $.   %     @    '      $     $      $"                      
              .W$'.......             ^ $%       $$   B   $^  .$$   8$ .$% '$`    `^ $          .      $     @     ^$                       
                 . .... . @$B$$$         .%   '[email protected]  $B' %   $.^  $  .$$$ $$B $$M  $`. $    :     B     :$    .$     $'    '$.                
                           .. ''@$   $    .$$  `@@ '.$$$. [email protected] ' [email protected] !$  `$$' $   $$ .$    $     $      $    $     <B     $.                 
                                ` B$$ .$    @@@  $B  . $$    '   $'  $  '$   $   $$  @.   $           $    '$     @`      '                 
                                     $$$B    [email protected]   @$   $B  [email protected]   $$  $B  [email protected]  @   @L  $    $    @     .$.   .$    $      $                   
                                       [email protected]$$   @$$  [email protected]   $  '$   r.  $&  $Y  $   $ .$$   [email protected]$  ,$ ;   BZ     $         [email protected]                    
             [email protected][email protected]  .   $B$B ' `..'      $$$    $`  ^$   $q '@    $  $   @`  B. [email protected]  $    $^   .     $$'    $^   $     $`                    
                       '      k$$W$$B.     $$   `B`  [email protected]  $  $$  $W  $B  $$  $  $   $    @    [email protected]  `$     $   '8     $"     '.              
                                   m$;B$ ^  $Bm`  $'  $   $% '$  $$  $   $$  %  $" $    @&   [email protected]  . @    .$B^  '$.  'B      ..               
                     "  .              [email protected]    $$. $B.'$`' $$$ $B. .' $'  $* $c Q$ '$    $`  $$.   $    .$$  ' . '  $`    [email protected]                 
          ,[email protected][email protected][email protected]$$$$^        ' $$   $$  [email protected]' @   8  %U'@   `^  $M $$ $$ $B   $   88^ . @Q   '$    ^Bm   Bp   ..$                  
                             [email protected]$$$$        ^BB`. `  $.'$   @  $B ' ' .  [email protected]'"$+ $'.$$  .$  @`   [email protected]     $.    $.   @$   `$                    
                                 ``[email protected]%$$    $$   $  $  @.  ' $'  `   C  $    .t .$'  [email protected] $$'  [email protected]`   '$$   $$     $   `B.                    
                      @ $$$$$$$$$.`  .  $$$   @)%.`@ [email protected] '. ' ' }'$$$ @@@$%$$$$$   @  $ ' "   B$.   $    $B    "BB   $%                      
               $%$$$$  $$         [email protected]' ' $.$%   $ 'a "$$$$'$ @@%$$Y1 [email protected] [email protected]$    -' .`  $B  '.$$' .$$   . $$   $$.     .B               
         "8$&                      '^ $$  ...$$ .^$$`$W$%$B$o$$$$$$'[email protected]$$$$B1^<$'  ' 'l$`   $:   $$$    [email protected]'   $$.     $ .               
          '                            $%$  . ^<[email protected][email protected] u$$$$$$$$$$$$$$$$$$$$$$$$$$$$'r$1$ "`  ` .'  $B$     $     $ "    C$.                 
                                        ' @$'  $'UZ$B$$$$$$$$$$$$$v [email protected][email protected]  b   .$ '"  $$$'  B$      '$$                   
                                             @Y$$$$$$$$$$$$$$$$$$$ @[email protected]$rB$z  .     . $. ' $$     ''@$                     
                                             [email protected]$$$1 B$&hB$`.'   'B.   .$$      @$'                      
                                           @[email protected]&$bX$$  Zu  '[email protected]$      %$$           .            
                                         '[email protected]]$$$$$$$$$$$$$$$qcz#0IBj;M t$^~$      $ `     Y$q$           $'            
                                         $$$$$$$$$$$$$$$$$$$$$$c [email protected]$%%$8B%[email protected]%$$$$$1%[].v)<+x'bB  a   d]' ' [email protected]          . @$"              
                                        $$$$$$$$$$$$$$$$$ ..     ' .I};czf')[email protected]$j$a.$u #.uhu W|  . ^M $$             @$                  
                                      .$$$$$$$$$$$$$$$$WY     ('. [email protected]@[email protected]{@b~B$$$Q*$+q.'J\]c`-$    -.       '  ^ $$`         .          
                                     '$$$$$$$$$$$$$$$$$  `' '[email protected]$$$]$$$ $$$"[email protected]({%@@aC`:w~$`}$ `'U      #%$B$$$$&B$^      `'[email protected]$           
                                     B$$$$$$$$$$$$$$B$h  ' 8$$B'&W$ $$  [email protected][email protected]`$,:%u'$+'Cm~ +z   @+`{ ."           [email protected]@                
                                     $$$$$$$$%$$$.$$B  . 'x$$$$l$$$$$$B$$8 $$$$$$$$$$-q-k,.B~n -'  x.              .`$$$  ''                
                                      B$$$&$$$   @@ 'r.  `B1$$Z$#@$$$$$$%[email protected]@$$$$Cq(  .,` . .$ ` |;$ [email protected] [email protected]$$                       
                                      ,8$$$      B$$. $ .&[email protected]$ [email protected][email protected]   '''.."    '^` '  $$ ^                        
                                       $$ '.     '$$$$w'[email protected][email protected][email protected]$$B$$$Q$%$$.r  " '}^>uxB`                                    
                                      $}$$$k.     $BB$$..X'[email protected]$$$$$$$$$$$B$$XB$$$$$Z]#M$1n'`^. _}.bX ^}X'@$$$$  "B                        
                                  '$$$$$$$$$$-   [email protected] .  h].Q:-'"[email protected]@$$$$o [email protected]@$%wj$\0$$$>a':.'+>         .   $q+%[email protected][email protected] l         
                                  $$$$$$B `$_  [email protected]$$$$ .....f($Uo$fB\ [email protected]  ' @  $B$$$$$r.  /]'. `  !.n?b$$``   .                           
                                     $$$$$ B  [email protected]$ r    '.x0      1  $w [email protected]$\[email protected]$$$$b|[email protected] r'  .'B '.  [email protected]$$$.. .                       
                                     [email protected][email protected]$$$.  $^$$$$$ Y'    .8 @$$  [email protected]$$z\[email protected]$$Zp0<  x  ^ I$  [~        %[email protected]$  .                    
                                     [email protected]@'X [email protected]@B$$$$$ ...' [email protected]$m%` ^;'oBBQp.lC. U"_^^ '# .  [email protected]`         [email protected]$ '..               
                                     $$$$$^.  'c%$$$' [email protected][email protected]`' @[email protected]$ ...   ^ $$$$$$$?t$$.$c}t<  .   ~_ ~    ]$o             $B$8  .          
                                .   .$$$$$$ .$  b}  <   q [email protected]$$ $B$.$$    [email protected] $vB)cW'.$uzU*1.. `'   $   ., B$$                           
                                 8$$$$$$$$$ .o$$ '+. .  '{$ > $B $$ . . `@ @Bk'B$.hf$$$QWj] !. (`^.  .  '$$     [email protected] .                       
                                  $$%$$$\[email protected] @@@:    $ [email protected]      '$$vb$ `[email protected]$$$zf.)Q].I/Z   .   c' _: .  @$o      '$$                      
                                 [email protected]$$'$B^@[email protected]@ [email protected]$B.$ $      $*[email protected][email protected]$.%'[email protected]$(&Ji$&:` ^ur' 0`k '  ^     $B.       '$$                  
                                 _.  $8$$[$B '[email protected] $ $$$$$`$ $$    [email protected] {w$$  ` .> .>..+p`\'n [email protected]  [email protected]   ` $$'       '$$'               
                                $p$$.$Z ^$M8$.$$$$$a0.$$   %[email protected]%@$$.$ $ %. @ [email protected]'   . '. 0kvqjr8.?$  w' >$$      $$$                        
                               8$ $^ [email protected]  $ . $$  $$ $.$$   $$$$$$$('    $ . @$$_.. ^  j0$  '^o{$'I$  \  ''$.    '$$$.                      
                               $%@$$$%<  $$Z  "$$$'[email protected]$  $   8$$$$$'l           [email protected]$u`Lp$ '.      $   $` $.  .$B     . $B                     
                              $$`[email protected]$$$  .$$.   $$ `$$      '$$$$$f.$$           " &@           `$  $B  @    $$      . $"                   
                              $$ $$ $$    $'    '$  ' $'      $$$$$?+q$                         . $  Y   $    [email protected]$'     `"[email protected]                 
                              $  $ .$$    $.     '@            $B$$$h.$%.                        . $  .B 'B '   Z$.                         
                                 $  @     @#                    $$$$$<[email protected]                         $$   B^ 'BB     $.                        
                                    $      $`              ..   $B$$$.n$$                         [email protected]^ ` ' '@      .                        
                                           $.               $''$$$$$$$$$$B                          $,  $    $ `    .                       
                                                          [email protected][email protected]$$$$$i                             B$.  $    $      .                      
                                                 '       [email protected]$ $ $$$$`$$                              @.       '$.                           
                                                 @      [email protected]$$ @ @$$: $$                              8   .$     $                           
                                                 $B  . .$$ .$%$z$$Y$.$.                              `$.  .$      '                         
                                                  @  $ $$B %$ [email protected]                                   $`    '                               
                                               $. @$_"$ [email protected]$$$C%.                                   .$                                    
                                              [email protected]  [email protected]@ $$$8 n$$<W$.                                          .                              
                                             [email protected]% [email protected]$"@h$$&0}&$                                                                          
                                            :$,$$$$$ @[email protected]$]8$B/.$'                                                                         
                                              8 $$ [email protected]'@ B'x  .                                                                          
                                               *. @[email protected][email protected]'Q ZB                                                                            
                                                 [email protected]$${B. .]$                                                                             
                                                  $     $$' .$                                                                              
                                                          $$.                                                                               
                                                          @                                             


*转载请注明来自AresX’s Blog

本博客采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议(CC BY-NC-SA 4.0) 发布.转载请注明出处
本文链接:https://ares-x.com/2017/09/05/Python图片转换成字符画/