转自实验楼:https://www.shiyanlou.com/courses/370/learning/?id=1191

代码:

# -*- coding:utf-8 -*-
from PIL import Image
import argparse #创建ArgumentParser实例
parser = argparse.ArgumentParser() #定义输入文件、输出文件、输出字符画的宽和高
parser.add_argument('file') #必选参数1
parser.add_argument('-o','--output') #可选参数2
parser.add_argument('--width',type=int,default=80) #可选参数3
parser.add_argument('--height',type=int,default=80) #可选参数4 #解析并获取参数
args = parser.parse_args() #输入的图片文件路径
IMG= args.file #输入字符画的宽度
WIDTH = args.width #输入字符画的高度
HEIGHT = args.height #输出字符画的路径
OUTPUT = args.output #首先将RGB值转为灰度值,然后使用灰度值映射到字符列表中的某个字符
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") def get_char(r,g,b,alpha=256):
#判断alpha值
if alpha == 0:
return ' '
#获取字符集的长度
length = len(ascii_char) #将RGB值转为gray,灰度值范围为0-255
gray = int(0.2126*r + 0.7152*g + 0.0722*b) #灰度值范围为0-255,而字符集只有70
#需要进行如下处理才能将灰度值映射到制定的字符上
uint = (256.0 + 1)/length #返回灰度值对应的字符
return ascii_char[int(gray/uint)] 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)

运行结果:

20个python项目--图片转字符画的更多相关文章

  1. Python 【图片转字符画】

    一.安装的第三方模块 $ sudo pip3 install --upgrade pip $ sudo pip3 install pillow //window pip3 install pillow ...

  2. 通过python将图片生成字符画

    基础知识: 1.python基础知识   快速学习链接:https://www.shiyanlou.com/courses/214 2.linux命令行操作   快速学习链接:https://www. ...

  3. Python实现图片转字符画

    from PIL import Image def get_char(r, g, b, alpha=256): ascii_char = '''$@B%8&WM#*oahkbdpqwmZO0Q ...

  4. Python 图片转字符画

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

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

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

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

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

  7. python小项目(-)图片转字符画

    # -*- coding: utf-8 -*- from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrj ...

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

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

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

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

随机推荐

  1. k8s命令集锦

    集群环境相关命令$kubectl vertion --short=true #显示当前使用的客户端及服务端程序版本信息$kubectl cluster-info #获取集群信息$kubectl api ...

  2. 前端要懂的nginx配置

    多服务接口nginx反向代理 ```js server { listen 80; server_name xx.xx.xx.com; // 前端部署的域名 root /med; index index ...

  3. C# JSON的序列化与反序列化

    需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然后使用Using: using System.Runtime.Serial ...

  4. SparkStreaming HA高可用性

    1.UpdateStateByKey.windows等有状态的操作时,自动进行checkpoint,必须设置checkpoint目录,数据保留一份在容错的文件系统中,一旦内存中的数据丢失,可以从文件系 ...

  5. react-报错-1

    react 错误提示:显示IP端口被占用

  6. [Linux系统] (5)系统网络

    一.路由表 路由表是如何决策的: [root@centos-clone1 ~]# route -n Kernel IP routing table Destination Gateway Genmas ...

  7. [转] Siamese network 孪生神经网络--一个简单神奇的结构

    转自: 作者:fighting41love 链接:https://www.jianshu.com/p/92d7f6eaacf5 1.名字的由来 Siamese和Chinese有点像.Siam是古时候泰 ...

  8. CMS 与 框架

    Framework:框架.是整合的工具集,基于编程语言.可以帮助我们快速开发网站.比较常见的是J2EE(基于Java),Symfony2(基于PHP),Django(基于Python),Ruby on ...

  9. UVA 11181 Possibility Given

    #include<bits/stdc++.h> #include<stdio.h> #include<iostream> #include<cmath> ...

  10. pip安装源和虚拟环境的搭建

    一.pip安装源 1.介绍 采用国内源,加速下载模块的速度 常用pip源: 豆瓣:https://pypi.douban.com/simple 阿里:https://mirrors.aliyun.co ...