转载:http://allstack.net/wordpress/post-2295.html
Ubuntu16.04 安装pillow
Posted on 2017年3月24日, updated on 2017年3月24日 by allenmo
安装命令:
Shell
1 | sudo pip install pillow |
在用matplotlib.pyplot画图时,运行以下程序:
Python
1 2 3 4 5 | import matplotlib.pyplot as plt
# plt.imshow(plt.imread("/home/allen/Downloads/pandas.png")) plt.imshow(plt.imread("/home/allen/Downloads/choose power.jpg")) plt.show() |
用plt.imshow()函数时报了一个错:
Shell
1 | ValueError: Only know how to handle extensions: [u'png']; with Pillow installed matplotlib can handle more images |
意思是安装了pillow后, matplotlib就可以处理更多的图片格式了,因为默认情况下matplotlib只能处理png格式,现在要让它处理jpg格式,就需要安装pillow来解决了。
官网的解释是这样的:
matplotlib.pyplot.
imread
(*args, **kwargs)
Read an image from a file into an array.
fname may be a string path, a valid URL, or a Python file-like object. If using a file object, it must be opened in binary mode.
If format is provided, will try to read file of that type, otherwise the format is deduced from the filename. If nothing can be deduced, PNG is tried.
Return value is a numpy.array
. For grayscale images, the return array is MxN. For RGB images, the return value is MxNx3. For RGBA images the return value is MxNx4.
matplotlib can only read PNGs natively, but if PIL is installed, it will use it to load the image and return an array (if possible) which can be used with imshow()
. Note, URL strings may not be compatible with PIL. Check the PIL documentation for more information.