调整图片大小
if len(nwh) == 1:
#如果项数为1,表示用户只输入了一个数字。即按比例缩放为较长边为此长度的矩形
ww = int(nwh[0] / max(w,h) * w) #max函数获取较大值
hh = int(nwh[0] / max(w,h) * h)
image = image.resize((ww,hh),Image.ANTIALIAS)
#改变图片大小
#第一个参数放入一个元组,指定宽高
#第二个参数 Image.ANTIALIAS 表示获取高质量图片
else:
#项数不为1,缩放为指定宽高的矩形
image = image.resize((nwh[0],nwh[1]),Image.ANTIALIAS)
2.5 转换字符
指定转换的字符集
asciis = list('M%$@#&WNBRwm8S5A4E3KXFPH69nsxeazgpqbdoctfhkyvuGZYVTUCI2QOD0L7Jjl1ri!^{}/|;:*<>_~-,. ')
#list函数将字符串转换为列表
定义转换字符的函数
def getasc(r,g,b,t=100): #t为透明度
if t == 0:
return(’ ') #如果是透明的,则直接返回空值
else:
asc = ‘’
gray = int(0.2126r+0.7152g+0.0722*b) #转灰度值
asc = asciis[int(len(asciis)/256*(gray))] #获取字符
return(asc)
开始转换字符
for i in range(h):
for o in range(w): #按行读取每一个像素的RGB值
p = image.getpixel((o,i))
g = getasc(*p) # * 将参数列表转换为多个项
txt = txt + g + distance #连接字符
txt = txt + ‘\n’ #换行
函数 getpixel
获取指定位置的 RGB
值,它的第一个参数为元组,传入像素位置 (x,y)
,如果图片是 JPG
格式的,它会返回含三项的列表 [r,g,b]
,如果图片是 PNG
格式的,它会返回含四项的列表 [r,g,b,t]
,t
是透明度
2.6 保存文本
使用 python
内置函数 open
保存文件
with open(outputfile,‘w’) as f: # ‘w’ 表示写入
f.write(txt)
2.7 效果
================== RESTART: D:\Python38-32\Files\ji2a\ji2a.py ==================
=image to ascii=
inputfile:
dora.png
outputfile:
dora.txt
distance?(Y/n):
y
resize?(needn’t:‘’, square:side length, restangle:width,height):
100
Opening ‘dora.png’…
Getting…
Saving…
Seccessfully
原图:
结果:
from PIL import Image as Image
asciis = list('M%$@#&WNBRwm8S5A4E3KXFPH69nsxeazgpqbdoctfhkyvuGZYVTUCI2QOD0L7Jjl1ri!^{}/|;:*<>_~-,. ')
#gray = int(0.2126r+0.7152g+0.0722*b)
def main():
global asciis
print(‘=image to ascii=’)
inputfile, outputfile, distance, re = getargs()
image = openfile(inputfile)
image = resize(image,re)
w, h = image.size
txt = gettxt(image,w,h,distance)
savefile(outputfile,txt)
print(‘Seccessfully’)
def getargs():
inputfile = input(‘inputfile:\n’)
outputfile = input(‘outputfile:\n’)
distance = {‘’:’ ‘,‘y’:’ ‘,‘n’:’'}
distance = distance[input(‘distance?(Y/n):\n’)]
re = input(“resize?(needn’t:‘’, square:side length, restangle:width,height):\n”)
return(inputfile,outputfile,distance,re)
def openfile(inputfile):
print(“\nOpening '”+inputfile+“'…”)
image = Image.open(inputfile)
return(image)
def resize(image,re):
if re != ‘’:
print(‘Resizing…’)
nwh = re.split(‘,’)
for i in range(len(nwh)):nwh[i]=int(nwh[i])
w, h = image.size
if len(nwh) == 1:
ww = int(nwh[0] / max(w,h) * w)
hh = int(nwh[0] / max(w,h) * h)
image = image.resize((ww,hh),Image.ANTIALIAS)
else:
image = image.resize((nwh[0],nwh[1]),Image.ANTIALIAS)
return(image)
def gettxt(image,w,h,distance):
txt = ‘’
print(‘Getting…’)
for i in range(h):
for o in range(w):
p = image.getpixel((o,i))
txt = txt + getasc(*p) + distance
txt = txt + ‘\n’
return(txt)
def getasc(r,g,b,t=100):
if t == 0:
return(’ ')
else:
asc = ‘’
gray = int(0.2126r+0.7152g+0.0722*b)
asc = asciis[int(len(asciis)/256*(gray))]
return(asc)
def savefile(outputfile,txt):
(1)Python所有方向的学习路线(新版)
这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
最近我才对这些路线做了一下新的更新,知识体系更全面了。
(2)Python学习视频
包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。
(3)100多个练手项目
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!