【问题标题】:How do I get a string representation of an image before uploading?如何在上传之前获取图像的字符串表示形式?
【发布时间】:2011-09-07 13:12:32
【问题描述】:

我正在尝试从 HTML 表单的输入控件中截取图像,以将其转换为字节字符串,然后再在服务器端进行处理。

如何截取文件?

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

如何将其转成字节串,以后再转回图片?

我正在使用 Python 和 App Engine。

【问题讨论】:

  • 不清楚你在问什么 - 你引用的代码是服务器端的,所以当你运行它时,图像已经上传了。而且,图像一个字节串。
  • 你已经有一个字节串......你只需要将它存储在一个 blob 属性中......
  • 谢谢大家的cmets。我想我必须对图像有更多的了解——这是我的第一次尝试。为了帮助我开始,如果我要执行 str(blob_info),它会像使用 str(urlfetch(url).result.content) 那样返回 base64 字符串的字符串代表吗?如果您觉得应该,我可以将其作为一个单独的问题发布。

标签: python image google-app-engine blobstore


【解决方案1】:

假设您的上传控件位于名为“image”的表单中,并且您使用的是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 现在包含您需要的字符串信息。

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多