views:

1701

answers:

9
+2  Q: 

Python Canvas

I'm looking for a Python library for creating canvases for manipulating gemetric shapes. Specifically I need the ability to create arbitrary polygons and place them on the canvas, the polygons need to have the ability to be transparent/have an alpha channel, I need to be able to edit polygons that are currently on the canvas, and I need to be able to get the actual color of a given pixel(the aggregate of all the transparent piece that are there).

Basically I'm trying to make this: http://alteredqualia.com/visualization/evolve/ in python.

+1  A: 

Pygame [http://pygame.org/] should be able to do this for you.

See pygame.draw.polygon

too much php
A: 

The problem with that(just like GTK's drawing area and Tk's canvas) is that I don't see a way to do transparency/alpha channel. Is there a a way in these?

Alex Gaynor
Write these responses as comments to the answer not as a separate answer, the sort order of answers is not guarantied to stay appropriate.
Toni Ruža
+1  A: 

I think cairo will do a lot of what you want. They have python bindings, too.

The one requirement that that won't help you with is modifying previously-drawn polygons, but I don't know of any canvas that will do that for you.

A: 

I believe the HTML canvas lets you modify elements, which makes me believe there might be another canvas that can as well. However, if there is not that would basically require me to keep a separate list of all the polygons and when I wanted to make a change, alter the item in the list and then create a new canvas, which seems like it would have a significant overhead.

Alex Gaynor
+1  A: 

Sounds like a job for OpenGL.

My advice is that, whichever library you choose, you make a data structure for your polygons that suits your algorithms so that they can be more simple and readable rather then try to get these algorithms to manipulate a canvas directly. Then you can write the code that draws them separate (i.e. independent) of the main logic.

Toni Ruža
A: 

Both Qt and wxWidgets have some canvas drawing abilities (Qt calls it GraphicsView). Quick Google searches will get you a lot of examples so you can see if it fits your requirements.

Mosor
+2  A: 

This discussion on Stackoverflow has some comparisons and code snippets on various GUI toolkits for Python. I'm pretty sure that the QGraphicsView on QT will do transparency. Nokia (nee Troll) make a demo suite for QT that should give you an idea of its capabilities.

ConcernedOfTunbridgeWells
+1  A: 

Try pyglet. It is a graphics library for Python with OpenGL. If you've done OpenGL programming before, it is certainly the easiest way to get what you want.

Herge
A: 

I believe the HTML canvas lets you modify elements

It does not. You can check out my HTML canvas tutorial to see how you draw a moving ball; you wipe the screen and draw a new circle at the spot you want.

You can draw simple shapes to a canvas in all of pyglet, pygame, QT, Tkinter, wxPython and cairo.

Generally, you will have objects called "sprites" or "shapes" that represent objects drawn to the screen, and you'll store them all in a container. Then the library or framework will, at every frame, render them all to the canvas. Thus it will seem to the user (you) that you can modify the objects on screen; you set a ball's x and y coordinates and in the next frame it's rendered there. However, at a low level, everything's being wiped and redrawn again.

For computationally intensive animation, a technique called double-buffering will be employed whereby a bitmap in memory will be modified instead of the one onscreen, and then the drawing process will simply be to copy that bitmap to the screen.

alter the item in the list and then create a new canvas, which seems like it would have a significant overhead.

All of the frameworks mentioned above will give you a nice abstraction for the list of objects to draw, so that you won't need to maintain it manually, and you can program as if the sprites/shapes you've drawn can be directly moved onscreen, even though they really aren't at a low level.

llimllib
"you wipe the screen and draw a new circle at the spot you want" - while that is true, it's not a real problem performance-wise. i did something similar, and most of the time goes into comparing the images (evaluating the fitness), not drawing. the penalty shouldn't be too big.
Schnalle
I wasn't saying that it was a performance problem, just that you can't modify elements that are on the canvas; you have to draw over anything that's changed.
llimllib