2

I would like to make simple graphs for my web page in python/django, but I do not know, which library (and how) to use.

I DO NOT WANT CHARTS, I SEEK A WAY TO CREATE IMAGE FROM PRIMITIVES LIKE RECTANGLES.

Each such graph is probabely generated and used only one time, as next time the values would differ.

I can simply compute the positions of all rectangles, lines or texts in it, so I would like something lightweight to just create pictre from that, which I would return as img/png (or so) mime style like <img src="http://my.web.www/my/page/graph" > where the parameters to show would be decidied by session and database.

I can compute all the sizes beforehand, so I would like something simple like

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()

Can you direct me, how to do it?

Thanks beforehand

graph


navit nailed it :)

# 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

Community
  • 1
  • 1
gilhad
  • 609
  • 1
  • 5
  • 22
  • Do you want static chart (like an image) or dynamic chart. Either way this is a duplicated question. See: http://stackoverflow.com/questions/8840255/djhttp://stackoverflow.com/questions/7034/graph-visualization-library-in-javascriptango-and-interactive-graph-network-visualization and http://stackoverflow.com/questions/7034/graph-visualization-library-in-javascript – eguaio Aug 24 '16 at 11:00
  • I want to generate image in python (and transfer it via http to browser, where it is displayed as any other "normal image") – gilhad Aug 24 '16 at 11:05
  • My poblem is just how to construct one-time image in python. I do not want to use javascript/jquery/anything else on client side – gilhad Aug 24 '16 at 11:08

3 Answers3

2

You should check Pillow out. Here is a sample how it works:

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")

Serving a file from Pillow to your client should be straightforward. Let me know if you have a question.

edit: found these examples to get you started.

navid
  • 566
  • 6
  • 15
  • Thanks, I looked some of them, but it seems, that all do CHARTS, (as you supply data, chart magiccally appears), not IMAGES (you draw rectangles, lines, text on some canvas and get the result as a (static) image) – gilhad Aug 24 '16 at 11:20
  • Thank you, exactly, what I needed. also here is simple example how to fill in django :) http://effbot.org/zone/django-pil.htm – gilhad Aug 24 '16 at 12:04
  • Great ! have fun. – navid Aug 24 '16 at 12:10
  • Included your example (customized) to my question :) – gilhad Aug 24 '16 at 12:11
0

http://matplotlib.org/ permits to generate plenty of great graphs. You should be able to save it as an image and integrate it to your webpage

Julien
  • 1,810
  • 1
  • 16
  • 34
  • I do not want to create permanent images, same them and then present them, I just want to combine some rectagles in memory and send it as image to user, then forgot it, as it never be generated the same again. – gilhad Aug 24 '16 at 11:22
0

What about plotly? Never used in a project, but by reading the examples it seems very powerful and easy to use. It has a static image export (as most graphic libraries probably have).

eguaio
  • 3,754
  • 1
  • 24
  • 38