需要把一段文字转换成图片,我找到了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的安装和使用的更多相关文章

  1. Python pillow库安装报错

    报错信息: D:\pythontest\duanxinhongzha>pip3 install pillowCollecting pillow Could not find a version ...

  2. numpy、pandas、scipy、matplotlib、jieba、 openpyxl、pillow的安装

    cmd环境下进入python安装包里的Script文件夹 安装numpy 安装pandas 安装scipy 安装matplotlib 安装jieba(应该是之前装的库安装依赖时下载了) 安装openp ...

  3. Windows下Python中pip安装Pillow报错总结(转载)

    遇到的俩种错误1.ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting 问题原因 ...

  4. PIL、Pillow安装使用方法

    PIL(Python Imaging Library)是Python常用的图像处理库,而Pillow是PIL的一个友好Fork,提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存.图像显 ...

  5. Python验证码识别 安装Pillow、tesseract-ocr与pytesseract模块的安装以及错误解决

    1.安装Pillow pip install Pillow 2.安装tesseract-ocr OCR(Optical Character Recognition, 光学字符识别) 软件 安装包含两个 ...

  6. python 图像处理(从安装Pillow开始)

    python2.x及以下用的是PIL(图像处理库是 PIL(Python Image Library)),最新版本是 1.1.7  可在http://www.pythonware.com/produc ...

  7. django中安装pillow ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting

    在windows系统上,使用  pip install pillow安装pillow时 报错 在使用 easy_install Pillow 方式安装成功,默认是最高版本 如果需要在安装时,指定安装版 ...

  8. 杂项之图像处理pillow

    杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/latest/ pillow中文 ...

  9. 最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow

    其实我之前使用的Mac os的版本是10.8的雪豹,可是最近想体验一下Mac os10.9新版本,于是就开始更新Mac os,经过10多个小时的下载和成功安装后,发现之前的配置全乱了,首先是发现lat ...

随机推荐

  1. 创建 CSS3 下拉菜单

    1. [图片] 菜单效果 2. [代码]menu.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ...

  2. Sqlte 知识点记录

    1.表存在 select count(*) from sqlite_master where type='table' and name='MyTable'; sql),path ))"; ...

  3. algorithm 简单用法

    algorithm 简单用法 #include <iostream> #include <vector> #include <algorithm> using na ...

  4. 2013面试C++小结

    2013年我在厦门c++求职小结 1.一般公司出的面试题目中的找错误,都是出自平常公司内部使用过程中出现的真实错误. 比如stl 中erase的使用:详细请见 :http://blog.csdn.ne ...

  5. WingIDE用法笔记

    注释代码块 方法一:  ''' 被注释的代码块 '''   方法二:  选中要注释的代码块后 Ctrl + /,则选中的每一行都被# ,   用这种方法注释的代码,用Shift + Ctrl + / ...

  6. Android中高效的显示图片之一 ——加载大图

    在网上看了不少文章,发现还是官方文档介绍最详细,把重要的东西简单摘要出来.详细可看官方文档地址 ( http://www.bangchui.org/read.php?tid=9 ) . 在应用中显示图 ...

  7. Convolutional Neural Networks for Visual Recognition 2

    Linear Classification 在上一讲里,我们介绍了图像分类问题以及一个简单的分类模型K-NN模型,我们已经知道K-NN的模型有几个严重的缺陷,第一就是要保存训练集里的所有样本,这个比较 ...

  8. Mule ESB学习【转-结合了网络上的一些资源】

    1.SOA标准之一:SCA架构思想 SOA在Java领域有两套标准:一个是SUN推出的JBI(没有得到BEA和IBM的承认),另外一个是:IBM和BEA等公司推出的SCA和SDO标准. JBI之关注J ...

  9. BZOJ4317: Atm的树+2051+2117

    BZOJ4317: Atm的树+2051+2117 https://lydsy.com/JudgeOnline/problem.php?id=4317 分析: 二分答案之后就变成震波那道题了. 冷静一 ...

  10. 通过Jquery异步获取股票实时数据

    最近朋友让我帮他做个异步获取数据的程序,暂时服务器什么都没有,所以我就想先拿股票数据打个框架,方便后续开发和移植等事情 代码如下: <!-- 说明:股票看盘 作者:黑桃A 时间:2014-04- ...