Flask - 如何将 request.files['image'] 读取为 base64?
Flask - how to read request.files['image'] as base64?
我正在使用 Python Flask 作为我的后端,但遇到了一个小问题。
在前端应用程序中,我有一个包含图像上传功能的表单。
在后端,我将一个变量引用到图像中
image = request.files['image']
导出 FileStorage
对象。
我想将图像转换为 base64 格式,以便将其插入到我的数据库中。
我尝试了很多东西,但没有任何效果。
有人知道吗?
基本上你需要将它作为一个流来读取,然后将它转换成base64格式。
检查以下答案:
Encoding an image file with base64
解决方案应该是这样的:
import base64
...
image = request.files['image']
image_string = base64.b64encode(image.read())
我正在使用 Python Flask 作为我的后端,但遇到了一个小问题。 在前端应用程序中,我有一个包含图像上传功能的表单。
在后端,我将一个变量引用到图像中
image = request.files['image']
导出 FileStorage
对象。
我想将图像转换为 base64 格式,以便将其插入到我的数据库中。 我尝试了很多东西,但没有任何效果。 有人知道吗?
基本上你需要将它作为一个流来读取,然后将它转换成base64格式。 检查以下答案:
Encoding an image file with base64
解决方案应该是这样的:
import base64
...
image = request.files['image']
image_string = base64.b64encode(image.read())