BaseImage

class pygame_menu.baseimage.BaseImage(image_path, drawing_mode=101, drawing_offset=(0, 0), load_from_file=True)[source]

Object that loads an image, stores as a surface, transform it and let write the image to an surface.

Parameters
  • image_path (str, pathlib.Path) – Path of the image to be loaded. It can be a string or pathlib.Path on Python 3+

  • drawing_mode (int) – Drawing mode of the image

  • drawing_offset (tuple, list) – Offset of the image in drawing method

  • load_from_file (bool) – Loads the image from the given path

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 overriden by the function output.

Note

See BaseImage.to_bw() method as an example.

Parameters

image_function (callable) – Color function, takes colors as image_function=myfunc(r, g, b, a). Returns the same tuple (r,g,b,a)

Returns

Self reference

Return type

BaseImage

checkpoint()[source]

Updates the original surface to the current surface.

Returns

None

copy()[source]

Return a copy of the image.

Returns

Image

Return type

BaseImage

draw(surface, area=None, position=(0, 0))[source]

Draw the image in a given surface.

Parameters
  • surface (pygame.Surface) – Pygame surface object

  • area (pygame.Rect, None) – Area to draw, if None, Image will be drawn on entire surface

  • position (tuple) – Position to draw in (x, y)

Returns

None

equals(image)[source]

Return true if the image is the same as the object.

Parameters

image (pygame_menu.baseimage.BaseImage) – Image object

Returns

True if the image is the same (note, the surface)

Return type

bool

flip(x, y)[source]

This method can flip the image either vertically, horizontally, or both. Flipping a image is non-destructive and does not change the dimensions.

Parameters
  • x (bool) – Flip in x axis

  • y (bool) – Flip on y axis

Returns

Self reference

Return type

BaseImage

get_drawing_mode()[source]

Return the image drawing mode.

Returns

Image drawing mode

Return type

int

get_drawing_offset()[source]

Return the image drawing offset.

Returns

Image drawing offset

Return type

tuple

get_extension()[source]

Return the extension of the image file.

Returns

File extension

Return type

str

get_namefile()[source]

Return the name of the image file.

Returns

Filename

Return type

str

get_path()[source]

Return the image path.

Returns

Image path

Return type

str

get_rect()[source]

Return the rect of the image.

Returns

Pygame rect object

Return type

pygame.Rect

get_size()[source]

Return the size in pixels of the image.

Returns

(width,height)

Return type

tuple

get_surface()[source]

Return the surface object of the image.

Returns

Image surface

Return type

pygame.Surface

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 (tuple, list, str) – Channels, list or tuple containing ‘r’, ‘g’ or ‘b’ (all combinations are possible)

Returns

Self reference

Return type

BaseImage

resize(width, height, smooth=False)[source]

Set the image size to another size.

Parameters
  • width (int, float) – New width of the image in px

  • height (int, float) – New height of the image in px

  • smooth (bool) – Smooth scaling

Returns

Self reference

Return type

BaseImage

restore()[source]

Restore image to the original surface.

Returns

None

rotate(angle)[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 colorkey or the topleft pixel value.

Parameters

angle (int, float) – Rotation angle (degrees 0-360)

Returns

Self reference

Return type

BaseImage

scale(width, height, smooth=False)[source]

Scale the image to a desired width and height factor.

Parameters
  • width (int, float) – Scale factor of the width

  • height (int, float) – Scale factor of the height

  • smooth (bool) – Smooth scaling

Returns

Self reference

Return type

BaseImage

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 antialiased images it will look like a regular unfiltered scale.

Returns

Self reference

Return type

BaseImage

set_drawing_mode(drawing_mode)[source]

Set the image drawing mode.

Parameters

drawing_mode (int) – Drawing mode

Returns

None

set_drawing_offset(drawing_offset)[source]

Set the image drawing offset.

Parameters

drawing_offset (tuple, list) – Drawing offset tuple (x, y)

Returns

None

to_bw()[source]

Converts the image to black and white.

Returns

Self reference

Return type

BaseImage