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函数中 ...
随机推荐
- 为了支持AOP的编程模式,我为.NET Core写了一个轻量级的Interception框架[开源]
ASP.NET Core具有一个以ServiceCollection和ServiceProvider为核心的依赖注入框架,虽然这只是一个很轻量级的框架,但是在大部分情况下能够满足我们的需要.不过我觉得 ...
- 使用 Protocol Buffers 代替 JSON 的五个原因
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- \r,\n,\t
\r:回车符,返回到这一行的开头,return的意思. \n:换行符,到下一行的同一位置,纵坐标相同,new line的意思. \t:制表符,为了在不使用表格的情况下,上下对齐,table的意思. E ...
- 10 款精美的 CSS3 全新特效
大家都知道,在网页制作时使用CSS技术,可以有效地对页面的布局.字体.颜色.背景和其它效果实现更加精确的控制.只要对相应的代码做一些简单的修改,就可以改变同一页面的不同部分,或者页数不同的网页的外观和 ...
- Codeforces Round #140 (Div. 1) D. The table 构造
D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- SaundProgressBar
https://github.com/eltld/SaundProgressBar
- 在Android应用中实现Google搜索的例子
有一个很简单的方法在你的 Android 应用中实现 Google 搜索.在这个例子中,我们将接受用户的输入作为搜索词,我们将使用到 Intent.ACTION_WEB_SEARCH . Google ...
- 【Animation】 使用handler和Runnable实现某一个控件的抖动效果
布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...
- 【JSP】JSTL使用core标签总结(不断更新中)
使用core标签 在页面中使用taglib指令指定标签URI和prefix.如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core&q ...