由于有任务需要用到图像处理,当然首先想到的是OpenCV,但是这次想换一种解决方案,使用Python+PIL来处理。
然而Python标准库是不含PIL的,PIL是PythonWare公司的一个第三方Python库,在win平台上使用还有点麻烦,Google到的解决方案大都是LInux平台的,但是Linux平台上本来软件就好管理一些,而且Linux也确实适合开发,但是今天在Win下,难得麻烦转到Linux下去了,越是就开始倒腾起来。
首先,在PIL的官网 www.pythonware.com/products/pil/ 上只有win32的安装程序和一个源码,看来就只有走“自己编译源码安装”的路线了。
首先需要设置系统环境变量:VS90COMNTOOLS=%VS100COMNTOOLS% #其中“VS100COMNTOOLS”是VS2010的编译器,安装VS时貌似自动添加了。
然后再PIL目录下:>setup.py install 就OK了。
但是接下来情况发生了:
>>>import Image
>>>img = Image.open("img.jpg")
>>img.rotate(60)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "S:\Python2.7.5\lib\site-packages\PIL\Image.py", line 1359, in rotate
self.load()
File "S:\Python2.7.5\lib\site-packages\PIL\ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "S:\Python2.7.5\lib\site-packages\PIL\Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available
说明JPEG编码不支持,于是我又将图片转换成PNG格式
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "S:\Python2.7.5\lib\site-packages\PIL\Image.py", line 1359, in rotate
self.load()
File "S:\Python2.7.5\lib\site-packages\PIL\ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "S:\Python2.7.5\lib\site-packages\PIL\Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available
看来还是不支持,但是发现bmp格式的图片支持 img.rotate(); 但是当执行到 img.show() 或 img.save() 时程序就会崩溃。
查看PIL的 setup.py 文件,可以发现这么一段:
# --------------------------------------------------------------------
# Library pointers.
#
# Use None to look for the libraries in well-known library locations.
# Use a string to specify a single directory, for both the library and
# the include files. Use a tuple to specify separate directories:
# (libpath, includepath). Examples:
#
# JPEG_ROOT = "/home/libraries/jpeg-6b"
# TIFF_ROOT = "/opt/tiff/lib", "/opt/tiff/include"
#
# If you have "lib" and "include" directories under a common parent,
# you can use the "libinclude" helper:
#
# TIFF_ROOT = libinclude("/opt/tiff")
TCL_ROOT = None
JPEG_ROOT = None
ZLIB_ROOT = None
TIFF_ROOT = None
FREETYPE_ROOT = None
LCMS_ROOT = None
原来是缺少JPEG、ZLib等库的支持。而且在Win下的解决方案就是编译这些库的源码,然后添加到PIL中。
Refer:
http://www.cnblogs.com/byshome/p/3319660.html