Skip to content

Image

keyed_extras.image

ImageSource module-attribute

ImageSource = str | Path | Image

Image sources.

Image

Factory class for creating shapes with image fills.

This class provides a fluent interface for creating shapes with image patterns. Initialize it with image pattern parameters, then use method chaining to specify the shape and its parameters.

Example
# Create a rectangle with an image fill
Image("assets/texture.jpg", scale=0.5).on_rect(scene, width=300, height=200)

# Create a circle with the same image
Image("assets/texture.jpg", scale=0.5).on_circle(scene, radius=100)

on_rect

on_rect(scene, width=100, height=100, x=None, y=None, radius=0, color=(1, 1, 1), alpha=1, dash=None, operator=OPERATOR_OVER, draw_stroke=True, line_width=2, round_tl=True, round_tr=True, round_br=True, round_bl=True)

Create a rectangle with this image as its fill pattern.

Parameters:

Name Type Description Default
scene Scene

The scene to which the rectangle belongs

required
width HasValue[float]

Width of the rectangle.

100
height HasValue[float]

Height of the rectangle.

100
x HasValue[float] | None

X position of the rectangle.

None
y HasValue[float] | None

Y position of the rectangle.

None
radius HasValue[float]

Corner radius of the rectangle.

0
color tuple[float, float, float]

RGB color tuple for the rectangle's stroke.

(1, 1, 1)
alpha float

Opacity from 0 (transparent) to 1 (opaque).

1
dash tuple[tuple[float, ...], float] | None

Dash pattern for the rectangle's stroke.

None
operator Operator

Cairo compositing operator.

OPERATOR_OVER
draw_stroke bool

Whether to draw the stroke.

True
line_width HasValue[float]

Width of the stroke line.

2
round_tl bool

Whether to round the top-left corner.

True
round_tr bool

Whether to round the top-right corner.

True
round_br bool

Whether to round the bottom-right corner.

True
round_bl bool

Whether to round the bottom-left corner.

True

Returns:

Type Description
Rectangle

A Rectangle object with the image as its fill pattern

on_circle

on_circle(scene, radius=50, x=None, y=None, color=(1, 1, 1), alpha=1, dash=None, operator=OPERATOR_OVER, draw_stroke=True, line_width=2)

Create a circle with this image as its fill pattern.

Parameters:

Name Type Description Default
scene Scene

The scene to which the circle belongs

required
radius HasValue[float]

Radius of the circle (default: 50)

50
x HasValue[float] | None

X position of the circle (default: scene center)

None
y HasValue[float] | None

Y position of the circle (default: scene center)

None
color tuple[float, float, float]

RGB color tuple for the circle's stroke (default: white)

(1, 1, 1)
alpha float

Opacity from 0 (transparent) to 1 (opaque) (default: 1)

1
dash tuple[tuple[float, ...], float] | None

Dash pattern for the circle's stroke (default: None)

None
operator Operator

Cairo compositing operator (default: OPERATOR_OVER)

OPERATOR_OVER
line_width float

Width of the stroke line (default: 2)

2

Returns:

Type Description
Circle

A Circle object with the image as its fill pattern

image_pattern

image_pattern(source, scale=1, dx=0, dy=0, extend=DEFAULT_EXTEND, filter=DEFAULT_FILTER)

Create an image pattern from an image file, URL, or PIL Image object that can be used as a fill or stroke source.

Parameters:

Name Type Description Default
source ImageSource

Image source

required
scale float

Scaling factor for the pattern

1
dx float

X offset for the pattern

0
dx float

Y offset for the pattern

0
extend Extend

How the pattern extends beyond its bounds. Options include:

DEFAULT_EXTEND
filter Filter DEFAULT_FILTER

Returns: A Cairo surface pattern configured according to the parameters

See Also

keyed_extras.image.Image.

Example
r = Rectangle(...)
r.fill_pattern = image_pattern("assets/texture.png", scale=0.5)