简体   繁体   中英

Trouble submitting image URL from Reddit using requests.get()

I am trying to submit an image URL from Reddit.com to a vision API using requests.get() in Python but I am running into difficulties in what could be a simple error on my part. The requests.get() request is successful when the link points to an explicit *.jpg, eg, https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Beef_fillet_steak_with_mushrooms.jpg/800px-Beef_fillet_steak_with_mushrooms.jpg , but unsuccessful when the link points to what I perceive to be a soft link, eg, https://preview.redd.it/9xu97c5snpr51.jpg?width=640&crop=smart&auto=webp&s=e68c02166f6fd21a47a957b187b98b92608f54a9 . Note that when pasted into a browser, both links work fine.

Does anyone have a suggestion for how I might preprocess the second link so it is handled like the first link? I would like to eventually have this code run remotely, so avoiding having to download the file locally is preferred.

From the documentation on: https://requests.readthedocs.io/en/master/user/quickstart/

You can access the response body as bytes, for non-text requests:

from PIL import Image
from io import BytesIO

i = Image.open(BytesIO(r.content))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM