本文介绍了使用Pycairo动态生成图像并在Django中提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Pycairo生成一个动态创建的png图像,然后用它来设置Django。我读到这个:。
I want to generate a dynamically created png image with Pycairo and serve it usign Django. I read this: Serve a dynamically generated image with Django.
有没有办法将数据从Pycairo表面直接传输到HTTP响应中?我现在正在这样做:
Is there a way to transport data from Pycairo surface directly into HTTP response? I'm doing this for now:
data = surface.to_rgba()
im = Image.frombuffer ("RGBA", (width, height), data, "raw", "RGBA", 0,1)
response = HttpResponse(mimetype="image/png")
im.save(response, "PNG")
return response
但它实际上不起作用,因为没有一个to_rgba电话(这个电话我发现使用谷歌代码,但不起作用)。
But it actually doesn't work because there isn't a to_rgba call (this call I found using Google code but doesn't work).
编辑:to_rgba可以用正确的调用get_data()代替,但是我仍然想知道我是否可以完全绕过PIL。
The to_rgba can be replaced by the correct call get_data(), but I still want to know if I can bypass PIL altogether.
推荐答案
def someView(request):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
context = cairo.Context(surface)
# Draw something ...
response = HttpResponse(mimetype="image/png")
surface.write_to_png(response)
return response
这篇关于使用Pycairo动态生成图像并在Django中提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!