首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Github导入图像到Colab

从Github导入图像到Colab
EN

Stack Overflow用户
提问于 2020-12-21 08:23:43
回答 1查看 370关注 0票数 0

我在将自己的图像导入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)时,我得到这个错误。

以下是代码

代码语言:javascript
复制
#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');

下面是错误:

代码语言:javascript
复制
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访问我的存储库的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-23 17:10:56

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

代码语言:javascript
复制
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下载包含图像的存储库。

代码语言:javascript
复制
!git clone https://github.com/thiirane/Artline_images.git images
from PIL import Image
Image.open('images/wp2504860.jpg')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65386157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档