如何在上传前获取图片的字符串表示?
我想从一个HTML表单的输入控件中获取一张图片,然后把它转换成字节字符串,这样在服务器端处理的时候就能用上。
我该怎么获取这个文件呢?
upload_files = self.get_uploads('file')
# Intercept here to do something different than just upload
blob_info = upload_files[0]
我该怎么把它转换成字节字符串,以后又能转换回图片呢?
我正在使用Python和App Engine。
1 个回答
0
假设你的上传控件在一个名为“image”的表单里,并且你正在使用Werkzeug的FileStorage:
img_stream = self.form.image.data
mimetype = img_stream.content_type
img_str = img_stream.read().encode('base64').replace('\n', '')
data_uri = 'data:%s;%s,%s' % (mimetype, 'base64', img_str)
现在你的data_uri里包含了你需要的字符串信息。
感谢大家的热心评论!