我在名为“tensorenv”的 anaconda 环境中安装了 tensorflow 0.8.0。然后我开始在名为 basic_python.py 的文件中运行基本代码。
import matplotlib.image as mpimg
# First, load the image
filename = "MarshOrchid.jpg"
image = mpimg.imread(filename)
# Print out its shape
print(image.shape)
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()
这段代码运行得很好。但是当我在这段代码中添加以下行时
import tensorflow as tf
在上面的代码的开头打印了以下跟踪:
Traceback (most recent call last):
File "basic_python.py", line 6, in <module>
image = mpimg.imread(filename)
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/matplotlib/image.py", line 1304, in imread
im = pilread(fname)
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/matplotlib/image.py", line 1283, in pilread
return pil_to_array(image)
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/matplotlib/image.py", line 1400, in pil_to_array
x = toarray(im)
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/matplotlib/image.py", line 1383, in toarray
x_str = im.tobytes('raw', im.mode)
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/PIL/Image.py", line 678, in tobytes
self.load()
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/PIL/ImageFile.py", line 235, in load
raise_ioerror(e)
File "/home/shubham/anaconda3/envs/tensorenv/lib/python3.5/site-packages/PIL/ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
OSError: broken data stream when reading image file
我该怎么办呢?因为在我编写的另一个简单的加法程序中,这个 tensorflow 导入没有引起任何问题。但在这里它引起了很多问题。
答案1
尝试在 tensorflow 之前导入 PIL。对于你的情况,只需在所有其他导入之后添加“import tensorflow as tf”。
答案2
这是一个已知的问题. 使用升级枕头pip install Pillow --upgrade
应该可以解决这个问题。