python - 如何在 python 2.7 中制作简单的图表

标签 python django python-2.7

我想在 python/django 中为我的网页制作简单的图表,但我不知道使用哪个库(以及如何)。

我不需要图表,我寻求一种从矩形等基元创建图像的方法。

每个这样的图表可能只生成并使用一次,因为下次值会有所不同。

我可以简单地计算其中所有矩形、线条或文本的位置,所以我想要一些轻量级的东西来从中创建图片,我将其返回为 img/png (或其他)mime 样式 就像 其中要显示的参数将由 session 和数据库决定。

我可以预先计算所有尺寸,所以我想要一些简单的东西

img=Image(PNG,598,89) # style, x, y
img.add_text('1.3.', 10,10)
img.add_rectagle(20,10, 70,20, CYAN, BLACK)
....
return img.render()

你能告诉我该怎么做吗?

先谢谢了

graph


navit成功了:)

# from django.utils.httpwrappers import HttpResponse
from PIL import Image, ImageDraw
import os,sys
im = Image.new('RGB',(598,89),'white')

draw = ImageDraw.Draw(im)
draw.rectangle((0,0,im.size[0]-1,im.size[1]-1), outline='blue')
draw.rectangle((25,10,590,20), fill='white', outline='black')
draw.rectangle((25,10,70,20), fill='rgb(255,0,0)', outline='black')
draw.rectangle((70,10,90,20), fill='green', outline='black')
draw.text((1,10),'1.3.',fill='black')
del draw

# write to stdout
im.save(sys.stdout, "PNG")

# draw.flush()
# response = HttpResponse(mimetype="image/png")
# image.save(response, "PNG")
# return response

enter image description here

最佳答案

你应该检查一下 Pillow。以下是其工作原理的示例:

from PIL import Image, ImageDraw

im = Image.open("lena.pgm")

draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw

# write to stdout
im.save(sys.stdout, "PNG")

从 Pillow 向您的客户提供文件应该很简单。如果您有疑问,请告诉我。

编辑:找到these帮助您入门的示例。

关于python - 如何在 python 2.7 中制作简单的图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39121235/

相关文章:

django 休息框架 : set field-level error from serializer validate() method

python - 类型对象 'User' 没有属性 'objects' (AbstractUser) python

Python 类实例 __dict__ 不包含所有实例变量。为什么?

python - 多线程 Python 文件系统爬虫

c++ - Python ctypes : initializing c_char_p()

python - 根据特定条件创建新行并迭代 pandas 中的列表

python - np where 语句获取 : ValueError: The truth value of a Series is ambiguous. 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

Python 创建原始音频

python - Celerycam 错误 - 此代码不在事务管理之下

python - 如何在 python 中添加/替换文件扩展名?