Skip to main content

ImageEffectBevel

ClassDataClassSource

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

The image effect: Bevel

Add the bevel effect to the alpha channel of the given image.

The "bevel" effect here is a replica of the "Chisel Hard" effect.

The algorithm is inspired by the following C++ implementation: https://dsp.stackexchange.com/a/533

Different from the above OpenCV version, this effect is purely implemented by Pillow. In other words, we implement the following algorithm from scratch:

  1. Determine the binarized version of the alpha channel.
  2. Given a specific max_distance value, for each pixel inside the alpha shape, calculate the Euclidean distance between the pixel and the nearest edge pixel. The "nearest" means that the distance is the minimal value.
  3. Run a 3x3 "bevel" filter on the solved distance map.
  4. Run any post-processing (such as smoothing) if necessary.
  5. Split the grayscale bevel layer into the highlight and shadow layers, merge them into the original image, respectively.

In this class, the "mode" is only referring to the mode of the highlight. The mode of the shadow is automatically chosen with respect to the highlight mode.

The default algorithm used for solving the distance is based on Squared Euclidean Distance Transform (Felzenszwalb & Huttenlocher Algorithm)

See https://cs.brown.edu/people/pfelzens/papers/seg-ijcv.pdf

The distance is strictly Euclidean. We provide an approximated version of this distance. Use "gaussian" algorithm to enable it. This approximation is faster but does not work well on sharp edges.

Fields​

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.
typeLiteral["bevel"]The type code of this effect. It should not be modified.
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.

Methods​

compose​

v_1: Image = self.compose(comp: ImageComposer, image_bg: Image, image_fg: Image)

Compose the background and foreground images.

This method do not need to be overriden unless the subclass needs to change the behavior of image blending.

Requires​

NameTypeRequired
Description
compImageComposerThe composer that is used for merging images.
image_bgImageThe background image where the effect will be rendered. This image should be RGB or RGBA.
image_fgImageThe image used for genereating this effect.

Returns​

NameType
Description
v_1ImageThe composed image.

effect​

v_1: Image | None = self.effect(image_fg: Image)

Generate the effect image from the given image.

Requires​

NameTypeRequired
Description
image_fgImageThe image used for genereating this effect.

Returns​

NameType
Description
v_1Image | None

The generated effect image. This image should be RGBA, and the size can be different from the input image.

This image should be center-aligned with the input image_fg.

Will return None if the effect should be skipped.