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
![](http://labfile.oss.aliyuncs.com/courses/370/test.png)
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小项目--图片转化为字符画
- 简单使用python将图片转换为字符画
- python图片转字符画
- Python 图片转字符画
- python实现图片转字符画
- 手把手教你用python将图片做成字符画!
- python小项目二:图片转字符画
- python3实现图片转字符画
- Python 25行代码实现图片转字符画
- 从零学python系列之教你如何根据图片生成字符画
- python将图片转换为字符画
- Python 图片转字符画
- 将普通图片转化为字符画(Python)
- python图片转字符画
- Python入门项目——图片转字符画
- 从零学python系列之教你如何根据图片生成字符画
- python图片转字符画
- python将图片转换为字符画
- Python将图片转换为字符画