【问题标题】:How to read an image from a URL in Colab?如何从 Colab 中的 URL 读取图像?
【发布时间】:2022-01-10 22:55:45
【问题描述】:

我已经看到了很多关于这个主题的不同主题,但他们的解决方案似乎都不适合我。我尝试了几种使用 URL 将驱动器中的图像读取到 Colab 的方法,但均未成功。我想使用它的 URL 来阅读它,而不是安装我的驱动器并使用目录,因为多人共享这个 Colab,并且他们的图像目录可能与我的不同。

第一次尝试来自一个关于这个问题的热门帖子:How do I read image data from a URL in Python?

from PIL import Image
import requests

url = 'https://drive.google.com/file/d/1z33YPsoMe0lSNNa2XWa0tiK2571j2tFu/view?usp=sharing'

im = Image.open(requests.get(url).raw) # apparently no need for bytes wrapping in new Python versions
im = Image.open(requests.get(url, stream=True).raw) # also does not work

我得到的错误是 UnidentifiedImageError: cannot identify image file <_io.bytesio object at>

然后我尝试了:

from skimage import io

io.imshow(io.imread(url))

返回 ValueError: Could not find a format to read the specified file in mode 'i'. 感觉很迷茫,因为所有这些方法似乎都适用于其他所有人。如有任何反馈,将不胜感激。

【问题讨论】:

    标签: python image file-upload google-colaboratory imageurl


    【解决方案1】:

    使用gdown 将图片从 Google Drive 读入 Colab。

    如果您的 Google 云端硬盘中有您的图片并且您正在使用 colab.research.google.com,那么您可以按照以下步骤操作:

    1. pip install gdown

    2. 从您的图片中获取分享链接(记得设置“分享给任何知道链接的人”选项)。例如: # https://drive.google.com/file/d/1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ/view?usp=sharing

    3. 从网址1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ中提取分享链接的id。

    4. 下载有权访问此 Colab 的特定用户的文件夹内容中的图像:

      !gdown --id '1WG3DGKAo8JEG4htBSBhIIVqhn6D2YPZ' --output bird1.jpp

    5. 从文件夹内容中读取图片文件

    from PIL import Image
    im = Image.open("../content/bird1.jpg") 
    im
    

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 2011-05-25
      • 2011-11-15
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2021-08-02
      • 2020-03-25
      相关资源
      最近更新 更多