Python项目之图片转字符画学习笔记
2018-01-03 17:10
369 查看
pip install pillow
ascii_dora.png链接地址:http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png
ascii.py源代码:
#-*- 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
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
# 将256灰度映射到70个字符上
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)/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)运行指令:python ascii.py ascii_dora.png
test.png链接地址:http://labfile.oss.aliyuncs.com/courses/370/test.png
运行指令:python ascii.py test.png
ascii_dora.png链接地址:http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png
ascii.py源代码:
#-*- 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
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
# 将256灰度映射到70个字符上
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)/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)运行指令:python ascii.py ascii_dora.png
test.png链接地址:http://labfile.oss.aliyuncs.com/courses/370/test.png
运行指令:python ascii.py test.png
相关文章推荐
- Python 图片转字符画 学习笔记
- opencv-python 学习笔记1:简单的图片处理
- Python爬虫学习笔记一:简单网页图片抓取
- Python爬虫学习笔记二:百度贴吧网页图片抓取
- python学习笔记三:解析html(HTMLParser、SGMLParser),并抓取图片
- PHP学习笔记(十二):$_FILES超级全局变量(借助网上社区项目的上传图片功能进行介绍)
- Python学习笔记-清除项目下SVN文件
- Scrap学习笔记 --- python实现抓取图片
- 【实验楼】Python 图片转字符画 学习记录
- python爬虫:下载百度贴吧图片学习笔记
- python学习笔记(14)--爬虫下载漫画图片修改版
- python学习笔记(8)--爬虫下载占位图片
- python学习笔记(10)--爬虫下载煎蛋图片
- python 学习笔记二十 django项目bbs论坛
- Cocos2dx 学习笔记整理----在项目中使用图片(一)
- 【Python学习笔记】-生成验证码图片
- Python OpenCV学习笔记之:通过图像金字塔方式缩小放大图片
- ios学习笔记图片+图片解释(c语言 oc语言 ios控件 ios小项目 ios小功能 swift都有而且笔记完整喔)
- Python爬虫框架Scrapy 学习笔记 4 ------- 第二个Scrapy项目
- python学习笔记-抓取网页图片脚本