I have an image URL in the form of a shareable Google Drive link that I would like to access in my Python script. Below is what I have been trying:
from PIL import Image import requests img = Image.open(requests.get(URL).raw)
However I know this returns an HTTPResponse. I have already consulted this and this thread to no avail. How do I get rid of all the other content in the URL and just keep the image?
Advertisement
Answer
Modification points:
- In your URL of
https://drive.google.com/file/d/###fileId###/view?usp=sharing
, this file is not publicly shared. When you want to use the URL of this file, please publicly share it. https://drive.google.com/file/d/###fileId###/view?usp=sharing
of “webViewLink” is not the direct link. In this case, please use “webContentLink” likehttps://drive.google.com/uc?export=download&id=###fileId###"
.- I think that it is required to modify
requests.get(URL).raw
ofImage.open(requests.get(URL).raw)
to BytesIO.
When these points are reflected in your script, it becomes as follows.
Modified script:
Please replace ###fileId###
of https://drive.google.com/uc?export=download&id=###fileId###
with your file 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)
- I could confirm that when the file of
https://drive.google.com/uc?export=download&id=###fileId###
is publicly shared, the script works.