1、小甲鱼绘图转码
用的pillow库
from PIL import Image
img = Image.open('mby.jpg')
#L表示灰度模式
out = img.convert('L')
#查看一下图片的大小
print(out.size)
width,height=out.size
#图片太大了,改一下图片的尺寸,将它变得小一点
out = out.resize((int(width*0.1),int(height*0.08)))
width,height=out.size
#从黑到简的一个个字符的模拟
asciis = '@%#*+=-. '
#初始化一个字符串用来保存
texts = ""
for row in range(height-1) :
for col in range(width-1):
gray = out.getpixel((col,row))
texts += asciis[int(gray / 255 * 9)]
#要记得换行
texts += '\n'
with open('ll.txt','w') as file :
file.write(texts)
转码后的效果
2、验证码
一个加上自定义字体的验证码图片
但要注意你的电脑上是不是有这个字体,在Windows环境,字体一般位于C:\WINDOWS\Fonts文件夹下。用户可以到此文件夹中查看Python程序中指定的字体是否存在。如果不存在,会产生OSError: cannot open resource错误。
from captcha.image import ImageCaptcha
image = ImageCaptcha(fonts=['data/RAVIE.TTF','data/TEMPSITC.TTF'])
image.write('sharon','captcha2.png')