python3----练习题(图片转字符画)
import argparse
from PIL import Image def parse_param():
parser = argparse.ArgumentParser() # 命令行输入参数处理 parser.add_argument("input_file") # 输入文件
parser.add_argument("out_file") # 输出文件 parser.add_argument("--width", type=int, default=50) # 输出字符画宽
parser.add_argument("--height", type=int, default=50) # 输出字符画高 args = parser.parse_args() # 获取参数
width, height, input_file, out_file = args.width, args.height, args.input_file, args.out_file
return width, height, input_file, out_file def get_char(r, g, b, alpha=256):
'''
gray / 256 = x / len(ascii_chra) '''
if alpha == 0:
return " "
gray = (2126 * r + 7152 * g + 722 * b)/10000
# 将256灰度映射到70个字符上
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
x = int((gray / (alpha + 1.0)) * len(ascii_char))
return ascii_char[x] def write_file(out_file_name, content):
with open(out_file_name, "w") as f:
f.write(content) def main(file_name="test.jpg", width=80, height=80, out_file_name="out_file"):
text = ""
im = Image.open(file_name)
im = im.resize((width, height), Image.NEAREST)
for i in range(height):
for j in range(width):
content = im.getpixel((j, i))
text += get_char(*content)
text += "\n"
print(text)
write_file(out_file_name, text) # 字符画出到文件 if __name__ == '__main__':
main() 图片测试:
http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png
待完善.......
python3----练习题(图片转字符画)的更多相关文章
- Python 图片转字符画
Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...
- [笔记] Python 图片转字符画
一.介绍 用Python 代码完成图片转字符画 二.python 环境 Python 3.6.6 pillow 5.1.0 Python 图像处理库, 需要另外安装 三.原理 gray = 0.21 ...
- Python 图片转字符画 学习笔记
Python 图片转字符画 学习笔记 标签(空格分隔): Python 声明:此文章和所有代码是学习笔记,非原创,原文教程地址:https://www.shiyanlou.com/courses/37 ...
- 图片转字符画 【学习ing】
1.创建ascii.py 2. 下面是 ascii.py 的完整代码: from PIL import Image import argparse #命令行输入参数处理 parser = argpar ...
- Swift 实现图片转字符画的功能
本文介绍一个IOS APP , 将图片转换成ASCII字符画,使用Swift语言编写. 举个例子,我们使用著名的蕾娜照片作为原图片 经APP转换后形成的字符画,我们打印出来,效果如下: 放大她的脸部可 ...
- 用函数式的 Swift 实现图片转字符画的功能
今天整理 Pocket 中待看的文章,看到这篇<Creating ASCII art in functional Swift>,讲解如何用 Swift 将图片转成 ASCII 字符.具体原 ...
- 图片处理拓展篇 : 图片转字符画(ascii)
首先要明确思路, 图片是由像素组成的, 不同的像素有不同的颜色(rgb), 那么既然我们要转化为字符画, 最直接的办法就是利用字符串来替代像素, 也就是用不同的字符串来代表不同的像素. 另外图片一般来 ...
- python学习---50行代码实现图片转字符画2
from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<> ...
- python学习---50行代码实现图片转字符画1
转自:https://blog.csdn.net/mm1030533738/article/details/78447714 项目链接: https://www.shiyanlou.com/cours ...
随机推荐
- Ldap登陆AD(Active Directory)进行认证的Java示例
原文地址:http://hi.baidu.com/js2007/item/24efbb0fae1c9b90a3df432a package LdapTest; import java.util.Has ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 转MQTT压力测试之Tsung的使用
转自:http://www.cnblogs.com/lingyejun/p/7941271.html nTsung测试工具的基本测试命令为 Tsung -f ~/.tsung/mqtt.xml -l ...
- 怎样给ExecutorService异步计算设置超时
ExecutorService接口使用submit方法会返回一个Future<V>对象.Future表示异步计算的结果.它提供了检查计算是否完毕的方法,以等待计算的完毕,并获取计算的结果. ...
- Centos 7 防火墙
systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体.启动一个服务:systemctl start firewalld.service ...
- 请介绍WCF服务
WCF本质上提供一个跨进程.跨机器以致跨网络的服务调用 WCF合并了Web服务..net Remoting.消息队列和Enterprise Services的功能并集成在Visual Studio中, ...
- js 字符串拼接 html 累加 html 叠加
正常来说已经使用es6 的 模板了如`` //页面层 layer.open({ type: 1, content:`<div class="child_card"> & ...
- EF4
http://www.cnblogs.com/xray2005/category/189491.html http://kb.cnblogs.com/zt/ef/ http://www.cnblogs ...
- python--迭代器的实现
#!/usr/local/python/bin/python3 """ 一个迭代器的例子 """ class exsample(object ...
- mybatis的#和$的差别
在项目中假设使用mybatis保存形如"1,2,3"这种字符串.你须要使用"#"号取值.假设使用$符号会报错, 假设是查询形如"id in (1,2, ...