Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pixelwise drawing in pyglet (python)

OK. I'm tired of googling and reading throught lots of documentation with no results.

My aim is simple: get pyglet to draw an image pixel by pixel.

I've been searching for hours with no results. Can anyone give an example of a short program that draws in a display specifying the color pixel by pixel? For example: drawing a gradient from black to white.

like image 615
Manuel Araoz Avatar asked Aug 20 '10 02:08

Manuel Araoz


1 Answers

As long as you realize this is going to take a long time...:

pyglet.graphics.draw can drawn one or more points when you pass it pyglet.gl.GL_POINTS, and you can pass attributes such as color as well as coordinates. For example:

for i in range(50):
    for j in range(50):
        color = int(2.56 * (i + j))
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
            ('v2i', (i, j)),
            ('c3B', (color, color, color))
        )

draws a 50 by 50 square with a diagonal gradient from black to white-ish. Just don't expect it to be particularly fast at doing that;-) -- GL is really oriented to graphics with much higher level of abstraction, not "pixel by pixel" painting.

You could get a modicum of extra speed by computing (say) a row at a time, and drawing that, instead of actually drawing pixels singly. But it still won't be super-fast!-)

like image 172
Alex Martelli Avatar answered Sep 24 '22 16:09

Alex Martelli