最近在用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的更多相关文章

  1. 用pil产生验证码出现:ImportError: The _imagingft C module is not installed

    这个是由于PIL没有编译freetype导致的查看 lib/python2.7/site-packages/PIL/看看 _imagingft.so 是否存在 # 需要先安装jpeg库wget htt ...

  2. Python: The _imagingft C module is not installed错误的解决

    Python: The _imagingft C module is not installed错误的解决 By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明 ...

  3. python的random模块(生成验证码)

    python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...

  4. pillow生成验证码

    1.结果 2.安装pillow cmd里进入python,pip install pillow,需要等一段时间 3.代码 from PIL import Image, ImageDraw, Image ...

  5. python3用pillow生成验证码,tornado中输出图片

    原文链接http://blog.csdn.net/cdnight/article/details/49636893

  6. 利用Python几行代码批量生成验证码

    几行代码批量生成authCode 整体步骤: 1.创建图片 2.创建画笔 3.在图片上生成点 4.在图片上画线 5.在图片在画圆 6.在图片在写文本 7.在图片在生成指定字体的文本 代码奉上 #!/u ...

  7. 解决Python图片处理模块pillow使用中出现的问题

    最近爬一个电影票房的网站(url:http://58921.com/alltime),上面总票房里面其实是一张图片,那么我需要把图片识别成文字,来获取票房数据.   我头脑里第一想到的解决方案就是要用 ...

  8. python图片处理PIL

    一.PIL介绍 PIL中所涉及的基本概念有如下几个:通道(bands).模式(mode).尺寸(size).坐标系统(coordinate system).调色板(palette).信息(info)和 ...

  9. Python 的PIL,可以解决ImportError The _imagingft C module is not installed

    删除PIL相关文件 mv PIL /tmp   pip install Pillow 安装Pillow后, 可能还会发生KeyError的错误, 检查项目源码后发现是 Image 模块的save函数中 ...

随机推荐

  1. poj3468A Simple Problem with Integers(线段树,在段更新时要注意)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  2. .NET两种常见上传下载文件方法

    1.FTP模式 代码如下: (1)浏览 /// <summary> /// 浏览文件 /// </summary> /// <param name="tbCon ...

  3. sudo权限集中管理用法

    #定义一组命令集合,名称DBA_CMD,禁止使用的命令前加!即可Cmnd_Alias DBA_CMD =  /bin/touch,/bin/mkdir,/sbin/service,/sbin/chkc ...

  4. js 判断pc与手机

    var u = navigator.userAgent; if ((u.indexOf('Mac') > -1 || u.indexOf('Windows') > -1) &&am ...

  5. PostQuitMessage, PostThreadMessage( WM_QUIT )

    http://blogs.msdn.com/b/oldnewthing/archive/2005/11/04/489028.aspx Why is there a special PostQuitMe ...

  6. The Aggregate Magic Algorithms

    http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...

  7. python三大神器之一fabric使用

    fabric 是一个python包 是一个基于ssh的部署工具包 通常用来对网站 微服务等等的批量部署 例如 我有5台线上服务器 可以通过一台对着5台分发,实现自动部署的目的. 简单介绍下 fabri ...

  8. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  9. smartPtr指针的实现

    编写一个智能指针类.智能指针是一种数据类型,一般用模板实现,模拟指针行为的同时还提供自动来及回收机制.它会自动记录SmartPointer<T*>对象的引用计数,一旦T类型对象的引用计数为 ...

  10. ubuntu14 部署zookeeper3.4.6启动失败

    解压缩zookeeper,启动时,报如下错误: zkServer.sh: 81: /home/xxx/zookeeper-3.4.6/bin/zkEnv.sh: Syntax error: " ...