【问题标题】:Image won't open from response object using PIL in google Colaboratory图片不会在谷歌 Colaboratory 中使用 PIL 从响应对象中打开
【发布时间】:2019-11-26 17:42:36
【问题描述】:

我正在学习 Udemy 上的“用于深度学习和计算机视觉的 PyTorch”课程,并按照说明将代码输入 Google Colaboratory。

但是,有一部分代码是 PIL 用于从响应对象中读取图像,但我有一个错误“AttributeError: can't set attribute”

我在 python 3.6 上使用枕头 4.0.0 我尝试将 resonse.raw 更改为 response.content、response.text 和只是响应。我尝试删除 stream = True 属性,并尝试将 url 直接输入到 Image.open 方法中

!pip3 install pillow==4.0.0

import PIL.ImageOps

import requests
from PIL import Image

url = 'https://c8.alamy.com/comp/DYC06A/hornless-reindeer-at-zoo-DYC06A.jpg'
response = requests.get(url, stream = True)
img = Image.open(response.raw)
plt.imshow(img)

我希望在 url 变量中包含带有 url 的鹿图像的情节。

相反,我收到此错误消息:

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-85-059526041234> in <module>()
      4 url = 'https://c8.alamy.com/comp/DYC06A/hornless-reindeer-at-zoo-DYC06A.jpg'
      5 response = requests.get(url, stream = True)
----> 6 img = Image.open(response.raw)
      7 plt.imshow(img)

5 frames

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in _open_core(fp, filename, prefix)

/usr/local/lib/python3.6/dist-packages/PIL/JpegImagePlugin.py in jpeg_factory(fp, filename)
    750 # Factory for making JPEG and MPO instances
    751 def jpeg_factory(fp=None, filename=None):
--> 752     im = JpegImageFile(fp, filename)
    753     try:
    754         mpheader = im._getmp()

/usr/local/lib/python3.6/dist-packages/PIL/ImageFile.py in __init__(self, fp, filename)
     95 
     96         try:
---> 97             self._open()
     98         except (IndexError,  # end of data
     99                 TypeError,  # end of data (ord)

/usr/local/lib/python3.6/dist-packages/PIL/JpegImagePlugin.py in _open(self)
    321                 # print(hex(i), name, description)
    322                 if handler is not None:
--> 323                     handler(self, i)
    324                 if i == 0xFFDA:  # start of scan
    325                     rawmode = self.mode

/usr/local/lib/python3.6/dist-packages/PIL/JpegImagePlugin.py in SOF(self, marker)
    144     n = i16(self.fp.read(2))-2
    145     s = ImageFile._safe_read(self.fp, n)
--> 146     self.size = i16(s[3:]), i16(s[1:])
    147 
    148     self.bits = i8(s[0])

AttributeError: can't set attribute

【问题讨论】:

  • 适用于 Pillow 6.0.0 和 Python 3.7。考虑更新包:pip install Pillow -U
  • 我应该在哪里插入包更新?
  • 您的代码以pip 语句开头,尝试将其替换为我的并运行它。确保它以! 开头
  • 成功了。我之前尝试过更新它,但我忽略了“重新启动运行时消息”。这次我没有,它奏效了

标签: python python-3.x python-requests python-imaging-library google-colaboratory


【解决方案1】:

升级枕头为我解决了这个问题

pip install pillow --upgrade

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 2020-04-21
    • 2020-10-31
    • 2016-07-01
    • 2018-07-29
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多