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函数中 ...
随机推荐
- 查看大图 zoomImage
添加引用 <link rel="stylesheet" media="screen" type="text/css" href=&qu ...
- Session的实现与存储
[集群中session安全和同步] 使用数据库来保存session,就算服务器宕机了也没事,session照样在. 每次请求都进行数据库读写开销不小(使用内存数据库可以提高性能,宕机就会丢失数据. ...
- gif动态图片去白边,杂边
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-30)
- js 数组去重 的5种方法
一万数组,4个重复项,先贴上成绩. 1.3毫秒 2.115毫秒 3.71毫秒 4.6毫秒 1.哈希表 2.JQuery (最快的方法是用JQuery 这句话是截图带的... 实际上Jq是最慢的) 3. ...
- linux的webserver配置与管理——创建用户个人主页
本实验用的是RedHat linux9.0,在虚拟机上进行操作,它已具有相当完好的可视化界面,这样用户会更加easy接受和理解. 首先呢就是虚拟机的相关配置,这个就不说了,当我们安装完系统后,打开终端 ...
- Codeforces Round #337 (Div. 2) B. Vika and Squares 贪心
B. Vika and Squares 题目连接: http://www.codeforces.com/contest/610/problem/B Description Vika has n jar ...
- centos 6.3 搭建git/gitosis/gitweb
1. git的安装和配置 (1)使用yum源安装git yum install git (2)创建git用户并设置密码 #useradd --home /home/git git #passwd gi ...
- j简单的递归
1 某人写了n封信和n个信封,如果所有的信都装错了信封.求所有的信都装错信封共有多少种不同情况. 归纳法例子 1.有n个硬币(n为偶数)正面朝上排成一排,每次将n-1个硬币翻成朝上为止.编程让计算机把 ...
- MySQL auto_increment实现
http://www.cnblogs.com/xpchild/p/3825309.html 运维的时候,经常遇到auto_increment的疑惑: 机器异常crash,重启后id回退的问题 性能考虑 ...
- TIDB ---NEW SQL
https://github.com/pingcap/tidb http://www.pingcap.com/ Quick Start Run TiDB with Docker You can qui ...