-1

How can I use httplib2 to receiving images from the site, but not save it to my computer, but at the same time so that I can use it. My code is:

h = httplib2.Http('.cache')
response, content = h.request(self.url + 'v1588505946/images/mc-donalds_vexbhd.png')
out = open('images2/' + self.names[1], 'wb')
out.write(content) # How to avoid this line
out.close()
self.img1 = Image.open('images2/' + self.names[1]) # Here I want to open the image directly from the server
self.img1 = ImageTk.PhotoImage(self.img1)
3
  • Upload?In your example,it seems that you want to download a image. May 3, 2020 at 15:45
  • Just the problem is that in my example the file is saved on the PC, but I need it to open directly without downloading May 3, 2020 at 16:33
  • Does this answer your question? how-do-i-read-image-data-from-a-url-in-python
    – stovfl
    May 3, 2020 at 17:36

1 Answer 1

0

Use BytesIO to convert it directly,then you could use Image.open to open it directly. example:

from io import BytesIO

...

response, content = h.request(self.url + 'v1588505946/images/mc-donalds_vexbhd.png')
self.img1 = Image.open(BytesIO(content))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.