pythonimagegoogle-colaboratoryhttpresponseimageurl

Reading URL image content from HTTPResponse in Python


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?


Solution

  • Modification points:

    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)
    

    Reference: