目录:
本机环境:
win10 64bit
python2.7.13
PIL包下载地址:
如下图为下载的包:
(1) 安装方法
tar.gz安装方法:
1 下载第三方包,解压
2 在命令提示符里输入cmd,然后用cd进入到第三方包的路径下
3 输入Python setup.py build
4 输入python setup.py install
看到包内的文件在安装即可……
(2) Unable to find vcvarsall.bat错误解决办法
安装过程中提示出错:
出错信息“Unable to find vcvarsall.bat”
解决方案:
1下载vcredist_x64.exe和Microsoft Visual C++ Compiler for Python 2.7
vcredist_x64.exe下载地址
找到Microsoft Visual C++ 2008,链接进去下载vcredist_x64.exe后安装
然后到下面这个网站下载Microsoft Visual C++ Compiler for Python 2.7并安装
Microsoft Visual C++ Compiler for Python 2.7下载地址2 重启电脑,找到vcvarsall.bat,然后按下述方法编辑注册表
1.打开注册表编辑器 win+R,然后输入regedit 2.配置: 2.1、如果你安装的Python是32位的,则,创建如下项: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Setup\VC 2.2、如果你安装的Python是64位的,则,创建如下项:HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC 3、并在此项下新建字符串值: 名称:productdir 数据:vcvarsall.bat所在路径 我的vcvarsall.bat文件位置: C:\Users\GPJ\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0
按照(一)中tar.gz安装方法重新安装
安装,成功
(3) PIL中的show函数不能正常显示图片
运行如下程序图片不能正常显示:
from PIL import Image
from PIL import ImageDraw
b=Image.new('RGB',(200,300))
m=ImageDraw.Draw(b)
m.polygon([(20,20),(35,140),(180,166)],fill=0xff00ff)
b.show()
解决办法:
修改PIL安装目录下的ImageShow.py文件的第99行
我的ImageShow.py文件位置在E:\IDE\Python\Lib\site-packages\PIL路径下
原来的第99行是:
return "start /wait %s && del /f %s" % (file, file)
改为:
return "start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s" % (file, file)
PIL库初始不支持jpg图片格式,需要之前安装libjpeg包和freetype包
#创建图片画一个三角形并读取
from PIL import Image
from PIL import ImageDraw
a=Image.new('RGB',(200,300))
print a
m=ImageDraw.Draw(a)
m.polygon([(20,20),(35,140),(180,166)],fill=0xff00ff)
print a
wihth=a.size[0]
height=a.size[1]
for x in range(wihth):
for y in range(height):
pixel=a.getpixel((x,y))
print pixel
#a.show()
#打开图片并读取(100,100)位置的像素,及读取该像素列表上的第一个数据
from PIL import Image
im=Image.open(r"C:\Users\GPJ\Desktop\test.bmp")
print im.format,im.size,im.mode
print im.getpixel((100,100))
b=im.getpixel((100,100))
print b[0]