需要把一段文字转换成图片,我找到了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. matlab产生很多个相同的数字

    如产生100行1列的0.5: ones(100,1)*0.5:

  2. css3 多列布局使用

    css3的出现,解决了不少前端的问题,比如动画,圆角等: 这里总结一下css3 的多列布局: w3c上给出了很多属性: 我们一般用到column-count.column-gap.column-wid ...

  3. 恢复delete删除的数据

    SELECT * FROM tablename AS OF TIMESTAMP TO_TIMESTAMP('2010-12-15 11:10:17', 'YYYY-MM-DD HH:MI:SS')

  4. 11 Python 文件操作

    文件操作基本流程 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知 ...

  5. Java钉钉开发_00_资源帖

    1.源码 本系列教程的源码已上传至GitHub:  https://github.com/shirayner/DingTalk_Demo 2.官方 官方源码:https://github.com/op ...

  6. 关于Windows与Linux下32位与64位开发中的数据类型长度的一点汇总

    32位与64位的数据类型长度是不一样的,而且windows和linux也有些许区别,下面把64位下的数据长度列表如下(无符号unsigned和有符号的长度一样): linux64            ...

  7. Gym 100801J Journey to the "The World's Start"(二分+单调队列)

    题意: 现在有1,2,3...N这N个站, 给定限定时间Limt,  N-1种票的价格, 分别对应一个最远距离,  叫你选择一种票, 满足可以在规定时间到达N站台,而且价格最低 思路: 如果买距离为L ...

  8. ACM学习历程—Hihocoder 1290 Demo Day(动态规划)

    http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...

  9. tiny4412sdk-1506原生uboot卡死

    于16年2月多购买了tiny4412sdk-1506,用友善之臂(以下简称友善)的superboot是可以进入linux,而用三星原始的uboot_tiny4412-20130729则不可以.出现现象 ...

  10. Python:Iterable和Iterator

    转于:https://blog.csdn.net/whgqgq/article/details/63685066 博主:gongqi1992 iterable和iterator最基本的区别: iter ...