AttributeError:module' PIL.Image'没有属性' register_extensions'

时间:2018-01-31 17:06:04

标签: python deep-learning google-colaboratory

我在google-colab上运行了fast.ai的第1课。当我来到线上时

img = plt.imread(f'{PATH}valid/cats/{files[0]}')

plt.imshow(img);

它没有显示图像。相反,我收到了一个错误:

AttributeError: module 'PIL.Image' has no attribute 'register_extensions'

导致这种情况的原因是什么?

10 个答案:

答案 0 :(得分:10)

遇到此问题时我正在使用Google Colab。

之后,要安装torch的代码,请添加:

!pip install pillow==4.1.1
%reload_ext autoreload
%autoreload

%autoreload将重新加载所有模块,因此不需要重新启动内核。

贷方转到this forum post

答案 1 :(得分:7)

对于我使用pip安装较新的Pillow并运行使用它的代码后,从菜单中使用“Runtime / Restart runtime ...”重新启动运行时修复了问题。

答案 2 :(得分:7)

加载模块pillow之后对我有用,所有其他的fastai设置是这样的:

# workaround 
from PIL import Image
def register_extension(id, extension): Image.EXTENSION[extension.lower()] = id.upper()
Image.register_extension = register_extension
def register_extensions(id, extensions): 
  for extension in extensions: register_extension(id, extension)
Image.register_extensions = register_extensions

现在不需要运行时重启。

答案 3 :(得分:4)

在您的colab笔记本的开头运行这3行

!pip install Pillow==4.0.0
!pip install PIL
!pip install image

我也在同一个问题上挣扎。但这对我有用。 https://pillow.readthedocs.io/en/3.1.x/reference/Image.html

答案 4 :(得分:3)

您安装的Pillow版本不是最新的。运行以下命令:

import PIL
print(PIL.PILLOW_VERSION)

可能是4.0.0。如果是这样,请运行以下命令:

!pip uninstall Pillow
!pip install Pillow==5.3.0

然后重新启动运行时(按CTRL + M .或菜单中的Runtime --> Restart runtime)。再次运行第一组命令,以确保当前的PIL版本为5.3.0。

答案 5 :(得分:2)

下次启动colab虚拟机时,请务必注释掉以下两行(即不要运行它们)

#%reload_ext autoreload        <------------— comment out 
#%autoreload 0                 <------------— comment out
%matplotlib inline

为了安全起见,我还重新安装了我的PIL

!pip install --no-cache-dir -I pillow

答案 6 :(得分:1)

在所有设置之后添加以下内容后,它对我有用:

from PIL import Image

def register_extension(id, extension): Image.EXTENSION[extension.lower()] = id.upper()
Image.register_extension = register_extension
def register_extensions(id, extensions): 
  for extension in extensions: register_extension(id, extension)
Image.register_extensions = register_extensions

,然后在Analyzing Results: Looking At Pictures

部分更改了以下功能
def load_img_id(ds, idx): return np.array(PIL.Image.open(PATH+ds.fnames[idx]))

def load_img_id(ds, idx): return np.array(Image.open(PATH+ds.fnames[idx]))

答案 7 :(得分:0)

Probably the format of the file. I changed it from ".tiff" to ".png". It works. The module does not have tiff decoder.

答案 8 :(得分:0)

运行

python -m pip install --upgrade Pillow
python3 -m pip install --upgrade Pillow

并重新启动python /重新导入模块对我来说已解决此问题。

答案 9 :(得分:-1)

!pip install Pillow==4.0.0
!pip install PIL
!pip install image
from PIL import Im