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 ...
随机推荐
- 理解Load Average做好压力测试(转)
转载自:http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果 ...
- 【LeetCode】69. Sqrt(x) (2 solutions)
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...
- unity, editorWindow lose data when enter play mode
我写了个editorWindow,其中有个成员变量m_x,在创建editorWindow的时候为m_x赋的值,而在editorWindow的OnGUI里把m_x显示出来. 当我开着这个editorWi ...
- (Spring Boot框架)快速入门
Spring Boot 系列文章推荐 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 今天介绍一 ...
- 【Android】14.1 内部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...
- 监控 Linux 性能的 18 个命令行工具(转)
http://www.oschina.net/translate/command-line-tools-to-monitor-linux-performance?cmp&p=1# 1.Top- ...
- 关于64位 windows&linux双系统引导问题
换了台本子win7 64位,抽空做个双系统,装了下linux. 遇到开机问题:进linux可以正常使用,进win7花屏死机,初步估计是grub(此时的boot sector位grub)的问题,启动器被 ...
- Ubuntu12.04 修复GRUB
电脑安装了双系统,本来好好的GRUB管理启动,在重装过之后就只能进win7了,所以尝试将GRuB重新安装到mbr,使用GRUB作为启动管理程序. 1.制作U盘系统 使用软碟通,讲Ubuntu12.04 ...
- spring boot热部署pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Unix系统编程()brk,sbrk
在堆上分配内存 进程可以通过增加堆的大小来分配内存,所谓堆是一段长度可变的连续虚拟内存,始于进程的未初始化数据段末尾,随着内存的分配和释放而增减.通常将堆的当前内存边界称为"program ...