导入错误:无法导入在 tensorflow 上加载图像文件所需的 Python 成像库 (PIL)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41124353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:28:17  来源:igfitidea点击:

ImportError: Could not import the Python Imaging Library (PIL) required to load image files on tensorflow

pythonpython-2.7tensorflowpython-imaging-librarypillow

提问by Muneeb Ul HaXan

I am doing a deep learning course on udacity. For the first assignment whenI tried to run the script which is below the problem 1 , I got this error. So I tried to uninstall PIL and pillow and then installed these individually but I didnot succeeded. I need help guy. I am using tensorflow docker image with python notebook.

我正在做一个关于 udacity 的深度学习课程。对于第一个任务,当我尝试运行问题 1 下方的脚本时,出现此错误。所以我尝试卸载 PIL 和枕头,然后单独安装它们,但我没有成功。我需要帮助的家伙。我正在将 tensorflow docker 图像与 python 笔记本一起使用。

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import scipy
import tarfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
# Config the matplotlib backend as plotting inline in IPython
%matplotlib inline 

url = 'http://commondatastorage.googleapis.com/books1000/'
last_percent_reported = None

def download_progress_hook(count, blockSize, totalSize):
    percent = int(count * blockSize * 100 / totalSize)

   if last_percent_reported != percent:
     if percent % 5 == 0:
  sys.stdout.write("%s%%" % percent)
  sys.stdout.flush()
else:
  sys.stdout.write(".")
  sys.stdout.flush()

last_percent_reported = percent

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/1_notmnist.ipynb

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/1_notmnist.ipynb

You can see the code here. I got error in the code block after problem 1 Error Image

你可以在这里看到代码。问题 1错误图像后,我在代码块中 出错

I tried each and everything describe here in these two links or solutions:

我尝试了这两个链接或解决方案中描述的所有内容:

Solution 1 on stackoverflow

stackoverflow上的解决方案1

Solution 2 on stackoverflow

stackoverflow上的解决方案2

Operating System:

操作系统:

using docker and tensorflow is installed in a container with IPython notebook.

使用 docker 和 tensorflow 安装在带有 IPython notebook 的容器中。

The output from python -c "import tensorflow; print(tensorflow.version)".

从蟒蛇-c输出“进口tensorflow;打印(tensorflow。)”。

0.11.0

0.11.0

回答by Yohanna

pip install pillow

pip install pillow

Then replace from IPython.display import display, Imagewith from IPython.display import displayfrom PIL import Image

然后替换 from IPython.display import display, Imagefrom IPython.display import displayfrom PIL import Image

回答by rort1989

I met the same problem. But I am using a different setting for the tensorflow. OS: Ubuntu 14.04 LTS. Installation using Anaconda. I solved it by following the warnings in Pillow installation. It may not be useful for a docker installation of tensorflow though.

我遇到了同样的问题。但是我对张量流使用了不同的设置。操作系统:Ubuntu 14.04 LTS。使用 Anaconda 安装。我按照Pillow installation 中的警告解决了它。不过,它可能对 tensorflow 的 docker 安装没有用。

Here are the steps I did. First enter the tensorflow environment,

这是我做的步骤。首先进入tensorflow环境,

source activate tensorflow

Then uninstall PIL and install Pillow

然后卸载PIL并安装Pillow

conda uninstall PIL
conda install Pillow

Then in the provided code, replace

然后在提供的代码中,替换

from IPython.display import display, Image

by

经过

from IPython.display import display
from PIL import Image

That's all. Re-run the code and it works without PIL error.

就这样。重新运行代码,它可以正常工作而不会出现 PIL 错误。

回答by A. Attia

I solved this issue by uninstalling Jupyter and re-installed it properly. The problem was linked to the notebook kernel. My terminal and my notebook didn't have the same kernel. To check it, I did in my virtualenv:

我通过卸载 Jupyter 并正确重新安装来解决了这个问题。该问题与笔记本内核有关。我的终端和我的笔记本没有相同的内核。为了检查它,我在我的 virtualenv 中做了:

jupyter-kernelspec list

jupyter-kernelspec list

then go to your kernel directories lists and open the json file (something like /Library/Jupyter/kernels/virtualenv/kernel.json)

然后转到您的内核目录列表并打开 json 文件(类似于/Library/Jupyter/kernels/virtualenv/kernel.json

and check than the Python link is the same than in the output of which python.

并检查 Python 链接是否与 which python.

If not, create another kernel for your virtualenv.

如果没有,请为您的 virtualenv 创建另一个内核。

回答by A. Attia

As for Windows users who use Anaconda, there is likely a simple solution to your problem. If you've installed 'tensorflow' with pip, or a pip variant (ie. pip3), then you will have to install tensorflow again, but this time with the command conda install tensorflow.

对于使用 Anaconda 的 Windows 用户,您的问题可能有一个简单的解决方案。如果您已经使用 pip 或 pip 变体(即 pip3)安装了“tensorflow”,那么您将不得不再次安装 tensorflow,但这次使用命令conda install tensorflow.

回答by Sneha Mule

Install PILin anaconda, then:

PIL在anaconda中安装,然后:

from PIL import Image
model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // batch_size,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=800 // batch_size)
model.save_weights('first_try.h5') 

output will display like: Epoch 1/50
 34/125 [=======>......................] - ETA: 7:23 - loss: 0.7237 - acc: 0.5478 ... comntinue