Pillow的安装和使用
需要把一段文字转换成图片,我找到了PIL(Python Imaging Library)库,专门干这个用的。还有一个Pillow是“friendly PIL fork”,于是我选择了后者。
安装过程稍有曲折,主要是要先把它的依赖库安装就绪,再装Pillow,过程如下:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # 安装brew
sudo easy_install pip #安装pip
brew install libzip # 安装libzip
ln -s /usr/local/include/freetype2 /usr/local/include/freetype
ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10..sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/X11 /usr/local/include/X11 sudo pip install pillow # 安装pillow
刚开始我在没有安装libzip和freetype之前,直接安装了pillow,会报出如下错误:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10..sdk/usr/include/tk.h::: fatal error:
'X11/Xlib.h' file not found
# include <X11/Xlib.h>
^
error generated.
error: command 'cc' failed with exit status
font = PIL.ImageFont.truetype('Helvetical', )
File "/Library/Python/2.7/site-packages/PIL/ImageFont.py", line , in truetype
return FreeTypeFont(filename, size, index, encoding)
File "/Library/Python/2.7/site-packages/PIL/ImageFont.py", line , in __init__
self.font = core.getfont(file, size, index, encoding)
File "/Library/Python/2.7/site-packages/PIL/ImageFont.py", line , in __getattr__
raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed
IOError: encoder zip not available
将前面的依赖库安装就绪之后,这些问题就都解决了。
写段代码如下:
import loggingimport PIL.Image
import PIL.ImageDraw
import PIL.ImageFont class Main(object):
def text2png(self, text, fontName, fontSize, pngPath):
font = PIL.ImageFont.truetype(fontName, fontSize)
width, height = font.getsize(text)
logging.debug('(width, height) = (%d, %d)' % (width, height))
image = PIL.Image.new('RGBA', (width, height), (0, 0, 0, 0)) # 设置透明背景
draw = PIL.ImageDraw.Draw(image)
draw.text((0, 0), text, font = font, fill = '#000000')
image.save(pngPath) def Main(self):
text = u' ^_^ '
fontName = '/Library/Fonts/华文黑体.ttf'
pngPath = 'test.png'
self.text2png(text, fontName, 37, pngPath)
即可将字符' ^_^ '转为图片,如下

Pillow的安装和使用的更多相关文章
- Python pillow库安装报错
报错信息: D:\pythontest\duanxinhongzha>pip3 install pillowCollecting pillow Could not find a version ...
- numpy、pandas、scipy、matplotlib、jieba、 openpyxl、pillow的安装
cmd环境下进入python安装包里的Script文件夹 安装numpy 安装pandas 安装scipy 安装matplotlib 安装jieba(应该是之前装的库安装依赖时下载了) 安装openp ...
- Windows下Python中pip安装Pillow报错总结(转载)
遇到的俩种错误1.ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting 问题原因 ...
- PIL、Pillow安装使用方法
PIL(Python Imaging Library)是Python常用的图像处理库,而Pillow是PIL的一个友好Fork,提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存.图像显 ...
- Python验证码识别 安装Pillow、tesseract-ocr与pytesseract模块的安装以及错误解决
1.安装Pillow pip install Pillow 2.安装tesseract-ocr OCR(Optical Character Recognition, 光学字符识别) 软件 安装包含两个 ...
- python 图像处理(从安装Pillow开始)
python2.x及以下用的是PIL(图像处理库是 PIL(Python Image Library)),最新版本是 1.1.7 可在http://www.pythonware.com/produc ...
- django中安装pillow ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting
在windows系统上,使用 pip install pillow安装pillow时 报错 在使用 easy_install Pillow 方式安装成功,默认是最高版本 如果需要在安装时,指定安装版 ...
- 杂项之图像处理pillow
杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/latest/ pillow中文 ...
- 最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow
其实我之前使用的Mac os的版本是10.8的雪豹,可是最近想体验一下Mac os10.9新版本,于是就开始更新Mac os,经过10多个小时的下载和成功安装后,发现之前的配置全乱了,首先是发现lat ...
随机推荐
- Nginx HTTP反向代理基础配置
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- php版微信公众平台开发之验证步骤实例详解
本文实例讲述了php版微信公众平台开发之验证步骤.分享给大家供大家参考,具体如下: 微信公众平台开发我们现在做得比较多了,这里给各位介绍的是一个入门级别的微信公众平台验证基础知识了,有兴趣的和小编来看 ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- JavaUtil_07_HttpUtil_使用Hutool 封装的 HttpUtil
二.参考资料 1.[Hutool]Hutool工具类之Http工具——HttpUtil
- 异常之: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
在 MySQL 中执行命令试下: set global time_zone=’+8:00’ 设置为东8区 就不报错了. show variables like '%time_zone%'; 解释:在 ...
- Struts2 - 异常处理: exception-mapping 元素
异常处理: exception-mapping 元素 在action方法中添加 int i=1/0; 请求action后,结果为: 在struts.xml中添加异常处理:exception-mappi ...
- NO3:步履蹒跚-完成第一章节学习
第一章小记: 每个C程序都要求有一个main()函数(多于一个main()函数是不合法的(已犯错:在VS 2010一个项目里两个C文件都有main函数,不能编译通过,必须删除一个文件,永记)).mai ...
- luoguP1941福赖皮波德
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- 通过Jquery异步获取股票实时数据
最近朋友让我帮他做个异步获取数据的程序,暂时服务器什么都没有,所以我就想先拿股票数据打个框架,方便后续开发和移植等事情 代码如下: <!-- 说明:股票看盘 作者:黑桃A 时间:2014-04- ...
- 在Debug中使用断点调试程序
我最近在学习汇编的程序,所以很多都需要动手写点代码去测试,如果是测试三五行代码的还比较简单,可以在debug中直接按T进行单步调试,但是到后来调试的代码越来越复杂,越来越长,如果再使用单步调试不知道要 ...