madam.image module
- class madam.image.FlipOrientation(value)[source]
Bases:
EnumRepresents an axis for image flip operations.
- HORIZONTAL = 0
Horizontal axis
- VERTICAL = 1
Vertical axis
- class madam.image.PillowProcessor(config: Mapping[str, Any] | None = None)[source]
Bases:
ProcessorRepresents a processor that uses Pillow as a backend.
- __init__(config: Mapping[str, Any] | None = None) None[source]
Initializes a new PillowProcessor.
- Parameters:
config – Mapping with settings.
- adjust_brightness(asset: Asset, factor: float) Asset[source]
Creates a new asset whose essence has adjusted brightness.
A factor of
0.0produces a black image. A factor of1.0returns an image identical to the input. Values above1.0increase brightness; values between0.0and1.0decrease it.
- adjust_contrast(asset: Asset, factor: float) Asset[source]
Creates a new asset whose essence has adjusted contrast.
A factor of
0.0produces a solid gray image (the mean color of the original). A factor of1.0returns an image identical to the input. Values above1.0increase contrast; values between0.0and1.0decrease it.
- adjust_saturation(asset: Asset, factor: float) Asset[source]
Creates a new asset whose essence has adjusted color saturation.
A factor of
0.0produces a greyscale image. A factor of1.0returns an image identical to the input. Values above1.0increase saturation; values between0.0and1.0decrease it.
- adjust_sharpness(asset: Asset, factor: float) Asset[source]
Creates a new asset whose essence has adjusted sharpness.
A factor of
0.0produces a blurred (smoothed) image. A factor of1.0returns an image identical to the input. Values above1.0sharpen the image; values between0.0and1.0blur it.
- apply_mask(asset: Asset, mask_asset: Asset) Asset[source]
Creates a new asset whose alpha channel is replaced by a mask image.
The luminance of
mask_assetcontrols the alpha of the output: white (255) is fully opaque and black (0) is fully transparent.mask_assetmust have the same dimensions as the base image. The output is always an RGBA PNG image.
- auto_orient(asset: Asset) Asset[source]
Creates a new asset whose essence is rotated according to the Exif orientation. If no orientation metadata exists or asset is not rotated, an identical asset object is returned.
- blur(asset: Asset, radius: float = 2) Asset[source]
Creates a new asset whose essence is blurred using a Gaussian kernel.
Higher
radiusvalues produce stronger blur. Aradiusof0leaves the image unchanged.
- can_read(file: IO) bool[source]
Returns whether the specified MIME type is supported by this processor.
- Parameters:
file (IO) – file-like object to be tested
- Returns:
whether the data format of the specified file is supported or not
- Return type:
- composite(asset: Asset, overlay_asset: Asset, x: int = 0, y: int = 0, gravity: str = 'north_west', opacity: float = 1.0) Asset[source]
Creates a new asset whose essence has another image composited on top.
The
overlay_assetis placed over the base image at the position determined by(x, y)orgravity. When bothx/yandgravityare specified,gravityis ignored and(x, y)is used directly.opacityscales the overlay’s alpha channel.- Parameters:
asset (Asset) – Base image asset
overlay_asset (Asset) – Asset to composite over the base
x (int) – Horizontal offset of the overlay from the left edge
y (int) – Vertical offset of the overlay from the top edge
gravity (str) – Anchor position when
xandyare both0; valid values are'north_west','north','north_east','west','center','east','south_west','south','south_east'opacity (float) – Overlay opacity in the range
[0.0, 1.0]
- Returns:
Asset with overlay composited onto the base
- Return type:
- convert(asset: Asset, mime_type: MimeType | str, color_space: str | None = None, depth: int | None = None, data_type: str | None = None) Asset[source]
Creates a new asset of the specified MIME type from the essence of the specified asset.
- Parameters:
- Returns:
New asset with converted essence
- Return type:
- crop(asset: Asset, width: int, height: int, x: int | None = None, y: int | None = None, gravity: str = 'north_west') Asset[source]
Creates a new asset whose essence is cropped to the specified rectangular area.
When
xandyare bothNone(the default), the crop window is positioned usinggravity. When either coordinate is supplied explicitly, both must be provided andgravityis ignored.Valid gravity values:
'north_west','north','north_east','west','center','east','south_west','south','south_east'.- Parameters:
asset (Asset) – Asset whose contents will be cropped
width (int) – Width of the cropping area
height (int) – Height of the cropping area
x (int or None) – Horizontal offset of the cropping area from the left edge, or
Noneto derive fromgravityy (int or None) – Vertical offset of the cropping area from the top edge, or
Noneto derive fromgravitygravity (str) – Anchor position used when
xandyare not specified
- Returns:
New asset with cropped essence
- Return type:
- crop_to_focal_point(asset: Asset, width: int, height: int, focal_x: float, focal_y: float) Asset[source]
Creates a new asset cropped to the given dimensions, keeping the specified focal point as close to the center of the output as possible.
The focal point is expressed as relative coordinates in the range
[0.0, 1.0], where(0.0, 0.0)is the top-left corner and(1.0, 1.0)is the bottom-right corner. The crop window is centered on the focal point and clamped so it stays within the image bounds.This operator is intentionally geometry-only: the caller is responsible for computing the focal-point coordinates via face detection, saliency analysis, or any other content-aware strategy.
- Parameters:
asset (Asset) – Asset whose essence will be cropped
width (int) – Crop width in pixels; must not exceed the source width
height (int) – Crop height in pixels; must not exceed the source height
focal_x (float) – Horizontal focal-point coordinate in
[0.0, 1.0]focal_y (float) – Vertical focal-point coordinate in
[0.0, 1.0]
- Returns:
Asset with cropped essence centered on the focal point
- Return type:
- Raises:
OperatorError – If the crop dimensions exceed the source dimensions
- extract_frame(asset: Asset, frame: int = 0) Asset[source]
Extracts a single frame from an animated image asset as a static image.
The returned asset has the same MIME type as the source. Use
frame_countfrom the asset metadata to know how many frames are available.- Parameters:
- Returns:
Static image asset for the requested frame
- Return type:
- Raises:
OperatorError – if frame is out of range
- fill_background(asset: Asset, color: tuple[int, int, int]) Asset[source]
Creates a new asset whose alpha channel is merged into a solid colour background.
Transparent and semi-transparent pixels are composited over the specified background colour. If the source image has no alpha channel, the pixels are returned unchanged. The output is always an opaque RGB image in the same format as the input.
- flip(asset: Asset, orientation: FlipOrientation) Asset[source]
Creates a new asset whose essence is flipped according the specified orientation.
- Parameters:
asset (Asset) – Asset whose essence is to be flipped
orientation (FlipOrientation) – axis of the flip operation
- Returns:
Asset with flipped essence
- Return type:
- optimize_quality(asset: Asset, min_ssim_score: float = 80.0, mime_type: str | MimeType | None = None, min_quality: int = 20, max_quality: int = 95) Asset[source]
Re-encodes asset at the lowest quality whose SSIMULACRA2 score against the original is at least min_ssim_score.
SSIMULACRA2 is a perceptual quality metric in (−∞, 100] where 100 means identical. Typical thresholds: ≥ 90 nearly imperceptible, ≥ 80 good quality, ≥ 70 acceptable.
The operator binary-searches quality values in [min_quality, max_quality] (Pillow
qualityscale, 1–95) and returns the highest-compression encoding that still meets the score threshold. If no quality value satisfies the constraint the result is encoded at max_quality (best achievable).Accepts lossless source formats (PNG, TIFF, …) when mime_type names a lossy target format. Requires the
ssimulacra2optional dependency (pip install "madam[analysis]").Supported output formats: JPEG, WebP, AVIF.
- Parameters:
asset – Source image asset (any format readable by Pillow)
min_ssim_score – Minimum acceptable SSIMULACRA2 score (default 80.0)
mime_type – Output MIME type; defaults to the asset’s own MIME type
min_quality – Lower bound for Pillow quality value (1–95)
max_quality – Upper bound for Pillow quality value (1–95)
- Returns:
Re-encoded image asset
- Raises:
OperatorError – if the target format does not support quality-based compression, or if ssimulacra2 is not installed
- pad(asset: Asset, width: int, height: int, color: tuple[int, int, int] | tuple[int, int, int, int] = (0, 0, 0, 0), gravity: str = 'center') Asset[source]
Creates a new asset whose essence is placed on a larger canvas.
The source image is pasted onto a canvas of size
(width, height)filled withcolor. The position on the canvas is determined bygravity.Valid gravity values:
'north_west','north','north_east','west','center','east','south_west','south','south_east'.- Parameters:
asset (Asset) – Asset whose essence will be padded
width (int) – Canvas width; must be >= the source image width
height (int) – Canvas height; must be >= the source image height
color (tuple) – Fill color for the added area as an RGB or RGBA tuple
gravity (str) – Anchor position of the source image on the canvas
- Returns:
Asset with padded essence
- Return type:
- Raises:
OperatorError – If the canvas is smaller than the source image
- read(file: IO) Asset[source]
Returns an
Assetobject whose essence is identical to the contents of the specified file.- Parameters:
file (IO) – file-like object to be read
- Returns:
Asset with essence
- Return type:
- Raises:
UnsupportedFormatError – if the specified data format is not supported
- resize(asset: Asset, width: int, height: int, mode: ResizeMode = ResizeMode.EXACT, gravity: str = 'center') Asset[source]
Creates a new Asset whose essence is resized according to the specified parameters.
In
FILLmode the image is scaled up until it covers the target dimensions, then cropped to the exact target size. Thegravityparameter controls which part of the scaled image is kept; it has no effect inEXACTorFITmode.Valid gravity values:
'north_west','north','north_east','west','center','east','south_west','south','south_east'.
- rotate(asset: Asset, angle: float, expand: bool = False) Asset[source]
Creates an asset whose essence is rotated by the specified angle in degrees.
- Parameters:
- Returns:
New asset with rotated essence
- Return type:
- round_corners(asset: Asset, radius: int) Asset[source]
Creates a new asset whose essence has rounded corners.
The corners are cut to the specified
radiususing a smooth circular mask. Pixels outside the rounded rectangle become fully transparent. The output is always an RGBA PNG image.
- sepia(asset: Asset) Asset[source]
Creates a new asset whose essence has a sepia tone applied.
The image is first converted to greyscale, then colorised with warm brown tones characteristic of historical photographs. The output is always an RGB image in the same format as the input.
- sharpen(asset: Asset, radius: float = 2, percent: int = 150, threshold: int = 3) Asset[source]
Creates a new asset whose essence is sharpened using an unsharp mask.
The unsharp mask works by subtracting a blurred version of the image from itself. Higher
percentvalues produce stronger sharpening;thresholdcontrols which pixel differences are sharpened.
- property supported_mime_types: frozenset
MIME types this processor can handle (used to build the Madam index).
- tint(asset: Asset, color: tuple[int, int, int], opacity: float = 0.5) Asset[source]
Creates a new asset whose essence is tinted with the specified color.
The tint is blended over the image at the given opacity. An
opacityof0.0leaves the image unchanged;1.0fills it entirely withcolor. The output is always an RGB image in the same format as the input.
- transpose(asset: Asset) Asset[source]
Creates a new image asset whose essence is the transpose of the specified asset’s essence.
- vignette(asset: Asset, strength: float = 0.5) Asset[source]
Creates a new asset whose essence has a radial vignette applied.
The vignette darkens the edges of the image while leaving the center unaffected.
strengthcontrols how much darkening is applied at the corners:0.0leaves the image unchanged;1.0makes the corners completely black.
- class madam.image.ResizeMode(value)[source]
Bases:
EnumRepresents a behavior for image resize operations.
- EXACT = 0
Image exactly matches the specified dimensions
- FILL = 2
Image is resized to completely fill the specified dimensions
- FIT = 1
Image is resized to fit completely into the specified dimensions
- madam.image.extract_palette(asset: Asset, count: int = 5) list[tuple[int, int, int]][source]
Extract the dominant colors from an image asset.
Quantizes the image to count representative colors using Pillow’s median-cut algorithm and returns them as
(r, g, b)tuples sorted by frequency (most frequent color first).The returned list contains at most count entries; it may be shorter if the image has fewer unique colors than count.
- Parameters:
asset – Source image asset (any format readable by Pillow)
count – Maximum number of colors to return (default 5)
- Returns:
List of
(r, g, b)tuples sorted by pixel frequency, descending
- madam.image.render_text(text: str, font_path: str | None = None, font_size: int = 24, color: tuple[int, int, int] = (0, 0, 0), background: tuple[int, int, int, int] = (0, 0, 0, 0), padding: int = 0) Asset[source]
Renders the given text into a new RGBA PNG image Asset.
The canvas is sized to fit the text exactly, with an optional uniform
paddingadded on all sides. A system font is used whenfont_pathisNone.- Parameters:
text (str) – Text string to render
font_path (str or None) – Path to a TrueType or OpenType font file, or
Noneto use the default Pillow fontfont_size (int) – Font size in points (ignored when
font_pathisNonebecause the default font has a fixed size)color (tuple[int, int, int]) – Text color as an
(red, green, blue)tuplebackground (tuple[int, int, int, int]) – Canvas background color as an
(r, g, b, alpha)tuple; defaults to fully transparentpadding (int) – Uniform padding in pixels added around the text
- Returns:
RGBA PNG Asset containing the rendered text
- Return type: