# -*- coding: utf-8 -*-
from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字符画所需的字符集
count = len(codeLib) def transform1(image_file):
image_file = image_file.convert("L")#转换为黑白图片,参数"L"表示黑白模式
codePic = ''
for h in range(0,image_file.size[1]): #size属性表示图片的分辨率,'0'为横向大小,'1'为纵向
for w in range(0,image_file.size[0]):
gray = image_file.getpixel((w,h)) #返回指定位置的像素,如果所打开的图像是多层次的图片,那这个方法就返回一个元组
codePic = codePic + codeLib[int(((count-1)*gray)/256)]#建立灰度与字符集的映射
codePic = codePic+'\r\n'
return codePic def transform2(image_file):
codePic = ''
for h in range(0,image_file.size[1]):
for w in range(0,image_file.size[0]):
g,r,b = image_file.getpixel((w,h))
gray = int(r* 0.299+g* 0.587+b* 0.114)
codePic = codePic + codeLib[int(((count-1)*gray)/256)]
codePic = codePic+'\r\n'
return codePic #图片文件的位置
fp = open('C:\\Users\\ZSC\\Pictures\\Saved Pictures\\ascii_dora.png','rb')
image_file = Image.open(fp)
image_file = image_file.resize((int(image_file.size[0]*0.75), int(image_file.size[1]*0.5)))#调整图片大小
print('Info:',image_file.size[0],' ',image_file.size[1],' ',count)
#输出到txt文件中
tmp = open('E:\\tmp.txt','w')
tmp.write(transform1(image_file))
tmp.close()

  

python小项目(-)图片转字符画的更多相关文章

  1. 20个python项目--图片转字符画

    转自实验楼:https://www.shiyanlou.com/courses/370/learning/?id=1191 代码: # -*- coding:utf-8 -*- from PIL im ...

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

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

  3. Python 图片转字符画

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

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

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

  5. 第一个Python小项目:图片转换成字符图片

    实现的效果:                                                                                               ...

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

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

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

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

  8. python图片转字符画(转)

    先上代码: from PIL import Image import argparse #命令行输入参数处理 parser = argparse.ArgumentParser() parser.add ...

  9. Python 【图片转字符画】

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

随机推荐

  1. oracle10G锁查询、批量杀锁及常用sql

    前言 记录1.oracle10G锁查杀技巧 2.资源检查方面的sql 锁查杀 找出所有被锁的对象.注意:不一定是死锁,大部分应该是阻塞,如果发现大量的锁对象,一定要检查程序逻辑了,优化sql sele ...

  2. linux-tomcat-install

    1.解压: tar zxvf xxx.tar.gz 配置JDK的环境变量,在etc/profile文件中添加 2.修改Tomcat启动端口 cd tomcat/conf/server.xml中的con ...

  3. 【leetcode】sudokuSolver数独解题

    0.摘要 小时候在报纸上玩过数独,那时候觉得很难,前几天在leetcode上遇到了这个题,挺有意思于是记录下来 一般一道数独题,就像他给的例子这样,9*9的格子,满足 行,列 ,宫均取1-9的数,切互 ...

  4. python3.x current_question

    扩散四维过程遇到的问题,暂不能解决,但先收集起来. ''' list_str=['test', None, '', 'str', ' ', 'END'] data = filter(lambda st ...

  5. 一次峰回路转的getshell

    扫目录发现 http://www.xxx.test.cn/bak/以及/bak/upload.jsp

  6. selenuim模块的使用 解析库

    selenium: 是自动化测试工具,我们可以用它来进行爬虫. 可以驱动浏览器,执行自定义好的任务. 可以执行js代码 执行速度慢,效率低. 一般用于做登录的认证 基本选择器: find_elemen ...

  7. 阿里云异构计算团队亮相英伟达2018 GTC大会

    摘要: 首届云原生计算国际会议(KubeCon + CloudNativeCon,China,2018)在上海举办,弹性计算研究员伯瑜介绍了基于虚拟化.容器化编排技术的云计算操作系统PouchCont ...

  8. sql中简单的触发器功能

    触发器分为DML触发器和DDL触发器DML触发器包含After触发器,执行insert update delete语句后会触发after触发器,会事务回滚DML触发器还包含instead of触发器, ...

  9. 修改Tomcat的server.xml之后,tomcat 部署项目报错:Removing obsolete files from server... Could not clean server of obsolete files: null java.lang.NullPointerException

    介个是你在clean tomcat的时候 文件没有clean清.(临时崩溃 系统宕机或其他原因)导致自己eclipse里的service.xml 在clean时没有copy一致. 解决方案: 1找到你 ...

  10. appium 链接真机运行时报错

    今天用appium链接真机时,碰到的第一个问题:Attempt to re-install io.appium.settings without first uninstalling.(这是日志中显示 ...