psychos.visual.Image#

class psychos.visual.Image(image_path: PathStr, position: Tuple[float, float] = (0, 0), width: float | None = None, height: float | None = None, scale: float | None = None, rotation: float = 0, anchor_x: AnchorHorizontal = 'center', anchor_y: AnchorVertical = 'center', window: Window | None = None, coordinates: UnitType | Unit | None = None, **kwargs)[source]#

Bases: Sprite

A class to display an image in a Pyglet window using the Sprite component.

This class supports positioning, scaling, and rotation of the image, as well as custom anchor points.

Parameters:
  • image_path (PathStr) – The path to the image file to be displayed.

  • position (Tuple[float, float], default=(0, 0)) – The position of the image in the window, based on the defined coordinate system.

  • width (Optional[float], default=None) – The target width to scale the image to. Cannot be used in conjunction with height and scale.

  • height (Optional[float], default=None) – The target height to scale the image to. Cannot be used in conjunction with width and scale.

  • scale (Optional[float], default=None) – A uniform scaling factor for the image. Cannot be used if width or height is specified.

  • rotation (float, default=0) – The rotation angle of the image in degrees.

  • anchor_x (AnchorHorizontal, default="center") – The horizontal anchor alignment for the image.

  • anchor_y (AnchorVertical, default="center") – The vertical anchor alignment for the image.

  • window (Optional[Window], default=None) – The window in which the image will be displayed. If None, the default window is used.

  • coordinate_units (Optional[Union[UnitType, Units]], default=None) – The coordinate system to be used for positioning the image. If None, the window’s default unit system is used.

  • kwargs (dict) – Additional keyword arguments passed to the Pyglet Sprite class.

rotation#

The rotation of the image in degrees.

Type:

float

scale#

The scale factor of the image. If both width and height are provided, this will be a tuple of (scale_x, scale_y).

Type:

float

Examples

Basic usage with default positioning:

>>> image = Image("path/to/image.png")
>>> image.draw()

Scaling the image based on width:

>>> image = Image("path/to/image.png", width=200)
>>> image.draw()

Scaling the image based on height:

>>> image = Image("path/to/image.png", height=150)
>>> image.draw()

Using a custom scaling factor:

>>> image = Image("path/to/image.png", scale=2.0)
>>> image.draw()

Positioning the image at a specific coordinate:

>>> image = Image("path/to/image.png", position=(100, 200))
>>> image.draw()

Rotating the image by 45 degrees:

>>> image = Image("path/to/image.png", rotation=45)
>>> image.draw()

Custom anchor points (e.g., top-right):

>>> image = Image("path/to/image.png", anchor_x="right", anchor_y="top")
>>> image.draw()

Setting both width and height for non-uniform scaling:

>>> image = Image("path/to/image.png", width=200, height=300)
>>> image.draw()

Dynamically change position:

>>> image.position = (150, 250)
>>> image.draw()
__init__(image_path: PathStr, position: Tuple[float, float] = (0, 0), width: float | None = None, height: float | None = None, scale: float | None = None, rotation: float = 0, anchor_x: AnchorHorizontal = 'center', anchor_y: AnchorVertical = 'center', window: Window | None = None, coordinates: UnitType | Unit | None = None, **kwargs)[source]#

Create a Sprite instance.

Parameters:
  • img – Image or Animation to display.

  • x – X coordinate of the sprite.

  • y – Y coordinate of the sprite.

  • z – Z coordinate of the sprite.

  • blend_src – OpenGL blend source mode. The default is suitable for compositing sprites drawn from back-to-front.

  • blend_dest – OpenGL blend destination mode. The default is suitable for compositing sprites drawn from back-to-front.

  • batch – Optional batch to add the sprite to.

  • group – Optional parent group of the sprite.

  • subpixel – Allow floating-point coordinates for the sprite. By default, coordinates are restricted to integer values.

  • program – A specific shader program to initialize the sprite with. By default, a pre-made shader will be chosen based on the texture type passed.

Added in version 2.0.16: The program parameter.

Methods

__init__(image_path[, position, width, ...])

Create a Sprite instance.

delete()

Force immediate removal of the sprite from video memory.

dispatch_event(event_type, *args)

Dispatch an event to the attached event handlers.

draw()

Draw the sprite at its current position.

event(*args)

Function decorator for an event handler.

get_sprite_group()

Creates and returns a group to be used to render the sprite.

pop_handlers()

Pop the top level of event handlers off the stack.

post_event(event_type, *args)

Post an event to the main application thread.

push_handlers(*args, **kwargs)

Push a new level onto the handler stack, and add 0 or more handlers.

register_event_type(name)

Register an event type with the dispatcher.

remove_handler(name, handler)

Remove a single event handler.

remove_handlers(*args, **kwargs)

Remove event handlers from the event stack.

set_handler(name, handler)

Attach a single event handler.

set_handlers(*args, **kwargs)

Attach one or more event handlers to the top level of the handler stack.

update([x, y, z, rotation, scale, scale_x, ...])

Simultaneously change the position, rotation or scale.

Attributes

batch

Graphics batch.

blend_mode

The current blend mode applied to this sprite.

color

Blend color.

coordinates

Get the coordinate system used for the text.

event_types

frame_index

The current Animation frame.

group

Parent graphics group specified by the user.

height

Scaled height of the sprite.

image

The Sprite's Image or Animation to display.

opacity

Blend opacity.

paused

Pause/resume the Sprite's Animation.

position

Get the position of the image.

program

The current shader program.

rotation

Clockwise rotation of the sprite, in degrees.

scale

Base Scaling factor.

scale_x

Horizontal scaling factor.

scale_y

Vertical scaling factor.

visible

True if the sprite will be drawn.

width

Scaled width of the sprite.

x

X coordinate of the sprite.

y

Y coordinate of the sprite.

z

Z coordinate of the sprite.

property coordinates: Unit#

Get the coordinate system used for the text.

draw() Image[source]#

Draw the sprite at its current position.

See the module documentation for hints on drawing multiple sprites efficiently.

property position: Tuple[float, float]#

Get the position of the image.