Skip to main content

ImageLayer

ClassSource

ImageLayer(
parent: ImageLayerContainerProtocol,
name: str,
img: ImageLayerContentProtocol,
anchor: str | ImageAnchor = ImageAnchor.center,
related_to: str | None = None,
rel_anchor: str | ImageAnchor = ImageAnchor.center,
pos_shift: tuple[int, int] | None = None,
effects: ImageEffects | None = None,
)

A layer of image. It is typically used as a member of MultiLayerImage stack.

All instance operations of this class are non-inplace.

Arguments​

Requires​

NameTypeRequired
Description
parentImageLayerContainerProtocol

The canvas, i.e. the container that manages several image layers.

This layer is registered as a member of this container.

namestrThe name of this layer.
imgImageLayerContentProtocolThe image instance maintained by this layer.
anchorstr | ImageAnchorThe anchor of this image.
related_tostr | None

The name of another image layer. When rendering the image, will place this image based on the position of the image specified by related_to.

If not specified, the image layer will be put related to the canvas.

rel_anchorstr | ImageAnchorThe anchor of the reference image specified by related_to when placing this image.
pos_shifttuple[int, int] | NoneThe (h_shift, v_shift) of this image position. If not specified, will use (0, 0).
effectsImageEffects | NoneThe effects configured during the initialization. If not specified, will use an empty ImageEffects to configure this layer.

Methods​

add_overlay_color​

self.add_overlay_color(
color: str | tuple[float, ...],
mode: ImageComposerMode = "default",
pos_shift: tuple[int, int] = (0, 0),
)

Add a pure-color overlaying layer to the current layer.

Requires​

NameTypeRequired
Description
colorstr | tuple[float, ...]The RGB/RGBA color of the overlaying layer.
modeImageComposerModeThe blending mode of the overlaying effect.
pos_shifttuple[int, int]The shift of the overlaying content. By default, the content will be center-aligned with the giving shape. The overlaying content that is outside the shape mask will be cut off.

add_overlay_gradient​

self.add_overlay_gradient(
color_a: str | tuple[float, ...],
color_b: str | tuple[float, ...],
mode: ImageComposerMode = "default",
size: tuple[int | None, int | None] | None = None,
direction: Literal[
"left_to_right",
"top_to_bottom",
"rect_center_to_outside",
"radius_center_to_outside",
] = "left_to_right",
pos_shift: tuple[int, int] = (0, 0),
)

Add a linear-gradient overlaying layer to the current layer.

Requires​

NameTypeRequired
Description
color_astr | tuple[float, ...]The starting RGB/RGBA color of the overlaying layer.
color_bstr | tuple[float, ...]The ending RGB/RGBA color of the overlaying layer.
modeImageComposerModeThe blending mode of the overlaying effect.
sizetuple[int | None, int | None] | NoneThe size of the gradient. If not specified, will use the layer size.
directionLiteral["left_to_right", "top_to_bottom", "rect_center_to_outside", "radius_center_to_outside"]
  • "left_to_right"
  • "top_to_bottom"
  • "rect_center_to_outside"
  • "radius_center_to_outside"

The gradient direction. The value means the direction from color_a to color_b.

pos_shifttuple[int, int]The shift of the overlaying content. By default, the content will be center-aligned with the giving shape. The overlaying content that is outside the shape mask will be cut off.

add_overlay_image​

self.add_overlay_image(
image: Image,
mode: ImageComposerMode = "default",
size: tuple[int | None, int | None] | None = None,
feather_depth: int = 0,
pos_shift: tuple[int, int] = (0, 0),
)

Add an image-based overlaying layer to the current layer.

Requires​

NameTypeRequired
Description
imageImageThe image filled as the overlaying layer.
modeImageComposerModeThe blending mode of the overlaying effect.
sizetuple[int | None, int | None] | None

The size of the image. If not specified, will use the layer size.

If at least one of the size is not specified, will attempt to resize the image while preserving the ratio.

feather_depthintWhether to apply feathering edge mask to the image. If this value is 0, will use the image as it is.
pos_shifttuple[int, int]The shift of the overlaying content. By default, the content will be center-aligned with the giving shape. The overlaying content that is outside the shape mask will be cut off.

copy​

v_1: Self = self.copy()

Get a copy of this instance.

Returns​

NameType
Description
v_1SelfThe copy of the layer.

del_bevel​

self.del_bevel()

Delete the bevel effect of this layer.


del_glow​

self.del_glow()

Delete the glowing effect of this layer.


del_shadow​

self.del_shadow()

Delete the shadow effect of this layer.


del_stroke​

self.del_stroke()

Delete the glowing effect of this layer.


delete_overlays​

self.delete_overlays()

Delete all overlay effects. This method is equivalent to

self.effects.overlays.clear()

get_pos_abs​

v_1: _LayerPos = self.get_pos_abs(cache: MutableMapping[str, _LayerPos] | None = None)

Get the absolute position of this layer.

Requires​

NameTypeRequired
Description
cacheMutableMapping[str, _LayerPos] | None

The cache used for accelerating the position locating. Typically, this value is maintained in the container when rendering all layers.

If not specified, will not benefit from acceleration.

Returns​

NameType
Description
v_1_LayerPosThe position of the top left corner (anchor), and the (width, height) of this layer.

render​

v_1: Image = self.render(
comp: ImageComposer,
image_bg: Image,
cache: MutableMapping[str, _LayerPos] | None = None,
)

Render this layer.

Requires​

NameTypeRequired
Description
compImageComposerThe composer that is used for merging images.
image_bgImageThe background image where this layer will be rendered on. This image should be in RGB or RGBA.
cacheMutableMapping[str, _LayerPos] | None

The cache used for accelerating the position locating. Typically, this value is maintained in the container when rendering all layers.

If not specified, will not benefit from acceleration.

Returns​

NameType
Description
v_1ImageThe image_bg with this layer rendered on it.

set_bevel​

self.set_bevel(
mode: Literal["default", "screen", "lighter", "add"] = "default",
algorithm: Literal["euclidean", "gaussian"] = "euclidean",
threshold: int = 128,
max_distance: int = 3,
smooth: float = 1.0,
opacity: int = 75,
)

Set the glowing effect of this layer.

Requires​

NameTypeRequired
Description
modeLiteral["default", "screen", "lighter", "add"]The blending mode of the bevel effect. This value is the blending mode of the highlight. The mode of the bevel shadow will be automatically chosen based on this value.
algorithmLiteral["euclidean", "gaussian"]The algorithm used for solving the chisel distance. The "eudlidean" mode is strict, while the "gaussian" mode is a fast approximation.
thresholdintA threshold in the range of 0~255. This value is used for determining the edge of the bevel effect.
max_distanceintThe maximal distance when solving the depth of the bevel effect. When using "euclidean" algorithm, this value is strictly the same as the bevel depth.
smoothfloatThe size of the smoothing filter. The configuration smooth=0 is equivalent to "Chisel Hard" effect.
opacityintThe opacity of the effect. It is in the range of 0~100.

set_glow​

self.set_glow(
mode: ImageComposerMode = "screen",
size: int = 8,
color: str | tuple[float, ...] = "#FFFFFF",
)

Set the glowing effect of this layer.

Requires​

NameTypeRequired
Description
modeImageComposerModeThe blending mode of the glowing light.
sizeintThe size of the glowing light. This size can be viewed as "brush size" when creating the glowing light.
colorstr | tuple[float, ...]The RGB/RGBA color of the glowing light.

set_shadow​

self.set_shadow(
mode: ImageComposerMode = "multiply",
size: int = 4,
offset: int = 8,
color: str | tuple[float, ...] = "#000000",
)

Set the shadow effect of this layer.

Requires​

NameTypeRequired
Description
modeImageComposerModeThe blending mode of the shadow.
sizeintThe size of the shadow. This size can be viewed as "brush size" when creating the shadow.
offsetintThe offset of the shadow. It represents the distance between the shadow and the image.
colorstr | tuple[float, ...]The RGB/RGBA color of the shadow.

set_stroke​

self.set_stroke(
mode: ImageComposerMode = "default",
size: int = 2,
color: str | tuple[float, ...] = "#000000",
)

Set the glowing effect of this layer.

Requires​

NameTypeRequired
Description
modeImageComposerModeThe blending mode of the stroke.
sizeintThe size of the stroke. This size can be viewed as stroke width.
colorstr | tuple[float, ...]The RGB/RGBA color of the stroke.

Properties​

alpha​

alpha: Image | None = self.alpha

Get the alpha channel of this layer. It is equivalent to

self.img.img.getchannel("A")

The returned image is in the L mode.

If the image does not contain the alpha channel, will return None.


fmt​

fmt: ImageFormat = self.fmt

Attempt to get the image format from the content. If failed, return ImageFormat.png.


height​

height: int = self.height

The height of the image.


related: ImageLayer | ImageLayerContainerProtocol = self.related

The instance of the ImageLayer that is specified by self.related_to.

If self.related_to is None, return self.parent (canvas).


size​

size: tuple[int, int] = self.size

The (width, height) of the image.


width​

width: int = self.width

The width of the image.