我正在使用 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/