python - 如何使用 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))
推荐阅读
- r - sortable包中bucketlist的选择问题
- spring - 测试spring web客户端重试
- macos - 如何在 Mac 中使用现有私钥创建密钥库文件?
- javascript - 不使用 java 脚本中的 id 元素执行追加操作
- r - 多产品和多区域的单向方差分析
- python - 如何在 mysql 数据库中执行操作并将它们插入到树视图中?
- mysql - rails generate paper_trail:install 需要访问数据库吗?
- typescript - TypeScript:使用可区分联合的函数类型给出错误
- video - 为什么官方的 LibAV 12 视频示例不能正常工作?
- python - 从 Windows 剪贴板粘贴 Gimpfu