开始之前,交代一下开发环境。个人的体会是,由于版本兼容问题,学Python经常遇到莫名其妙的现象。
环境:
win7 专业版 64位 Python 2.7.14 Ipython 5.5.0 pip 9.0.1 matplotlib 2.1.0 numpy 1.13.3 PIL 1.1.7 Pillow 4.3.0
以下代码亲测可用
1 from numpy import * 2 from pylab import * 3 from PIL import Image 4 5 # 加载本地图片,当心中文字符 6 im = array(Image.open(unicode("f:\\桌面\\test.jpg", 'utf-8'))) 7 imshow(im) 8 9 10 x=[100,100,300,300] 11 y=[200,300,200,300] 12 13 # 在图片上绘制点和直线 14 plot(x,y,'r*') 15 plot(x[:2],y[:2]) 16 17 # 标题和坐标轴 18 title(unicode("DARK KNIGHT", 'utf-8')) 19 axis('off') 20 21 show()
在让上述代码正常工作前,我遇到了一些问题,记录如下以备查阅:
Q: 如何查看已经安装的python软件包和版本 A: pip list Q: 如何安装matplotlib A: python -mpip install -U matplotlib 参见 http://matplotlib.org/users/installing.html Q: name array is not defined A: from numpy import * Q: 使用imshow()函数时提示 PIL Image data can not convert to float A: python -mpip install pillow