I've written a little function that draws and saves to a file without
opening any windows. It's useful for scripting stuff and could be used
for regression testing. I've included the function with an example
below.

Is this there a better way to do this with pyglet? Is it guaranteed to
be portable across platforms?

/ Oscar Lindberg

#!/usr/bin/env python
# Saving an image from pyglet without displaying any window
# based on pyglet version of NeHe's OpenGL lesson03 (Philip Bober
[email protected])
# based on the pygame+PyOpenGL conversion by Paul Furber 2001 - 
[email protected]

from pyglet.gl import *
from pyglet import window

def save_opengl(draw_function, width, height, filename):
    win = window.Window(width, height, visible=False)
    draw_function(width, height)
    pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
    win.close()

def opengl_array(draw_function, width, height, format='RGB'):
    import numpy
    win = window.Window(width, height, visible=False)
    draw_function(width, height)
    cb = pyglet.image.get_buffer_manager().get_color_buffer()
    id = cb.get_image_data()
    a = numpy.fromstring(id.get_data(format, -width*len(format)), 'uint8')
    win.close()
    return a.reshape(height, width, -1)

def draw(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45/2, 1.0*width/height, 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glClearColor(0.4, 0.5, 0.9, 1.0)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()

    glTranslatef(0.0, 0.0, -6.0)

    glBegin(GL_TRIANGLES)
    glColor3f(1.0, 0.0, 0.0)
    glVertex3f(0.0, 1.0, 0.0)
    glColor3f(0.0, 1.0, 0.0)
    glVertex3f(-1.0, -1.0, 0)
    glColor3f(0.0, 0.0, 1.0)
    glVertex3f(1.0, -1.0, 0)
    glEnd()

def main():
    save_opengl(draw, 640, 480, 'screenshot.png')
    #direct = opengl_array(draw, 640, 480, format='I')

if __name__ == '__main__':
    main()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to