Save a texture to a file

Require PyMT 0.5

import pygame
from pymt import *
from OpenGL.GL import *

def texture_to_file(texture, filename):
    '''Take a PyMT texture, and save it into file. It require pygame.
    '
''
    # Activate texture
    texture.enable()
    texture.bind()
    # Copy to user land
    glCopyTexImage2D(texture.target, 0, GL_RGB, 0, 0,
                     texture.width, texture.height, 0)
    # Read data
    data = glGetTexImage(texture.target, 0, GL_RGB, GL_UNSIGNED_BYTE)
    # Deactivate texture
    texture.disable()
    # Write to jpeg
    surface = pygame.image.fromstring(str(buffer(data)), texture.size, 'RGB', True)
    pygame.image.save(surface, filename)

if __name__ == '__main__':
    img = Image('IMAG0023.jpg')
    texture_to_file(img.texture, 'my_output_image.jpg')