一、前言

遇到客户给了一个需求,需要拼接多个图片,上网找到一个解决方式,不过是需要安装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

(完)

win7 python2.7安装PIL库的更多相关文章

  1. 关于python下安装PIL库遇到的问题及解决办法

    关于python下安装PIL库遇到的问题及解决办法 关于python下安装PIL库遇到的问题及解决办法 :在下面这个网址下载pipllow(a replacement for PIL) www.lfd ...

  2. ubuntu14.04 安装PIL库出现OError: decoder jpeg not available 的解决方案

    出现 OError: decoder jpeg not available 的原因是,没有装JPEG的库,同时要支持png图片的话还要装 ZLIB.FREETYPE2.LITTLECMS的库文件. 先 ...

  3. python2.7安装第三方库错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0

    开发环境:win10, x64, pycharm社区版,python2.7.13 python2经常会遇见乱码的问题,并且一遇到中文就乱码.所以我们在安装的时候要注意,无论是解释器interpreto ...

  4. python2.7 安装pycrypto库报错

    windows + python2.7 先安装VC包 https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59 ...

  5. 安装PIL库时提示python未注册错误(自定义python安装路径)

    import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.pr ...

  6. ubuntu Python2.7 安装PIL问题

    $sudo easy_install PIL WARNING: '' not a valid package name; please use only.-separated package name ...

  7. python2.7安装PIL.Image模块

    这是大家常用的两种安装方法 sudo pip install PIL pip install PIL --allow-external PIL --allow-unverified PIL 如果安装成 ...

  8. Win7+vs2010下安装boost_1_46_1库

    一.boost库分类: (1)不需要编译库:any.array.asio.conversion.crc.bind/mem_fn.enable_if.function.lambda.mpl.smart_ ...

  9. win7 + python2.7 安装scipy

    问题: 直接pip install scipy将不能正确安装,缺少文件 方法: 下载  "scipy‑0.19.0‑cp27‑cp27m‑win_amd64.whl"[90多M] ...

随机推荐

  1. ConcurrentHashMap 分析

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/76795383 构造函数 JDK 1.5 引入了 ConcurrentHashMap . ...

  2. Jenkins配置slave遇到“无法启动该应用程序”的问题

    飞测说:最近在负责持续集成相关的工作,我们用的是jenkins+svn+maven+sonar, 今天在用slave这块出现了一个问题,排查了好久才解决,踩过的坑,现在和大家一起看看,希望对大家有帮助 ...

  3. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

  4. 配置Yaf

    pecl里面的yaf最新测试版http://pecl.php.net/package/Yaf 安装pcre 要先安装pcre, Debian ubuntu执行 sudo apt-get install ...

  5. java 8 新特性之Stream的排序/分类

    Stream简介 Stream是Java8提供的一个新的API,它位于java.util.stream包下.Stream API提供了一种新的方式来对Java集合进行操作,这种操作方式极大的提高了Ja ...

  6. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 20165222李勖

    1.系统运行监控 (1)使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间并分析该文件,综述一下分析结果.目标就是找出所有连网的程序,连了哪里,大约干了什么( ...

  7. http的报文结构和状态码的含义

    HTTP响应报文解剖 响应报文结构 HTTP的响应报文也由三部分组成(响应行+响应头+响应体): 以下是一个实际的HTTP响应报文: ①报文协议及版本: ②状态码及状态描述: ③响应报文头,也是由多个 ...

  8. Python 修饰符, 装饰符

    1, 看到@时候, 程序已经开始执行了. 所以@实际上是立即执行的 2, @后面的跟着函数名, 该函数(f1)是之前定义过的.  再后面跟着一个函数(f2), f2是f1的入口. 那么执行顺序是,  ...

  9. python random模块(随机数)详解

    使用前要先导入random模块 import random random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random ...

  10. 黑马Python——学习之前

    学习清单 学习态度