这篇文章主要介绍了基于python实现图片转字符画代码实例,文中经过示例代码介绍的很是详细,对你们的学习或者工做具备必定的参考学习价值,须要的朋友能够参考下python
直接上代码图片就使用我家爽妹子的吧web

若是没有安装pil模块的话先cmd安装下微信
输入:pip install pillow网络
# -*- coding: utf-8 -*-app
from PIL import Imageless
编辑器
codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字符画所需的字符集学习
count = len(codeLib)flex
url
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(r'C:\路径\3.jpg','rb')
image_file = Image.open(fp)
image_file=image_file.resize((int(image_file.size[0]*0.2), int(image_file.size[1]*0.1)))#调整图片大小
print (u'Info:',image_file.size[0],' ',image_file.size[1],' ',count)
tmp = open('tmp.txt','w')
tmp.write(transform1(image_file))
tmp.close()
a,b,c=1,2,3
print(a,b,c)
结果不知还可否看出来

以上就是本文的所有内容,但愿对你们的学习有所帮助
欢迎你们点赞,留言,转发,转载,感谢你们的相伴与支持
万水千山老是情,点个【在看】行不行
*声明:本文于网络整理,版权归原做者全部,如来源信息有误或侵犯权益,请联系咱们删除或受权事宜。