python - 动态地从 PIL 提供图像

标签 python python-imaging-library paste

我正在使用 PIL(Python 图像库)和 Paste 来制作一个非常简单的网页。我有一个仅返回图像标签的函数:

def home(self):
    return '<img src="photo" alt="photo"/>'

我还有一个名为 photo 的函数,它(理想情况下)返回要放入 img 标签中的图像:

def photo(self):
    img = image_from_PIL # this part works
    output = StringIO.StringIO()
    img.save(output, "JPEG") # saves the image as a StringIO in output
    final_img = output.getvalue()
    output.close()

    # then set headers['content-type'] = 'image/jpeg' (confirmed this works)

    return [final_img]

所以我知道图像存在于字符串final_img中,并且内容类型是“image/jpeg”。我希望这能起作用,但我得到的只是大小为 0 的 200 状态 OK 响应。

我也尝试了很多其他的事情,包括:

  • 将字符串转换为二进制并输出
  • 将 header 中的内容长度设置为字符串的长度(由于每个字符为 8 位,所以设置为 x8)

如果有什么区别的话,我的服务器正在使用 uWSGI 和 nginx

我不知道还能尝试什么!感谢您的帮助。

最佳答案

不要返回包含图像的列表,而是返回图像本身。

替换

return [final_img]

return final_img

关于python - 动态地从 PIL 提供图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6194865/

相关文章:

python - 使用 Python(没有 SciPy)检测照片中的特定水印

javascript - 如何过滤粘贴到 TextArea 中的值

python - 不同的功能,相同的结果

python - 如何使用选择器定位除第一个以外的所有目标

python - PIL.Image模块中各种图像缩放算法有什么区别?

python - 如何使用 python/PIL 将图像存储到 redis

vim - 在 Vim 中换行?

javascript - 仅将纯文本粘贴到可编辑的 div 中

python - 我们可以使用 SWIG 为 Qt 应用程序制作 python 绑定(bind)吗?

python - 在python中将cx_Oracle.LOB数据转换为字符串