在学习廖雪峰老师的python的模块时,需要使用到PIL这个第三方库。而为了得到这个,需要使用到pip这个管理工具,于是就试了用这个命令,结果显示未安装:
<span style="font-size:14px;">QXdeMacBook-Pro:/ qx$ pip
-bash: pip: command not found</span>
于是,就网上搜方法,看到了一个方法,就是使用这个命令:
wget http://bootstrap.pypa.io/get-pip.py
试了后,显示结果如下:
QXdeMacBook-Pro:/ qx$ wget http://bootstrap.pypa.io/get-pip.py
-bash: wget: command not found
原因是没有安装wget,于是“去这里将所有内容复制下来,新建get-pip.py
文件,将内容拷进去就OK了”,按照他的方法,终于成功了!
<span style="font-size:14px;">QXdeMacBook-Pro:workspace qx$ sudo python get-pip.py
The directory '/Users/qx/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/qx/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
/tmp/tmpUvRjM_/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
Downloading pip-7.1.2-py2.py3-none-any.whl (1.1MB)
100% |████████████████████████████████| 1.1MB 324kB/s
Collecting wheel
Downloading wheel-0.26.0-py2.py3-none-any.whl (63kB)
100% |████████████████████████████████| 65kB 2.5MB/s
Installing collected packages: pip, wheel
Successfully installed pip-7.1.2 wheel-0.26.0
/tmp/tmpUvRjM_/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.</span>
接下来就是安装PIL了
不管是用pip install PIL这个命令,还是sudo pip install PIL,还是pip install PIL --allow-external PIL --allow-unverified PIL,都会安装失败。
于是,看到有人建议安装Pillow,于是我试了pip install Pillow,还是报错了。。。
再后来,试了试网上的sudo pip install -U Pillow,奇迹出现了:
<span style="font-size:14px;">QXdeMacBook-Pro:workspace qx$ sudo pip install -U Pillow
Password:
The directory '/Users/qx/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/qx/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Pillow
/Library/Python/2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading Pillow-3.0.0-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (2.9MB)
100% |████████████████████████████████| 2.9MB 124kB/s
Installing collected packages: Pillow
Successfully installed Pillow-3.0.0</span>
终于安装好了Pillow!
可接下来想导入Image这个模块,还是报错:
<span style="font-size:14px;">>>> import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Image</span>
后来,终于找到了解决方案:
<span style="font-size:14px;">>>> from PIL import Image
>>> im=Image.open('qx.jpg')
>>> print im.format,im.size,im.mode
JPEG (3000, 4200) RGB
>>> im.thumbnail((300,420))
>>> im.save('thumb_qx.jpg','JPEG')</span>
原来需要:from PIL import Image这个命令才可以!
可能大家遇到的问题各不相同,但是只要有恒心去寻找问题的解决方案,一旦成功,就会很有自豪感!