一、前言

遇到客户给了一个需求,需要拼接多个图片,上网找到一个解决方式,不过是需要安装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.htmimport sysfrom _winreg import *# tweak as necessary
version = sys.version[:3]
installpath = sys.prefixregpath = "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!"returnprint "--- Python", version, "is now registered!"returnif (QueryValue(reg, installkey) == installpath andQueryValue(reg, pythonkey) == pythonpath):CloseKey(reg)print "=== Python", version, "is already registered!"returnCloseKey(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.htmimport sysfrom _winreg import *# tweak as necessary
version = sys.version[:3]
installpath = sys.prefixregpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (installpath, installpath, installpath
)
print version,pythonpathdef 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!"returnprint "--- Python", version, "is now registered!"returnif (QueryValue(reg, installkey) == installpath andQueryValue(reg, pythonkey) == pythonpath):CloseKey(reg)print "=== Python", version, "is already registered!"returnCloseKey(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

win7 python2.7安装PIL库相关推荐

  1. mac 如何安装PIL库

    你可以使用以下步骤在 Mac 上安装 PIL 库: 打开终端(你可以在应用程序的「实用工具」文件夹中找到它). 确保你已经安装了 pip,这是 Python 的包管理器.如果你还没有安装 pip,你可 ...

  2. Python3.9.5安装PIL库

    Python3.9.5安装PIL库 一.下载PIL库 二.安装PIL库 总结 一.下载PIL库 PIL这个库的名字官方改为了Pillow,PIL官方版不支持py3,不过有非官方那个的替代品pillow ...

  3. python2.7下安装PIL库

    window7下安装 1.因为PIL需要C++支持,先安装 Microsoft Visual C++ 9.0,安装下载地址:http://www.microsoft.com/en-us/downloa ...

  4. python3安装PIL库的经历

    前提:我是在电脑上同时装了python2和python3 两个环境.详见 Windows环境同时安装多个版本的Python解释器(python2和python3)(超详细)_落墨画雪的博客-CSDN博 ...

  5. Mac下安装PIL库

    首先,下载jpegsrc.zlib.PIL python Imaging Library 1.1.7 Source Kit jpegsrc.v9a.tar.gz zlib-1.2.8.tar.gz 然 ...

  6. python2.7 安装pycrypto库报错

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

  7. python如何安装pil库_Python安装PIL库

    一.安装pip python的库一般都用pip安装. p3.6的时候pip已经有了.如果没有的话,可以用在以下http://www.lfd.uci.edu/~gohlke/pythonlibs/#pi ...

  8. linux安装pil库_python安装pil库方法及代码

    安装PIL 在Debian/Ubuntu Linux下直接通过apt安装: $ sudo apt-get install python-imaging Mac和其他版本的Linux可以直接使用easy ...

  9. python安装pil cmd_Python安装PIL库

    一.安装pip python的库一般都用pip安装. p3.6的时候pip已经有了.如果没有的话,可以用在以下http://www.lfd.uci.edu/~gohlke/pythonlibs/#pi ...

最新文章

  1. 第二课unit2 控制对文件的访问
  2. Django 和 Ajax 简介
  3. java开发app启动跳转到一个广告,Android实现点击通知栏后,先启动应用再打开目标Activity...
  4. ECSHOP如何增加红包序列号字符
  5. 从零开始学JavaWeb
  6. python中写入文件数据及文件定位操作命令
  7. 前端面试准备---浏览器和网络篇(一)
  8. 立足共赢 | 开宸引领绿色会展经济新形式
  9. VScode 搭建 django 开发环境(Win Python3.71 django1.11.11)
  10. flex学习笔记 富文本编辑(一)-RichEditableText
  11. 在虚函数 声明中写override的作用
  12. html 让360浏览器兼容模式,360浏览器兼容模式的设置方法
  13. 北京专精特新企业申报攻略
  14. android id设备认证失败,教大家Apple ID验证失败发生未知错误的解决方法
  15. win10计算机文件夹隐藏,Win10怎么隐藏文件夹_Win10怎么隐藏此电脑的文件夹-192路由网...
  16. 使用云祺虚拟机备份软件恢复SANFOR HCI虚拟机
  17. 2019年京东PLUS会员前端开发之路总结
  18. 广告点击流量实时统计
  19. matlab绘制步进频率信号,雷达信号处理MATALB模拟---频率步进信号SFWC
  20. ssas脚本组织程序_SSAS系列——【01】准备知识

热门文章

  1. 利用Tomcat的用户名和密码构建“永久”后门
  2. NAS+CNN+Transformer=ViT-Res!MIT团队重磅开源ViT-Res,精度高于DeiT-Ti8.6%
  3. 近期两篇双目图像超分辨算法论文解读 |AAAI2020 SPL2020
  4. TensorFlow2.1正式版发布!最后一次支持Python2,进一步支持TPU
  5. 2d游戏地图编辑器_从零开始的unity(3)——2d背景的制作和使用
  6. 9篇!悉尼科技大学入选CVPR2021都研究什么?
  7. 腾讯2021春季校园招聘启动
  8. 最近目标检测新范式汇总SparseRCNN,OneNet,DeFCN等
  9. YOLO-Fastest算法!准确率接近YOLOv3,速度快上45%
  10. 各种排序算法的分析及javapython实现