0

How do you crop an image and save them as 2 OpenGL textures in PyGame?

This is my code for loading a full image:

def loadImage(image):
    img = pygame.image.load(image))
    textureData = pygame.image.tostring(img, "RGBA", 1)

    texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, texture)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.get_width(), img.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)

    return texture

Is it possible to create textureData with a cropped part of the image or can this be done with the OpenGL functions - I see that you could just replace img.get_height() with img.get_height()/2 if you want, but you wouldn't be able to choose a rectangle that doesn't start at (0,0) by changing those 4th and 5th parameters.

Matt Majic
  • 381
  • 9
  • 18
  • 1
    you can `blit()` part of image on new, smaller surface. Or try to use `pygame.Surface.subsurface()` - https://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface – furas Nov 19 '15 at 00:07
  • Thanks I used pygame.Surface.subsurface() and it worked – Matt Majic Nov 19 '15 at 11:22

0 Answers0