Python(Pillow):'str'没有属性'read'。使用Pillow读取和显示图像

时间:2017-11-15 14:46:30

标签: python image pillow

我已经获得了一个示例程序来读取.pgm文件并将其显示为图像:

from scipy import misc      # Import misc 
import matplotlib.pyplot as plt

def main():
#         Get the filename as string
fn = str(raw_input("filename.pgm"))

#         Read file it np array
im = misc.imread(fn)


#         Display with grayscale colour map 
plt.imshow(im,cmap=plt.cm.gray)

#         Show the image
plt.show()

main()

问题是当我运行程序时,我收到以下错误;

Traceback (most recent call last):
File "ImageTest.py", line 18, in <module>
 main()
File "ImageTest.py", line 9, in main
 im = misc.imread(fn)
File "C:\Python27\lib\site-packages\numpy\lib\utils.py", line 101, in newfunc
 return func(*args, **kwds)
File "C:\Users\James\AppData\Roaming\Python\Python27\site-packages\scipy\misc\pilutil.py", line 164, in imread
 im = Image.open(name)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2536, in open
 fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'

是否有我遗漏的内容,或示例代码中是否有错误?

0 个答案:

没有答案