您的位置:首页 > 编程语言 > Python开发

学习Python:Windows 64位系统使用PIL

2014-12-03 19:02 357 查看
Win7 64 位 安装第三方库PIL(Python Imaging Library),下载地址:http://www.pythonware.com/products/pil/,安装PIL-1.1.7.win32-py2.7.exe,出现
Python version 2.7 required, which was not found in the registry,如下图所示:



解决方法:建立一个文件 register.py 内容如下, 然后cmd,DOS命令行下输入:python register.py执行该脚本,会显示“python 2.7 is already registered”。

#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm #
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html 
import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)

def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"

if __name__ == "__main__":
RegisterPy()
安装成功后,进行简单的编程测试,测试代码如下:

import Image
im = Image.open('Lena.jpg')
w, h = im.size
im.thumbnail((100, 100))
im.save('Lena_2.jpg', 'jpeg')
运行程序,提示:


JPEG support not available

说明,安装PIL-1.1.7.win32-py2.7.exe出现问题,原因在于:我安装的python是64位的,pil官方只有32位的。安装时会提示找不到python的安装信息,怎么办呢?只好考虑使用其他的第三方库代替,好在有非官方的64位库(官方源码编译版)。

解决方法:使用另一个第三方PIL库,Pillow-2.6.1.win-amd64-py2.7,根据python的版本和系统的版本到网站:http://www.lfd.uci.edu/~gohlke/pythonlibs/上下载相应的版本:



由Pillow的说明可知,使用的时候,应该使用 from PIL import Image代替import Image,简单测试一下:

from PIL import Image, ImageFilter

im = Image.open('Lena.jpg')
# 模糊图片
im.filter(ImageFilter.GaussianBlur)
im.save('Lena_2.jpg', 'jpeg')


程序正常运行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python windows 64位