reply.it
“Once I get on a puzzle, I can’t get off.” Richard P. Feynman
Scale surface
Related Stack Overflow questions:
-
How can i reshape an array from (280, 280, 3) to (28, 28, 3)
-
How to change an image size in Pygame?
-
What does the parameter DestSurface in Pygame.transform.scale mean and how do I use it?
-
How do i properly rescale an image on PyGame without it being badly cropped?
-
Set the width and height of a pygame surface
-
Read more: Floating cooler for lake
Resize Image Content but Keep Image Dimensions
-
How can you draw more detailed/smoother images in pygame?
-
How to scale an image by window size?
pygame.transform.scale() does not scale the input Surface itself. It creates a new surface and does a scaled “blit” to the new surface. The new surface is returned by the return value:
pygame.transform.scale() does:
- Creates a new surface (newSurface) with size (width, height).
- Scale and copy Surface to newSurface.
- Return newSurface.
The destination surface is a pygame.Surface where the scaled surface is copied to. That is quicker, because the memory for the Surface has not to be allocated.
It DestSurface is set, then pygame.transform.scale() does:
- Scale and copy Surface to the DestSurface.
- Return DestSurface.
For that reason, the size of DestSurface has to be (width, height), the format of DestSurface has the same as the format of Surface.
A possible use case is if you have to continuously scale something to a fixed size in the main application loop (e.g. if the surface is dynamically generated). In the following surf is assumed to be a surface object:
Read more: Tw octane designs
The code can be improved by using the DestSurface parameter:
That is probably a rare case. In general you should try to scale the surfaces at the initialization, rather than continuously in the application loop and to use the scaled surfaces in the loop.
Do not try to use the parameter compulsively and do not “construct” a use case for the DestSurface parameter. Do it the other way around. Write your application and make it run. Then investigate whether the DestSurface parameter can be an improvement for your specific use case.
Transform scale and zoom surface
Related Stack Overflow questions:
-
How do I scale a PyGame image (Surface) with respect to its center? How to change an image size in Pygame?
? Minimal example – Spin
-
pygame.transform.scale does not work on the “game” surface
-
Pygame cannot make image bigger over time
Read more: Ida cannot find plugin
Define a zoom factor and calculate the size of the of the zoom area. e.g. If the zoom factor is 2, the area that needs to be zoomed on the window is half the width and height of the window:
Define the rectangular zoom area. The center point of the area is the position where to zoom to. (e.g the cursor position):
Create a new pygame.Surface with the size of the zoom area and copy the region of the window to the surface, by using ,blit, where the area parameter is set to the zoom region:
Scale zoom_surf by either pygame.transform.scale() or pygame.transform.smoothscale():
Now zoom_surf has the same size as the window. .blit the surface to the window:
Scale and zoom window
See Display, display position, resize, coordinate system and scroll – Scale and zoom window
Spin effect through scaling
Related Stack Overflow questions:
-
“Spin” coin image in python, pygame clicker game
? Minimal example – Spin