1. import argparse
  2. from PIL import Image
  3.  
  4. def parse_param():
  5. parser = argparse.ArgumentParser() # 命令行输入参数处理
  6.  
  7. parser.add_argument("input_file") # 输入文件
  8. parser.add_argument("out_file") # 输出文件
  9.  
  10. parser.add_argument("--width", type=int, default=50) # 输出字符画宽
  11. parser.add_argument("--height", type=int, default=50) # 输出字符画高
  12.  
  13. args = parser.parse_args() # 获取参数
  14. width, height, input_file, out_file = args.width, args.height, args.input_file, args.out_file
  15. return width, height, input_file, out_file
  16.  
  17. def get_char(r, g, b, alpha=256):
  18. '''
  19. gray / 256 = x / len(ascii_chra)
  20.  
  21. '''
  22. if alpha == 0:
  23. return " "
  24. gray = (2126 * r + 7152 * g + 722 * b)/10000
  25. # 将256灰度映射到70个字符上
  26. ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
  27. x = int((gray / (alpha + 1.0)) * len(ascii_char))
  28. return ascii_char[x]
  29.  
  30. def write_file(out_file_name, content):
  31. with open(out_file_name, "w") as f:
  32. f.write(content)
  33.  
  34. def main(file_name="test.jpg", width=80, height=80, out_file_name="out_file"):
  35. text = ""
  36. im = Image.open(file_name)
  37. im = im.resize((width, height), Image.NEAREST)
  38. for i in range(height):
  39. for j in range(width):
  40. content = im.getpixel((j, i))
  41. text += get_char(*content)
  42. text += "\n"
  43. print(text)
  44. write_file(out_file_name, text) # 字符画出到文件
  45.  
  46. if __name__ == '__main__':
  47. main()
  48.  
  49. 图片测试:
  50. http://labfile.oss.aliyuncs.com/courses/370/ascii_dora.png

待完善.......

python3----练习题(图片转字符画)的更多相关文章

  1. Python 图片转字符画

    Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...

  2. [笔记] Python 图片转字符画

    一.介绍 用Python 代码完成图片转字符画 二.python 环境 Python 3.6.6 pillow 5.1.0  Python 图像处理库, 需要另外安装 三.原理 gray = 0.21 ...

  3. Python 图片转字符画 学习笔记

    Python 图片转字符画 学习笔记 标签(空格分隔): Python 声明:此文章和所有代码是学习笔记,非原创,原文教程地址:https://www.shiyanlou.com/courses/37 ...

  4. 图片转字符画 【学习ing】

    1.创建ascii.py 2. 下面是 ascii.py 的完整代码: from PIL import Image import argparse #命令行输入参数处理 parser = argpar ...

  5. Swift 实现图片转字符画的功能

    本文介绍一个IOS APP , 将图片转换成ASCII字符画,使用Swift语言编写. 举个例子,我们使用著名的蕾娜照片作为原图片 经APP转换后形成的字符画,我们打印出来,效果如下: 放大她的脸部可 ...

  6. 用函数式的 Swift 实现图片转字符画的功能

    今天整理 Pocket 中待看的文章,看到这篇<Creating ASCII art in functional Swift>,讲解如何用 Swift 将图片转成 ASCII 字符.具体原 ...

  7. 图片处理拓展篇 : 图片转字符画(ascii)

    首先要明确思路, 图片是由像素组成的, 不同的像素有不同的颜色(rgb), 那么既然我们要转化为字符画, 最直接的办法就是利用字符串来替代像素, 也就是用不同的字符串来代表不同的像素. 另外图片一般来 ...

  8. python学习---50行代码实现图片转字符画2

    from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<> ...

  9. python学习---50行代码实现图片转字符画1

    转自:https://blog.csdn.net/mm1030533738/article/details/78447714 项目链接: https://www.shiyanlou.com/cours ...

随机推荐

  1. 理解Load Average做好压力测试(转)

    转载自:http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果 ...

  2. 【LeetCode】69. Sqrt(x) (2 solutions)

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...

  3. unity, editorWindow lose data when enter play mode

    我写了个editorWindow,其中有个成员变量m_x,在创建editorWindow的时候为m_x赋的值,而在editorWindow的OnGUI里把m_x显示出来. 当我开着这个editorWi ...

  4. (Spring Boot框架)快速入门

    Spring Boot 系列文章推荐 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 今天介绍一 ...

  5. 【Android】14.1 内部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...

  6. 监控 Linux 性能的 18 个命令行工具(转)

    http://www.oschina.net/translate/command-line-tools-to-monitor-linux-performance?cmp&p=1# 1.Top- ...

  7. 关于64位 windows&linux双系统引导问题

    换了台本子win7 64位,抽空做个双系统,装了下linux. 遇到开机问题:进linux可以正常使用,进win7花屏死机,初步估计是grub(此时的boot sector位grub)的问题,启动器被 ...

  8. Ubuntu12.04 修复GRUB

    电脑安装了双系统,本来好好的GRUB管理启动,在重装过之后就只能进win7了,所以尝试将GRuB重新安装到mbr,使用GRUB作为启动管理程序. 1.制作U盘系统 使用软碟通,讲Ubuntu12.04 ...

  9. spring boot热部署pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. Unix系统编程()brk,sbrk

    在堆上分配内存 进程可以通过增加堆的大小来分配内存,所谓堆是一段长度可变的连续虚拟内存,始于进程的未初始化数据段末尾,随着内存的分配和释放而增减.通常将堆的当前内存边界称为"program ...