【问题标题】:Importing images from Github to Colab将图像从 Github 导入 Colab
【发布时间】:2021-03-30 19:50:09
【问题描述】:

我无法将自己的图片导入https://colab.research.google.com/github/vijishmadhavan/Light-Up/blob/master/ArtLine.ipynb#scrollTo=eOhPqC6fysD4

我能够执行示例图像(例如,https://wallpapercave.com/wp/wp2504860.jpg),但是当我复制相同的图像并将其放入我自己的 Github 存储库(https://github.com/thiirane/Artline_images/blob/main/wp2504860.jpg)时,我收到此错误。

这是代码

#url = 'https://wallpapercave.com/wp/wp2504860.jpg' #@param {type:"string"}
url='https://github.com/thiirane/Artline_images/blob/main/wp2504860.jpg'#@param {type:"string"}
from google.colab import files
from PIL import Image
from IPython.display import Image

#uploaded = files.upload()
response = requests.get(url)

img= PIL.Image.open(BytesIO(response.content)).convert("RGB")
img_t = T.ToTensor()(img)
img_fast = Image(img_t)
show_image(img_fast, figsize=(8,8), interpolation='nearest');

这是错误:

UnidentifiedImageError                    Traceback (most recent call last)
<ipython-input-18-5d0fa6dc025f> in <module>()
      8 response = requests.get(url)
      9 
---> 10 img= PIL.Image.open(BytesIO(response.content)).convert("RGB")
     11 img_t = T.ToTensor()(img)
     12 img_fast = Image(img_t)

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
   2860         warnings.warn(message)
   2861     raise UnidentifiedImageError(
-> 2862         "cannot identify image file %r" % (filename if filename else fp)
   2863     )
   2864 

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fb88126f0f8>

感谢您的帮助。这可能是我不允许 Colab 访问我的存储库的事情。

【问题讨论】:

    标签: github google-colaboratory


    【解决方案1】:

    这是因为 URL 不是直接下载链接。改用这个。

    import requests
    from io import BytesIO
    from PIL import Image
    url = 'https://raw.githubusercontent.com/thiirane/Artline_images/main/wp2504860.jpg'
    page = requests.get(url)
    Image.open(BytesIO(page.content))
    

    或者您可以使用git 下载包含图像的存储库。

    !git clone https://github.com/thiirane/Artline_images.git images
    from PIL import Image
    Image.open('images/wp2504860.jpg')
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2020-07-16
      • 2014-07-01
      • 1970-01-01
      • 2019-03-11
      • 2017-01-05
      • 2016-12-27
      • 2021-01-12
      • 2022-10-19
      相关资源
      最近更新 更多