2011-06-03 41 views
1

我试图从HTML窗体的输入控件中截取图像,以便在服务器端处理它之前将其转换为字节字符串。如何在上传前获取图像的字符串表示形式?

如何拦截文件?

upload_files = self.get_uploads('file') 
# Intercept here to do something different than just upload 
blob_info = upload_files[0] 

如何将其转换为字节串,可转换回后的图像?

我正在使用Python和App Engine。

+2

base64? http://docs.python.org/library/base64.html – tMC 2011-06-03 05:00:30

+3

目前还不清楚你要问什么 - 你引用的代码是服务器端,所以当你运行它时,图像已经被上传。而且,image是一个字节字符串。 – 2011-06-03 05:20:48

+0

你已经有了一个字节串...你只需要将它存储在一个blob属性中。 – 2011-06-03 07:09:53

回答

0

假设你上传控件是在一个名为“图像”的形式,并且使用Werkzeug's 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现在包含您需要的字符串信息。

谢谢大家的宝贵意见!

相关问题