Reputation: 458
I can load entire images and work with them easily with
pygame.image.load("image.png")
but if I only want a tiny portion, I have to load the entire image and crop from there.
Is there a way in pygame to efficiently load only a portion of an image?
Upvotes: 3
Views: 9449
Reputation: 91
I recommended use DaFluffyPotato's code
def clip(surface, x, y, x_size, y_size): #Get a part of the image
handle_surface = surface.copy() #Sprite that will get process later
clipRect = pygame.Rect(x,y,x_size,y_size) #Part of the image
handle_surface.set_clip(clipRect) #Clip or you can call cropped
image = surface.subsurface(handle_surface.get_clip()) #Get subsurface
return image.copy() #Return
His channel https://www.youtube.com/c/DaFluffyPotato
Upvotes: 0
Reputation: 3664
Load your image
my_image = pygame.image.load("image.png")
Create a new surface
surf = pygame.Surface((X, Y))
X and Y are horizontal and vertical dimensions in px respectively.
Place the image on the surface
surf.blit( my_image, (A, B), (C, D, E, F) )
A and B are the distance from the top left corner. This places the image on the surface A px down and B px left.
C and D are the cropped part of the image from the top left corner. C px down, D px left.
E and F define the image size.
Upvotes: 5