win7 python2.7安装PIL库

一、前言

遇到客户给了一个需求,需要拼接多个图片,上网找到一个解决方式,不过是需要安装PIL的,相信安装过这个库的应该都遇到很多问题,接下来说说怎么解决。

我的环境是:

操作系统:win10 64bit

python版本:2.7.15

 

二、问题

1. 使用pip安装

使用命令:

pip install PIL

提示:

Could not find a version that satisfies the requirement PIL (from versions: )
No matching distribution found for PIL

 

2. 直接安装windows安装包

在python lib网站(http://effbot.org/downloads/#Imaging),可以下载PIL-1.1.7.win32-py2.7.exe,直接安装。

提示:Python version 2.7 required…

主要原因是安装这个的时候需要去注册表找python相关信息,也就是安装的时候是和当前有依赖的,可以下载网站上的脚本(http://effbot.org/zone/python-register.htm)来修复一下:

#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Löw for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm

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_LOCAL_MACHINE, regpath)
    except EnvironmentError:
        try:
            reg = CreateKey(HKEY_LOCAL_MACHINE, 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()

不过运行完提示:

"*** Unable to register!"
"*** You probably have another Python installation!"

找了很久终于找到问题:

分析脚本的源代码可以看出,该脚本的作用是在注册表:
HKEY_LOCAL_MACHINE/SOFTWARE/Python/PythonCore/
的位置创建注册表项,从而给PIL的安装程序使用。但是由于PIL的安装程序只在并未在此处搜索注册表项,所以造成上述错误。
通过查阅资料得出:官网下载的PIL的安装程序在Windows10 64bit平台下搜索注册表项目的位置为:
HKEY_CURRENT_USER/SOFTWARE/Python/PythonCore
而不是脚本中写入的
HKEY_LOCAL_MACHINE
因此,将脚本中两处HKEY_LOCAL_MACHINE替换为HKEY_CURRENT_USER(Line22,25),再次运行即可解决问题。修改后脚本如下:

#-*- coding:utf-8 -*-
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Löw for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm

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
)
print version,pythonpath

def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError:
        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()

运行,搞定!

 

3. 导入Image报错

如果你跑到这里,然后输入:

from PIL import Image

没有报错的话,可以不用看下去了。

偏偏我这边会报错:

ImportError: The _imaging C module is not installed

这个问题很棘手,应该是一些兼容性的问题,网上很多答案语焉不详,很多都是说要用二进制编辑器修改_imagingft.pyd的,但是用了网上别人修改过的文件还是没能解决问题。

 

4. 换轮子

既然PIL这么麻烦就换轮子吧,逛论坛的时候发现一个网站,里面恰好有PIL的替代品:Pillow,下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/,根据自己的操作系统和python版本选择,最后进入目录用pip安装一下:

pip install Pillow-5.2.0-cp27-cp27m-win_amd64.whl

重新导入Image,没有报错,并且功能正常。

 

三、参考

1. [解决]在安装Python PIL 时出现错误: Python version 2.7 required, which

(完)

转载于:https://www.cnblogs.com/harrymore/p/9366488.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值