我有一個可共享的Google Drive 鏈接形式的影像 URL ,我想在我的 Python 腳本中訪問它。以下是我一直在嘗試的:
from PIL import Image
import requests
img = Image.open(requests.get(URL).raw)
但是我知道這會回傳一個 HTTPResponse。我已經咨詢過這個和這個執行緒無濟于事。如何擺脫 URL 中的所有其他內容并只保留影像?
uj5u.com熱心網友回復:
修改點:
- 在您的 URL 中
https://drive.google.com/file/d/###fileId###/view?usp=sharing,此檔案未公開共享。當您想使用此檔案的 URL 時,請公開分享。 https://drive.google.com/file/d/###fileId###/view?usp=sharing“webViewLink”的不是直接鏈接。在這種情況下,請使用“webContentLink”之類的https://drive.google.com/uc?export=download&id=###fileId###"。- 我認為需要修改
requests.get(URL).raw為Image.open(requests.get(URL).raw)BytesIO。
當這些點反映在你的腳本中時,它變成如下。
修改后的腳本:
請用您###fileId###的https://drive.google.com/uc?export=download&id=###fileId###檔案 ID 替換。
from PIL import Image
import requests
import io
URL = "https://drive.google.com/uc?export=download&id=###fileId###"
img = Image.open(io.BytesIO(requests.get(URL).content))
print(img.format, img.size, img.mode)
- 我可以確認,當檔案
https://drive.google.com/uc?export=download&id=###fileId###被公開共享時,腳本有效。
參考:
- 檔案的資源表示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410289.html
標籤:
下一篇:如何創建光隧道變換影像
