excolor.patch

class excolor.patch.Patch(coords=None, img=None, start=None)[source]

Bases: object

A class representing a patch that can be drawn on images.

A patch is defined by either a set of coordinates or a mask image, and can be filled with solid colors or gradients. It can also cast shadows or glows on images.

Parameters:
bbox

Bounding box of the patch [(x0, y0), (x1, y1)]

Type:

List[Tuple[int, int]]

mask

Binary mask where 1 indicates inside area and 0 outside

Type:

numpy.ndarray

start

Starting position (x, y) of the patch

Type:

Tuple[int, int]

__init__(coords=None, img=None, start=None)[source]

Initializes a Patch from coordinates or mask.

Parameters:
  • coords (List[Tuple[int, int]], optional) – List of (x, y) coordinates defining the patch boundary

  • img (PIL.Image.Image, optional) – Image where black pixels define the patch area

  • start (Tuple[int, int], optional) – Starting position (x, y) for the mask. Required if mask is provided.

Raises:

ValueError – If neither coords nor mask is provided If mask is provided without start_pos

fill_solid(color)[source]

Fills the patch with a solid color.

Parameters:

color (str) – Color to fill the patch with. Can be: - Color name (e.g., ‘red’) - Hex string (e.g., ‘#FF0000’)

Returns:

Image of the filled patch with transparency

Return type:

PIL.Image.Image

fill_gradient(colors, angle=0)[source]

Fills the patch with a gradient.

Parameters:
  • colors (str or List[str] or Colormap) – Colormap or color or list of colors for the gradient

  • angle (float, default=0) – Angle of the gradient in degrees

Returns:

Image of the gradient-filled patch with transparency

Return type:

PIL.Image.Image

cast_shadow(img, kernel=(31, 31), sigma=10, color='#000000')[source]

Casts a shadow of the patch.

Note:

Returns only shadow image. Use add_layer() to add shadow to an image.

param img:

Image to cast shadow onto

type img:

PIL.Image.Image

param kernel:

Size of the Gaussian kernel

type kernel:

Tuple[int, int], default=(31, 31)

param sigma:

Standard deviation of the Gaussian kernel

type sigma:

float, default=10

param color:

Color of the shadow

type color:

str, default=”#000000”

returns:

Image with shadow cast

rtype:

PIL.Image.Image

Parameters:
Return type:

Image

get_average_color(img)[source]

Calculates the average color of the area where the patch will be placed.

Parameters:

img (PIL.Image.Image) – Image to analyze

Returns:

Average color as hex string

Return type:

str

get_centroid_color(img)[source]

Calculates the color of the centroid of the patch.

Parameters:

img (PIL.Image.Image) – Image to analyze

Returns:

Centroid color as hex string

Return type:

str

get_darkest_color(img)[source]

Calculates the darkest color of the area where the patch will be placed.

Parameters:

img (PIL.Image.Image) – Image to analyze

Returns:

Darkest color as hex string

Return type:

str

get_lightest_color(img)[source]

Calculates the lightest color of the area where the patch will be placed.

Parameters:

img (PIL.Image.Image) – Image to analyze

Returns:

Lightest color as hex string

Return type:

str

draw(fig, size)[source]

Draws the patch fill and shadow on an image.

Parameters:
Return type:

None