簡體   English   中英

使用 python PIL image.open() 從 github 打開圖像

[英]Opening image from github with python PIL image.open()

我希望使用預訓練的 resnet model 對一些線條圖進行分類,並從 github 頁面加載它們。 我認為錯誤來自我設置錯誤的文件位置,但任何幫助將不勝感激。

github 的鏈接在這里

這是我的代碼:

loc = 'https://github.com/AlexSwiderski/Images/tree/main/pnt'
fname1 = 'ambulance_resized.png'
response = requests.get(loc + fname1)
image = Image.open(BytesIO(response.content)).resize((256, 256))
data = torch.from_numpy(np.asarray(image)[:, :, :3]) / 255.

我的錯誤如下:

UnidentifiedImageError                    Traceback (most recent call last)
<ipython-input-29-6e447d67525f> in <module>()
      4 fname1 = 'ambulance_resized.png'
      5 response = requests.get(loc + fname1)
----> 6 image = Image.open(BytesIO(response.content)).resize((256, 256))
      7 data = torch.from_numpy(np.asarray(image)[:, :, :3]) / 255.
      8 

/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
   2894         warnings.warn(message)
   2895     raise UnidentifiedImageError(
-> 2896         "cannot identify image file %r" % (filename if filename else fp)
   2897     )
   2898 

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

您需要在字符串之前添加一個斜杠,否則連接的路徑將是

“https://github.com/AlexSwiderski/Images/tree/main/pntambulance_resized.png”

這是無效的。

loc = 'https://github.com/AlexSwiderski/Images/tree/main/pnt'
fname1 = '/ambulance_resized.png'
response = requests.get(loc + fname1)
image = Image.open(BytesIO(response.content)).resize((256, 256))
data = torch.from_numpy(np.asarray(image)[:, :, :3]) / 255.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM