首页 > 解决方案 > 如何使用 httplib2 上传图像,但不将其保存到我的计算机?

问题描述

如何使用httplib2从站点接收图像,但不将其保存到我的计算机,但同时可以使用它。我的代码是:

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)

标签: pythonpython-3.xtkinterhttplib2

解决方案


使用BytesIO直接转换,然后您可以使用Image.open直接打开它。例子:

from io import BytesIO

...

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

推荐阅读