Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows。
所以在安装好pil之后就开始写,就按照题目所说出现了The _imagingft C module is not installed 错误,找了很多建议,最后确定在windows下应该用pillpw。下载地址 点击打开链接 找到 Pillow‑2.5.2.win32‑py2.7.exe因为我用的是python2.7和win32系统,所以就应该下载这个,大家可以根据自己的需求找到响应的包进行安装, Pillow, 它是对PIL的一些BUG修正后的编译版
到了这里又遇见一个错误,那就是在使用Image.save('code.jpg','jpg')的时候会出现KeyError:’jpg‘错误,
看stackoverflow文章 http://stackoverflow.com/questions/21128256/why-does-pillow-not-recognize-the-jpeg-format
受到启发之后只需要把import Image改为 from PIL import Image 就行了
顺便附上一个生产中文验证码的代码
import ImageDraw,ImageFont
from PIL import Image
import random
import math, string class RandomChar(): @staticmethod
def Unicode():
val = random.randint(0x4E00, 0x9FBF)
return unichr(val) @staticmethod
def GB2312():
head = random.randint(0xB0, 0xCF)
body = random.randint(0xA, 0xF)
tail = random.randint(0, 0xF)
val = ( head << 8 ) | (body << 4) | tail
str = "%x" % val
return str.decode('hex').decode('gb2312') class ImageChar():
def __init__(self, fontColor = (0, 0, 0),
size = (100, 40),
fontPath = 'simsun.ttc',
bgColor = (255, 255, 255),
fontSize = 20):
self.size = size
self.fontPath = fontPath
self.bgColor = bgColor
self.fontSize = fontSize
self.fontColor = fontColor
self.font = ImageFont.truetype(self.fontPath, self.fontSize)
self.image = Image.new('RGB', size, bgColor) def rotate(self):
self.image.rotate(random.randint(0, 30), expand=0) def drawText(self, pos, txt, fill):
draw = ImageDraw.Draw(self.image)
draw.text(pos, txt, font=self.font, fill=fill)
del draw def randRGB(self):
return (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)) def randPoint(self):
(width, height) = self.size
return (random.randint(0, width), random.randint(0, height)) def randLine(self, num):
draw = ImageDraw.Draw(self.image)
for i in range(0, num):
draw.line([self.randPoint(), self.randPoint()], self.randRGB())
del draw def randChinese(self, num):
gap = 5
start = 0
for i in range(0, num):
char = RandomChar().GB2312()
x = start + self.fontSize * i + random.randint(0, gap) + gap * i
self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB())
self.rotate()
self.randLine(18) def save(self, path):
self.image.save(path,'jpeg')
if __name__=='__main__':
ic = ImageChar(fontColor=(100,211, 90))
ic.randChinese(4)
ic.save("1.jpeg")
Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed的更多相关文章
- 用pil产生验证码出现:ImportError: The _imagingft C module is not installed
这个是由于PIL没有编译freetype导致的查看 lib/python2.7/site-packages/PIL/看看 _imagingft.so 是否存在 # 需要先安装jpeg库wget htt ...
- Python: The _imagingft C module is not installed错误的解决
Python: The _imagingft C module is not installed错误的解决 By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明 ...
- python的random模块(生成验证码)
python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...
- pillow生成验证码
1.结果 2.安装pillow cmd里进入python,pip install pillow,需要等一段时间 3.代码 from PIL import Image, ImageDraw, Image ...
- python3用pillow生成验证码,tornado中输出图片
原文链接http://blog.csdn.net/cdnight/article/details/49636893
- 利用Python几行代码批量生成验证码
几行代码批量生成authCode 整体步骤: 1.创建图片 2.创建画笔 3.在图片上生成点 4.在图片上画线 5.在图片在画圆 6.在图片在写文本 7.在图片在生成指定字体的文本 代码奉上 #!/u ...
- 解决Python图片处理模块pillow使用中出现的问题
最近爬一个电影票房的网站(url:http://58921.com/alltime),上面总票房里面其实是一张图片,那么我需要把图片识别成文字,来获取票房数据. 我头脑里第一想到的解决方案就是要用 ...
- python图片处理PIL
一.PIL介绍 PIL中所涉及的基本概念有如下几个:通道(bands).模式(mode).尺寸(size).坐标系统(coordinate system).调色板(palette).信息(info)和 ...
- Python 的PIL,可以解决ImportError The _imagingft C module is not installed
删除PIL相关文件 mv PIL /tmp pip install Pillow 安装Pillow后, 可能还会发生KeyError的错误, 检查项目源码后发现是 Image 模块的save函数中 ...
随机推荐
- Web CORS 跨域方式使用方式
CORS 参考 http://enable-cors.org/index.html https://help.aliyun.com/document_detail/oss/practice/cors_ ...
- vtk读取文件中点坐标[转]
vtk基础编程(2)-读取数据文件中的坐标点 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不可少, 例如 数据文件points.dat, 中存放了三个点的坐标, 0.0 0 ...
- 基于KVM的虚拟化研究及应用
引言 虚拟化技术是IBM在20世纪70年代首先应用在IBM/370大型机上,这项技术极大地提高了大型机资源利用率.随着软硬件技术的迅速发展,这项属于大型机及专利的技术开始在普通X86计算机上应用并成为 ...
- iepngfix.htc让PNG-24在IE6中透明的方法(转)
add:360用的一个方法: <!--[if IE 6]> <script src="http://se.360.cn/js/DD_belatedPNG.js"& ...
- extjs表格下的分页条——Ext.grid.Panel 的 pagingtoolbar
两种分页条:每页固定条数的分页条 和 自定义选择每页内容条数的分页条 一.每页固定条数的分页条 这种样式的-- dockedItems: [{ xtype: 'pagingtoolbar', stor ...
- OC基础之方法和参数的命名规范
以前学过C/C++/Java/C#语言的童鞋可能刚开始对于OC的方法和参数的命名规范大为不爽 举例来说,如下一个OC方法: - (void)tableView:(UITableView *)table ...
- C#综合揭秘——细说多线程
一.线程的定义 1. 1 进程.应用程序域与线程的关系 进程(Process)是Windows系统中的一个基本概念,它包含着一个运行程序所需要的资源.进程之间是相对独立的,一个进程无法访问另一个进程 ...
- AlarmDemo-with-Database
https://github.com/anuj7sharma/AlarmDemo-with-Database
- [Angular 2] Move and Delete Angular 2 Components After Creation
After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to re ...
- 剑指 offer set 10 栈的压入、弹出序列
总结 1. 通过按位对比来判断, 没有更优的方法了