我如何使用httplib2上传图像,但不将其保存到计算机上?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何使用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)
答案
使用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))
以上是关于我如何使用httplib2上传图像,但不将其保存到计算机上?的主要内容,如果未能解决你的问题,请参考以下文章