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
(完)
win7 python2.7安装PIL库的更多相关文章
- 关于python下安装PIL库遇到的问题及解决办法
关于python下安装PIL库遇到的问题及解决办法 关于python下安装PIL库遇到的问题及解决办法 :在下面这个网址下载pipllow(a replacement for PIL) www.lfd ...
- ubuntu14.04 安装PIL库出现OError: decoder jpeg not available 的解决方案
出现 OError: decoder jpeg not available 的原因是,没有装JPEG的库,同时要支持png图片的话还要装 ZLIB.FREETYPE2.LITTLECMS的库文件. 先 ...
- python2.7安装第三方库错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0
开发环境:win10, x64, pycharm社区版,python2.7.13 python2经常会遇见乱码的问题,并且一遇到中文就乱码.所以我们在安装的时候要注意,无论是解释器interpreto ...
- python2.7 安装pycrypto库报错
windows + python2.7 先安装VC包 https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59 ...
- 安装PIL库时提示python未注册错误(自定义python安装路径)
import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.pr ...
- ubuntu Python2.7 安装PIL问题
$sudo easy_install PIL WARNING: '' not a valid package name; please use only.-separated package name ...
- python2.7安装PIL.Image模块
这是大家常用的两种安装方法 sudo pip install PIL pip install PIL --allow-external PIL --allow-unverified PIL 如果安装成 ...
- Win7+vs2010下安装boost_1_46_1库
一.boost库分类: (1)不需要编译库:any.array.asio.conversion.crc.bind/mem_fn.enable_if.function.lambda.mpl.smart_ ...
- win7 + python2.7 安装scipy
问题: 直接pip install scipy将不能正确安装,缺少文件 方法: 下载 "scipy‑0.19.0‑cp27‑cp27m‑win_amd64.whl"[90多M] ...
随机推荐
- 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件.
'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件. 今天中午新换了一个系统,重装了一下wampserver2.2.下午想导入一个数据库文件打开dos,输入MySQL -u root - ...
- MarkDown格式作业模板
发布的随笔可复制下面的MarkDowm模板 注意事项 标题第XX次作业替换成相应的第一次作业.第二次作业...... 代码托管的链接一定要换成自己的项目 码云提交历史截图必须是自己每周的提交截图 #& ...
- CF1082D:Maximum Diameter Graph (简单构造)
Graph constructive problems are back! This time the graph you are asked to build should match the fo ...
- hibernate映射xml文件配置之一对多,多对多
一对多配置 [1]班级和学生模型 --->班级可容纳多个学生 --->学生只能属于一个班级 [2]一对多配置中的关系维护(inverse) --->一端放弃关系的维护 ---> ...
- chaos-engineering 的一些开源工具
Chaos Monkey - A resiliency tool that helps applications tolerate random instance failures. The Simi ...
- Python学习思维导图
刚学习Python时,边学边总结的,采用思维导图的形式, 适合回顾使用.内容参考<Python:从入门到实践>一书. 再给出一张Datacamp网站上的一张关于Python基础的总结 ...
- 图解VS2005之单元测试
据说VS2005里即提供了测试功能,可是对于像我或者我们这样的开发人或团队真还没有进化到用测试这块.一直以来都是手工测试或等到用户发现问题.今天在网上找了一个介绍单元测试的WORD文档,按里面说的做了 ...
- Javascript 的数据是什么数据类型?
Javascript 中的数据都是以 64 位浮点 float 存储的. 所有语言对浮点的精度是很难确定的. 如下代码可以实验到问题. <script> var a = 0.4; var ...
- linux pwd命令查看当前路径命令
命令简介: 该命令用来显示目前所在的工作目录.指令英文原义:print work directory执行权限 :All User指令所在路径:/usr/bin/pwd 或 /bin/pwd 命令语法: ...
- 关于AM335X移植SDIO WIFI的简易教程(转)
最近应一个朋友邀请,帮他移植了SDIO WIFI到3.2版本内核.因为之前已经成功移植了3.14内核,所以整个过程花了一个下午就完成了.话不多说,先交待一下平台: CPU:TI AM3352 600M ...