您的位置:首页 > 编程语言 > Python开发

python图片字符画

2017-08-24 16:30 204 查看

how to generate a char picture.

1.install pillow.

2.import Image&&import argsparse.

argsparse is used for dealing with args,width,height.etc.

3.get_char() function

Our purpose is to get each pixel of the image and map it to a num and this num can be expressed by a char. We use gray to represent rgb. Here is func:

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+1)/length
return ascii_char[int(gray/unit)]


4.get each pixel and pass it to get_char()

for i in range(HEIGHT):
for j in range(WIDTH):
txt+=get_char(*im.getpixel((j,i)))
txt+='\n'


5.write this pic to output.txt

with open('test.png','w') as f:
f.write(txt)


Finally, we can get our expected picture!

Here is the source code:

#!. /usr/bin/env python3
#! -*- coding: utf-8 -*-
from PIL import Image
import argparse

parser=argparse.ArgumentParser()
parser.add_argument('file')
parser.add_argument('-o','--output')
parser.add_argument('--width',type=int,default=80)
parser.add_argument('--height',type=int,default=80)

args=parser.parse_args()

IMG=args.file
HEIGHT=args.height
WIDTH=args.width
OUTPUT=args.output

ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")

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+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'

with open('output.txt','w') as f:
f.write(txt)


Test



Output

*******
***********
***       ***
**.          **   *******
**             *******b*****
#*               **;       j**$
**               c           **
***** *                             **
*********                              **
****      *                              **
***                                        *
o*p                                         **
**                                          *****
**                                           ********
**                                           k    ?***
***                                                    **#
****0                                                     **
***                                                         **
**                                                            **
**       $$$                                                   **
**     \$$$$$                                                  \*
**     '$    $$ $$$$j $$$                                        *
**     $      $Y$$$$$$$$$$                                       *
**    $$       $$$)$$    $$                                      *
*Y    $        $/))$      $@                                    p*
**   $$         $)$^      .$$                                   **
** $$$q   $$$   $)$        $$$$                                 *
**$$$$   <$$$@  $$$        $$$$$                               *****
$$$))$   $$$$$  $$          $)\$$                             *******
$$$)))$   $$$$$  #$      $$$ $)))$$                          ,*h     **
$$p))))$   $$$$$   $     $$$$ $))))$$                                  **
$$))))))$   $$$$$   $     $$$$$$t))))$$                                  **
$$)))))))$   $$$$$  B$     $$$$$$$)))))$$                                 **
$$))))))))$    $$$   $$     $$$$$$$))))))$h                                 **
#$t))))))))$    +$h   $@     $$$$$h$))))))$$                                 **
$$)))))))))$          $$     $$$$ $$))))))$$                                  **
$$)))))))))$          $$     k$$$ $$))))))$$                                  **
$$)))))))))$          $$      f$  $0))))))$$                                  **
$$)))))))))$          $$          $))))))b$                                   **
$$)))))))))B$         $$          $))))))$$                                   **
$$))))))))$$        $$$          $))))$$$                                    **
$$))))))$$$Y       $)$8        k$$B$$$$                                     **
$$$))n$$$$$      $$))$        $$$$$$                                       **
$$$$$$ $|$$    `$)))$        $                                           i*
**8  x$))$$  $$))))m$      $m                                           **
*** $$)))$$$$))))))$$    $$                                   *        **
***$$))))))))))))))$$  $$                                    **!     **
*$))))))))))))))))h$$b                                     *o***|***
$$))))))))))))))))))$                                      *))*****
$$))))))))))))))))))$                                     **))$$
$$))))))))))))))))))$$                                    **))$$
$))))))))))))))))))p$$$                                  **)))Q$
$))))))))))))))))))$$z$$                                **$))))$
$$))))))))))))))))))$Czz$$                     *       *** $))))$
$$))))))))))))))))))$zzzz$]
c290
**********  $))))$
$$))))))))))))))))))$zzzz$$                    *)******    $\)))$$
$$))))))))))))))))))$zzzz$$                   **)))$$      $$)))$$
$W))))))))))))))))))$zzzz$$   *               **)))$$      $$)))%$
$X))))))))))))))))))$zzz$$   **p             **$)))$$      $$))))$
$v)))))))))))))))))$$zz$$@  ****t           **$$)))$$      $$))))$
$o)))))))))))))))))$$$$$@ o**  ***         ***$$)))M$       $))))$
$$)))))))))))))))))$$$$*****    ****)   ****  $$)))Q$       $))))$$
$$)))))))))))))))))$******)$      ********o   $$)))|$       $))))$$
$C)))$$)))$o)))))$$ $$))))$       $X)))$$    $$))))$       $q)))$$
$$))))))))))))))%$@ $$))))$       $#)))Y$    $$))))$       $$$$$$$
$$j)))))))))))$$$  $#)))/$       $$))))$    $$$$m)$       $$$$
$$$()))))))$$$$   $/)))m$       $$))))$    $$$$$$$       $
@$$$$$$$$$$$     $))))$$       $$))))$
$$$$$$B       $))))$$       $$))))$
$))))$$       $$))))$$
$))))$$       $$))))$$
$$))))$$       $$))))$$
$$))))$$       $$))))$$
$$))))$$       $$))))$$
$$))))$$       $$))))$$
$$))))$$       $$))))$$
$$))))$$       $$))))$$
$$$$))$$        $))$$$$
$$$$$$$$        $$$$$$$
$$$        $$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python