BaseImage
Image class used by pygame-menu
.
Drawing image modes
Drawing mode |
Description |
---|---|
|
Image is drawn at the center of the surface |
|
Image fills the surface |
|
Image repeats on X axis |
|
Image repeats on both axis |
|
Image repeats on Y axis |
|
Just place the image in the top/left corner |
BaseImage - API
- class pygame_menu.baseimage.BaseImage(image_path, drawing_mode=101, drawing_offset=(0, 0), drawing_position='position-northwest', load_from_file=True, frombase64=False, image_id='')[source]
Object that loads an image, stores as a surface, transform it and let write the image to a surface.
- Parameters
image_path (
Union
[str
,Path
,BytesIO
]) – Path of the image to be loaded. It can be a string (path, base64),pathlib.Path
, orio.BytesIO
drawing_mode (
int
) – Drawing mode of the imagedrawing_offset (
Union
[Tuple
[Union
[int
,float
],Union
[int
,float
]],List
[Union
[int
,float
]]]) – Offset of the image in drawing methoddrawing_position (
str
) – Drawing position if mode isIMAGE_MODE_SIMPLE
. Seepygame_menu.locals
for validposition
valuesload_from_file (
bool
) – Loads the image from the given pathfrombase64 (
bool
) – IfTrue
considerimage_path
as base64 stringimage_id (
str
) – str
- apply_image_function(image_function)[source]
Apply a function to each pixel of the image. The function will receive the red, green, blue and alpha colors and must return the same values. The color pixel will be overridden by the function output.
Note
See
pygame_menu.baseimage.BaseImage.to_bw()
method as an example.
- checkpoint()[source]
Updates the original surface to the current surface.
- Return type
- Returns
Self reference
- flip(x, y)[source]
This method can flip the image either vertically, horizontally, or both. Flipping an image is non-destructive and does not change the dimensions.
- get_at(pos, ignore_alpha=False)[source]
Get the color from a certain position in image on x-axis and y-axis (x, y).
get_at
return a copy of the RGBA Color value at the given pixel. If the Surface has no per pixel alpha, then the alpha value will always be255
(opaque). If the pixel position is outside the area of the Surface anIndexError
exception will be raised.Getting and setting pixels one at a time is generally too slow to be used in a game or realtime situation. It is better to use methods which operate on many pixels at a time like with the blit, fill and draw methods - or by using pygame.surfarraypygame module for accessing surface pixel data using array interfaces/pygame.PixelArraypygame object for direct pixel access of surfaces.
- get_crop(x, y, width, height)[source]
Get a crop of the image from coordinate on x-axis and y-axis (x, y).
- get_rect()[source]
Return the
pygame.Rect
object of the BaseImage.This method returns a new rectangle covering the entire surface. The rectangle will always start at (0, 0) with a same width and height size as the image.
- Return type
- Returns
Pygame rect object
- pick_channels(channels)[source]
Pick certain channels of the image, channels are
"r"
(red),"g"
(green) and"b"
(blue);channels param
is a list/tuple of channels (non-empty).For example,
pick_channels(['r', 'g'])
: All channels not included on the list will be discarded.- Parameters
channels (
Union
[Literal
[‘r’, ‘g’, ‘b’],Tuple
[Literal
[‘r’, ‘g’, ‘b’],Literal
[‘r’, ‘g’, ‘b’]],Tuple
[Literal
[‘r’, ‘g’, ‘b’],Literal
[‘r’, ‘g’, ‘b’],Literal
[‘r’, ‘g’, ‘b’]],List
[Literal
[‘r’, ‘g’, ‘b’]]]) – Channels, list or tuple containing"r"
,"g"
or"b"
(all combinations are possible)- Return type
- Returns
Self reference
- resize(width, height, smooth=True)[source]
Resize the image to a desired (width, height) size in pixels.
- rotate(angle, auto_checkpoint=True)[source]
Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.
Note
Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise, pygame will pick a color that matches the image color-key or the topleft pixel value.
Warning
Image should be rotated once. If this method is called once the Class rotates the previously check-pointed state. If you wish to rotate the current image use
checkpoint
to update the surface. This may increase the image size, because the bounding rectangle of a rotated image is always greater than the bounding rectangle of the original image (except some rotations by multiples of 90 degrees). The image gets distort because of the multiply copies. Each rotation generates a small error (inaccuracy). The sum of the errors is growing and the images decays.- Parameters
- Return type
- Returns
Self reference
- scale(width, height, smooth=True)[source]
Scale the image to a desired width and height factor.
Note
The scale transformation is permanent, and is applied over the same object. Thus, if
image.scale(2, 2).scale(2, 2)
the final scale of the initial image is equal toimage.scale(4, 4)
.
- scale2x()[source]
This will return a new image that is double the size of the original. It uses the AdvanceMAME Scale2X algorithm which does a “jaggy-less” scale of bitmap graphics.
This really only has an effect on simple images with solid colors. On photographic and anti-aliased images it will look like a regular unfiltered scale.
- Return type
- Returns
Self reference
- scale4x()[source]
Applies a x4 scale factor using scale 2x algorithm.
- Return type
- Returns
Self reference
- set_alpha(value, flags=0)[source]
Set the current alpha value for the Surface. When blitting this Surface onto a destination, the pixels will be drawn slightly transparent. The alpha value is an integer from
0
to255
,0
is fully transparent and255
is fully opaque. If None is passed for the alpha value, then alpha blending will be disabled, including per-pixel alpha.This value is different from the per pixel Surface alpha. For a surface with per pixel alpha, blanket alpha is ignored and None is returned.
For pygame 2.0: per-surface alpha can be combined with per-pixel alpha.
The optional flags argument can be set to pygame.RLEACCEL to provide better performance on non accelerated displays. A RLEACCEL Surface will be slower to modify, but quicker to blit as a source.
- set_drawing_position(position)[source]
Set the image position.
Note
See
pygame_menu.locals
for validposition
values.