为了方便安装pypy的第三方库,首先安装pip。
$ curl -O http://python-distribute.org/distribute_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo pypy distribute_setup.py
$ sudo pypy get-pip.py
首先找了一个3M(3072×2304)的图片,缩小为300×300的大小
脚本:
# -*- coding:utf-8 -*-
# -------------------------------
# Filename: .py
# Revision: 1.0
# Date: 2013-05-08
# Author: simonzhang
# Web: www.simonzhang.net
# Email: simon-zzm@163.com
# -------------------------------
from PIL import Image
def main():
get_data = Image.open('test.jpg')
tmp = get_data.resize((300, 300),)
tmp.save('test1.jpg', 'JPEG', quality=75)
if __name__ == '__main__':
main()
在pypy上装PIL
$ sudo /usr/lib/pypy-upstream/bin/pip install PIL
运行脚本报错如下:
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 1290, in resize
self.load()
File “/usr/lib/pypy-upstream/site-packages/PIL/ImageFile.py”, line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 385, in _getdecoder
raise IOError(“decoder %s not available” % decoder_name)
IOError: decoder jpeg not available
不能调用系统库,之前在处理过这种问题http://www.simonzhang.net/?p=435
但是pypy比较复杂,兼容有问题。直接删除PIL,使用pillow。Pillow基础就是PIL只是兼容性强,更利于推广。
$ sudo /usr/lib/pypy-upstream/bin/pip uninstall PIL
$ sudo /usr/lib/pypy-upstream/bin/pip install pillow
注意必须使用
from PIL import Image
否则会报错
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 2020, in open
raise IOError(“cannot identify image file”)
IOError: cannot identify image file
在安装完pillow后没有产生PIL.pth文件,直接手动写一个。
$ sudo vim /usr/lib/pypy-upstream/site-packages/PIL.pth
内容是:PIL。
开始测试
time python cut_pic.py
real 0m2.841s
user 0m2.630s
sys 0m0.200s
time pypy cut_pic.py
real 0m5.144s
user 0m4.870s
sys 0m0.230s
图片产生大小如下
-rw-r–r– 1 pi pi 3588203 5月 8 16:02 test.jpg
-rw-r–r– 1 pi pi 21907 5月 8 16:10 test_pypy.jpg
-rw-r–r– 1 pi pi 21907 5月 8 16:09 test_python.jpg
直接使用python的效果更佳,不清楚原因。之后有版本升级了再做测试。