API Reference¶
Core optimization¶
optimize_image(source: Path | str, output: Path | str | None = None, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, strip_metadata: bool = True, output_format: OutputFormat = OutputFormat.AUTO, keep_aspect_ratio: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE, auto_orient: bool = True, progressive: bool = True, optimize: bool = True, overwrite: bool = False, lossless: bool = False, backup_dir: Path | str | None = None, min_size_bytes: int | None = None, keep_exif_groups: set[EXIFGroup] | None = None) -> OptimizationResult
¶
Optimize a single image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output
|
Path | str | None
|
Path for the optimized image. If None, overwrites source (if overwrite=True). |
None
|
max_width
|
int | None
|
Maximum width in pixels. None means no resize. |
None
|
max_height
|
int | None
|
Maximum height in pixels. None means no resize. |
None
|
quality
|
int
|
JPEG/WEBP quality (1-100). Higher is better quality, larger file. |
85
|
strip_metadata
|
bool
|
Remove EXIF and other metadata. |
True
|
output_format
|
OutputFormat
|
Target format. AUTO infers from output path or original. |
AUTO
|
keep_aspect_ratio
|
bool
|
Maintain aspect ratio when resizing (legacy when fit is None). |
True
|
fit
|
FitMode | str | None
|
Resize fit mode: down, cover, contain, fill. |
None
|
anchor
|
Anchor | str
|
Anchor point for cover/contain cropping and positioning. |
CENTER
|
aspect_ratio
|
tuple[int, int] | str | None
|
Target aspect ratio as '16:9' or (16, 9). |
None
|
background_color
|
tuple[int, int, int] | str
|
RGB tuple or hex color for contain padding. |
WHITE
|
auto_orient
|
bool
|
Apply EXIF orientation before processing. |
True
|
progressive
|
bool
|
Use progressive JPEG encoding. |
True
|
optimize
|
bool
|
Enable Pillow optimization flags. |
True
|
overwrite
|
bool
|
Allow overwriting the source file when output is None. |
False
|
lossless
|
bool
|
Use lossless compression for PNG/WEBP. Ignored for JPEG. |
False
|
backup_dir
|
Path | str | None
|
Directory to copy the original file into before processing. |
None
|
min_size_bytes
|
int | None
|
Skip files already smaller than this threshold (bytes). |
None
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult with details of the operation. |
Source code in pixopt/optimizer.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
batch_optimize(sources: Iterable[Path | str], output_dir: Path | str, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, strip_metadata: bool = True, output_format: OutputFormat = OutputFormat.AUTO, keep_aspect_ratio: bool = True, progressive: bool = True, optimize: bool = True, overwrite: bool = False, lossless: bool = False, backup_dir: Path | str | None = None, min_size_bytes: int | None = None, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE, auto_orient: bool = True, keep_exif_groups: set[EXIFGroup] | None = None, on_progress: ProgressCallback | None = None) -> BatchReport
¶
Optimize multiple image files and return an aggregated report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
Iterable[Path | str]
|
Iterable of source image paths. |
required |
output_dir
|
Path | str
|
Directory where all optimized images are written. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
BatchReport
|
class: |
Source code in pixopt/optimizer.py
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 | |
optimize_directory(source_dir: Path | str, output_dir: Path | str | None = None, *, recursive: bool = False, extensions: Iterable[str] | None = None, backup_dir: Path | str | None = None, min_size_bytes: int | None = None, on_progress: ProgressCallback | None = None, **kwargs: Any) -> list[OptimizationResult]
¶
Optimize all images in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path | str
|
Directory containing images. |
required |
output_dir
|
Path | str | None
|
Destination directory. If None, overwrites in-place. |
None
|
recursive
|
bool
|
Search subdirectories. |
False
|
extensions
|
Iterable[str] | None
|
File extensions to process. Defaults to common image types. |
None
|
backup_dir
|
Path | str | None
|
Directory to copy originals into before processing. |
None
|
min_size_bytes
|
int | None
|
Skip files already smaller than this threshold (bytes). |
None
|
on_progress
|
ProgressCallback | None
|
Optional callback invoked after each file is processed. |
None
|
**kwargs
|
Any
|
Passed to optimize_image. |
{}
|
Returns:
| Type | Description |
|---|---|
list[OptimizationResult]
|
List of OptimizationResult for each processed file. |
Source code in pixopt/optimizer.py
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
validate_optimize_params(*, quality: int, max_width: int | None, max_height: int | None, min_size_bytes: int | None) -> str | None
¶
Return an error message if any parameter is invalid, otherwise None.
Source code in pixopt/optimizer.py
change_extension(source: Path | str, output: Path | str | None = None, *, output_format: OutputFormat = OutputFormat.AUTO, backup_dir: Path | str | None = None, min_size_bytes: int | None = None, **kwargs: Any) -> OptimizationResult
¶
Convert an image to a different file format / extension.
This is a thin wrapper around optimize_image focused on format conversion. All other optimization parameters are forwarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output
|
Path | str | None
|
Destination path. If None, overwrites source (requires overwrite=True). |
None
|
output_format
|
OutputFormat
|
Target format. Defaults to inferring from output path. |
AUTO
|
backup_dir
|
Path | str | None
|
Directory to copy originals into before processing. |
None
|
min_size_bytes
|
int | None
|
Skip files already smaller than this threshold (bytes). |
None
|
**kwargs
|
Any
|
Passed to optimize_image. |
{}
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult with details of the conversion. |
Source code in pixopt/optimizer.py
convert_to_favicon(source: Path | str, output: Path | str | None = None, *, sizes: list[int] | None = None, background: tuple[int, int, int] = WHITE, keep_transparency: bool = True, auto_orient: bool = True) -> OptimizationResult
¶
Convert an image to a multi-resolution ICO favicon.
Generates a .ico file containing multiple square resolutions suitable for browser tabs, bookmarks and high-DPI displays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output
|
Path | str | None
|
Output .ico path. If None, uses source name with .ico extension. |
None
|
sizes
|
list[int] | None
|
List of square sizes to include. Default: [16, 32, 48, 64, 128, 256]. |
None
|
background
|
tuple[int, int, int]
|
RGB fill for transparent images when keep_transparency=False. |
WHITE
|
keep_transparency
|
bool
|
Preserve alpha channel if present. |
True
|
auto_orient
|
bool
|
Apply EXIF orientation before processing. |
True
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult with details of the operation. |
Source code in pixopt/optimizer.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 | |
Models¶
OptimizationResult(source_path: Path, output_path: Path, original_size: int, optimized_size: int, savings_bytes: int, savings_percent: float, width: int, height: int, format: str, metadata_removed: bool, success: bool, error: str | None = None)
dataclass
¶
Result of an image optimization operation.
Attributes¶
source_path: Path
instance-attribute
¶
output_path: Path
instance-attribute
¶
original_size: int
instance-attribute
¶
optimized_size: int
instance-attribute
¶
savings_bytes: int
instance-attribute
¶
savings_percent: float
instance-attribute
¶
width: int
instance-attribute
¶
height: int
instance-attribute
¶
format: str
instance-attribute
¶
metadata_removed: bool
instance-attribute
¶
success: bool
instance-attribute
¶
error: str | None = None
class-attribute
instance-attribute
¶
human_original_size: str
property
¶
Return the original size as a human-readable string.
human_optimized_size: str
property
¶
Return the optimized size as a human-readable string.
human_savings: str
property
¶
Return the saved bytes as a human-readable string.
OutputFormat
¶
Bases: str, Enum
Supported output formats.
Attributes¶
AUTO = 'auto'
class-attribute
instance-attribute
¶
JPEG = 'jpeg'
class-attribute
instance-attribute
¶
PNG = 'png'
class-attribute
instance-attribute
¶
WEBP = 'webp'
class-attribute
instance-attribute
¶
AVIF = 'avif'
class-attribute
instance-attribute
¶
ORIGINAL = 'original'
class-attribute
instance-attribute
¶
Placeholders¶
generate_placeholder(image_path: Path, *, placeholder_type: PlaceholderType | str = PlaceholderType.LQIP, lqip_size: int = 32, lqip_quality: int = 20) -> str
¶
Generate a placeholder string for an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
Path
|
Path to the source image. |
required |
placeholder_type
|
PlaceholderType | str
|
One of 'color', 'lqip', 'blurhash'. |
LQIP
|
lqip_size
|
int
|
Max thumbnail dimension for LQIP. |
32
|
lqip_quality
|
int
|
JPEG quality for LQIP. |
20
|
Returns:
| Type | Description |
|---|---|
str
|
A CSS color string, base64 data URI, or blurhash string. |
Source code in pixopt/placeholder.py
extract_dominant_color(img: Image.Image) -> str
¶
Return the dominant color of an image as a hex CSS string.
Uses a downsample + average approach for accuracy.
Source code in pixopt/placeholder.py
generate_lqip_datauri(img: Image.Image, *, size: int = 32, quality: int = 20) -> str
¶
Generate a tiny blurred placeholder image as a base64 data URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Image
|
Source PIL Image. |
required |
size
|
int
|
Maximum dimension of the thumbnail (maintains aspect ratio). |
32
|
quality
|
int
|
JPEG quality for the tiny image (low = smaller). |
20
|
Returns:
| Type | Description |
|---|---|
str
|
A base64 data URI string like 'data:image/jpeg;base64,/9j/4AAQ...'. |
Source code in pixopt/placeholder.py
generate_blurhash(img: Image.Image, *, components_x: int = 4, components_y: int = 3) -> str
¶
Generate a simplified blurhash-like string from an image.
This is a pure-Python approximation that encodes average colors of a grid into a compact base-83 string. It is NOT the official BlurHash algorithm, but produces visually similar short placeholders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Image
|
Source PIL Image. |
required |
components_x
|
int
|
Number of horizontal grid cells. |
4
|
components_y
|
int
|
Number of vertical grid cells. |
3
|
Returns:
| Type | Description |
|---|---|
str
|
A short blurhash-like string. |
Source code in pixopt/placeholder.py
Smart format detection¶
detect_optimal_format(image_path: Path | str, *, allow_lossy: bool = True, allow_lossless: bool = True, allow_animation: bool = True) -> OutputFormat
¶
Analyze an image and return the most efficient output format.
Rules
- Transparent image → WEBP (or PNG if lossless only)
- Animated image → WEBP
- Photograph with many colors → WEBP (or JPEG if no WEBP)
- Graphic/UI with few colors → WEBP lossless or PNG
Source code in pixopt/smart_format.py
has_transparency(img: Image.Image) -> bool
¶
Check if the image contains any transparent or semi-transparent pixels.
Source code in pixopt/smart_format.py
count_unique_colors(img: Image.Image, max_colors: int = MAX_UNIQUE_COLORS) -> int
¶
Count unique colors in the image, capped at max_colors.
Uses a histogram approach with reduced precision for performance.
Source code in pixopt/smart_format.py
is_photo(img: Image.Image) -> bool
¶
Heuristic: returns True if the image looks like a photograph.
Photos tend to have many unique colors and smooth gradients. Graphics/UI tend to have fewer colors and sharp edges.
Source code in pixopt/smart_format.py
Srcset generation¶
generate_srcset_images(source: Path | str, output_dir: Path | str, widths: list[int], *, quality: int = 85, output_format: str = 'WEBP', strip_metadata: bool = True, progressive: bool = True, optimize: bool = True, lossless: bool = False) -> list[SrcsetImage]
¶
Generate resized variants of an image for responsive srcset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output_dir
|
Path | str
|
Directory where variants will be saved. |
required |
widths
|
list[int]
|
List of target widths in pixels. Each variant will have this width, preserving aspect ratio. |
required |
quality
|
int
|
JPEG/WEBP quality (1-100). |
85
|
output_format
|
str
|
Pillow format string for output (e.g. "WEBP", "JPEG"). |
'WEBP'
|
strip_metadata
|
bool
|
Remove EXIF and other metadata. |
True
|
progressive
|
bool
|
Use progressive JPEG encoding. |
True
|
optimize
|
bool
|
Enable Pillow optimizer. |
True
|
lossless
|
bool
|
Use lossless compression for PNG/WEBP. |
False
|
Returns:
| Type | Description |
|---|---|
list[SrcsetImage]
|
List of SrcsetImage entries, sorted by width ascending. |
Source code in pixopt/srcset_generator.py
SrcsetImage(width: int, output_path: Path, size_bytes: int)
dataclass
¶
Adaptive quality¶
find_quality_for_target_size(img: Image.Image, pillow_fmt: str, target_size: int, *, max_width: int | None = None, max_height: int | None = None, keep_aspect_ratio: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE, strip_metadata: bool = True, progressive: bool = True, optimize: bool = True, lossless: bool = False, min_quality: int = 1, max_quality: int = 100, tolerance: float = 0.05, max_iterations: int = 8) -> int
¶
Find the JPEG/WEBP quality that produces a file closest to target_size.
Uses binary search over quality (1-100) and measures the actual encoded file size in memory. Returns the quality value that yields a size closest to but not exceeding the target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Image
|
Open PIL Image. |
required |
pillow_fmt
|
str
|
Target Pillow format (JPEG or WEBP). |
required |
target_size
|
int
|
Target file size in bytes. |
required |
max_width
|
int | None
|
Maximum width in pixels, or None. |
None
|
max_height
|
int | None
|
Maximum height in pixels, or None. |
None
|
keep_aspect_ratio
|
bool
|
Whether to keep the original aspect ratio. |
True
|
fit
|
FitMode | str | None
|
Resize fit mode. |
None
|
anchor
|
Anchor | str
|
Anchor point for cover/contain. |
CENTER
|
aspect_ratio
|
tuple[int, int] | str | None
|
Target aspect ratio. |
None
|
background_color
|
tuple[int, int, int] | str
|
Background color for contain padding. |
WHITE
|
strip_metadata
|
bool
|
Whether to strip metadata before saving. |
True
|
progressive
|
bool
|
Whether to use progressive encoding. |
True
|
optimize
|
bool
|
Whether to optimize the output. |
True
|
lossless
|
bool
|
Whether to use lossless compression. |
False
|
min_quality
|
int
|
Lowest quality to try. |
1
|
max_quality
|
int
|
Highest quality to try. |
100
|
tolerance
|
float
|
Fractional tolerance around target_size (e.g. 0.05 = 5%). |
0.05
|
max_iterations
|
int
|
Maximum binary-search iterations. |
8
|
Returns:
| Type | Description |
|---|---|
int
|
Quality integer (1-100). |
Source code in pixopt/adaptive_quality.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
Visual comparison¶
generate_comparison_html(before_path: Path, after_path: Path, output_html: Path, title: str = 'Image Comparison') -> Path
¶
Generate a self-contained HTML file with an interactive before/after slider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
before_path
|
Path
|
Path to the original image. |
required |
after_path
|
Path
|
Path to the optimized image. |
required |
output_html
|
Path
|
Path where the HTML file will be saved. |
required |
title
|
str
|
Page title displayed above the slider. |
'Image Comparison'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the generated HTML file. |
Source code in pixopt/html_comparison.py
Watermark¶
add_text_watermark(source: Path | str, output: Path | str, text: str, *, position: WatermarkPosition = WatermarkPosition.BOTTOM_RIGHT, opacity: float = 0.5, padding: int = 20, font_size: int = _DEFAULT_FONT_SIZE, font_path: Path | str | None = None, color: tuple[int, int, int] = (255, 255, 255)) -> WatermarkResult
¶
Add a text watermark to an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output
|
Path | str
|
Path for the output image. |
required |
text
|
str
|
Watermark text. |
required |
position
|
WatermarkPosition
|
Where to place the watermark. |
BOTTOM_RIGHT
|
opacity
|
float
|
Opacity from 0.0 (transparent) to 1.0 (opaque). |
0.5
|
padding
|
int
|
Pixel padding from the edge. |
20
|
font_size
|
int
|
Font size in pixels. |
_DEFAULT_FONT_SIZE
|
font_path
|
Path | str | None
|
Optional path to a TTF/OTF font file. |
None
|
color
|
tuple[int, int, int]
|
Text color as (R, G, B). |
(255, 255, 255)
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
WatermarkResult
|
class: |
Source code in pixopt/watermark.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
add_image_watermark(source: Path | str, output: Path | str, watermark: Path | str, *, position: WatermarkPosition = WatermarkPosition.BOTTOM_RIGHT, opacity: float = 0.5, padding: int = 20, scale: float | None = None) -> WatermarkResult
¶
Add an image watermark (logo/overlay) onto a base image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output
|
Path | str
|
Path for the output image. |
required |
watermark
|
Path | str
|
Path to the watermark image (PNG with alpha recommended). |
required |
position
|
WatermarkPosition
|
Where to place the watermark. |
BOTTOM_RIGHT
|
opacity
|
float
|
Opacity from 0.0 (transparent) to 1.0 (opaque). |
0.5
|
padding
|
int
|
Pixel padding from the edge. |
20
|
scale
|
float | None
|
Scale factor for the watermark relative to the base image width. If None, the watermark is used at its original size. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
WatermarkResult
|
class: |
Source code in pixopt/watermark.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
Sprite¶
create_sprite(images: Sequence[Path | str], output: Path | str, *, cell_width: int | None = None, cell_height: int | None = None, columns: int | None = None, layout: SpriteLayout | str = SpriteLayout.GRID, padding: int = 0, background: tuple[int, int, int] = WHITE, fmt: str = 'PNG') -> SpriteResult
¶
Combine multiple images into a single sprite sheet.
Each image is resized to fit within cell_width × cell_height
(keeping aspect ratio by default) and placed in a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Sequence[Path | str]
|
List of image file paths. |
required |
output
|
Path | str
|
Output sprite sheet path. |
required |
cell_width
|
int | None
|
Width of each cell. If None, uses the max image width. |
None
|
cell_height
|
int | None
|
Height of each cell. If None, uses the max image height. |
None
|
columns
|
int | None
|
Number of columns (grid layout). If None, auto-calculated. |
None
|
layout
|
SpriteLayout | str
|
|
GRID
|
padding
|
int
|
Pixels between cells. |
0
|
background
|
tuple[int, int, int]
|
Background color for empty areas. |
WHITE
|
fmt
|
str
|
Output image format (PNG, JPEG, WEBP). |
'PNG'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SpriteResult
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the image list is empty. |
Source code in pixopt/sprite.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
create_contact_sheet(images: Sequence[Path | str], output: Path | str, *, cell_width: int = 200, cell_height: int = 200, columns: int | None = None, padding: int = 10, background: tuple[int, int, int] = WHITE, label_height: int = 20, fmt: str = 'PNG') -> SpriteResult
¶
Create a contact sheet with labels showing image filenames.
A contact sheet is a grid of thumbnails with text labels below each image, similar to a photo proof sheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Sequence[Path | str]
|
List of image file paths. |
required |
output
|
Path | str
|
Output contact sheet path. |
required |
cell_width
|
int
|
Width of each thumbnail cell. |
200
|
cell_height
|
int
|
Height of each thumbnail cell. |
200
|
columns
|
int | None
|
Number of columns. If None, auto-calculated. |
None
|
padding
|
int
|
Pixels between cells. |
10
|
background
|
tuple[int, int, int]
|
Background color. |
WHITE
|
label_height
|
int
|
Height of the label area below each image. |
20
|
fmt
|
str
|
Output image format. |
'PNG'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SpriteResult
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the image list is empty. |
Source code in pixopt/sprite.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
Bundle¶
generate_asset_bundle(source: Path | str, output_dir: Path | str, *, options: BundleOptions | None = None) -> AssetBundle
¶
Generate a complete asset bundle from a single source image.
Produces hero image, thumbnail, og:image, favicon, srcset variants, LQIP data URI, blurhash, dominant color, and color palette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output_dir
|
Path | str
|
Directory where all generated assets are written. |
required |
options
|
BundleOptions | None
|
Configuration for what to generate and sizing parameters. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
An |
AssetBundle
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the source image does not exist. |
Source code in pixopt/bundle.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
In-memory I/O¶
optimize_bytes(data: bytes, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, output_format: OutputFormat | str = OutputFormat.WEBP, progressive: bool = True, optimize: bool = True, strip_metadata: bool = True, lossless: bool = False, auto_orient: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE) -> BytesResult
¶
Optimize an image from raw bytes and return optimized bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Raw image file bytes (JPEG, PNG, WEBP, etc.). |
required |
max_width
|
int | None
|
Maximum width in pixels. |
None
|
max_height
|
int | None
|
Maximum height in pixels. |
None
|
quality
|
int
|
JPEG/WEBP quality (1-100). |
85
|
output_format
|
OutputFormat | str
|
Target format as :class: |
WEBP
|
progressive
|
bool
|
Use progressive JPEG encoding. |
True
|
optimize
|
bool
|
Enable Pillow optimization flags. |
True
|
strip_metadata
|
bool
|
Remove EXIF and other metadata. |
True
|
lossless
|
bool
|
Use lossless compression for PNG/WEBP. |
False
|
auto_orient
|
bool
|
Apply EXIF orientation before processing. |
True
|
fit
|
FitMode | str | None
|
Resize fit mode: down, cover, contain, fill. |
None
|
anchor
|
Anchor | str
|
Anchor point for cover/contain cropping. |
CENTER
|
aspect_ratio
|
tuple[int, int] | str | None
|
Target aspect ratio as '16:9' or (16, 9). |
None
|
background_color
|
tuple[int, int, int] | str
|
RGB tuple or hex color for contain padding. |
WHITE
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
BytesResult
|
class: |
Source code in pixopt/io_bytes.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
optimize_base64(b64_str: str, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, output_format: OutputFormat | str = OutputFormat.WEBP, progressive: bool = True, optimize: bool = True, strip_metadata: bool = True, lossless: bool = False, auto_orient: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE) -> Base64Result
¶
Optimize an image from a base64 string and return base64 + metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b64_str
|
str
|
Base64-encoded image data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
Base64Result
|
class: |
Source code in pixopt/io_bytes.py
BytesResult(data: bytes, format: str, width: int, height: int, original_size: int, optimized_size: int, savings_bytes: int, savings_percent: float, success: bool, error: str | None = None)
dataclass
¶
Result of an in-memory image optimization.
Attributes¶
data: bytes
instance-attribute
¶
format: str
instance-attribute
¶
width: int
instance-attribute
¶
height: int
instance-attribute
¶
original_size: int
instance-attribute
¶
optimized_size: int
instance-attribute
¶
savings_bytes: int
instance-attribute
¶
savings_percent: float
instance-attribute
¶
success: bool
instance-attribute
¶
error: str | None = None
class-attribute
instance-attribute
¶
human_original_size: str
property
¶
human_optimized_size: str
property
¶
Methods:¶
to_dict() -> dict[str, Any]
¶
Source code in pixopt/io_bytes.py
Base64Result(base64: str | None, format: str, width: int, height: int, original_size: int, optimized_size: int, savings_bytes: int, savings_percent: float, success: bool, error: str | None = None)
dataclass
¶
Result of an in-memory base64 image optimization.
Attributes¶
base64: str | None
instance-attribute
¶
format: str
instance-attribute
¶
width: int
instance-attribute
¶
height: int
instance-attribute
¶
original_size: int
instance-attribute
¶
optimized_size: int
instance-attribute
¶
savings_bytes: int
instance-attribute
¶
savings_percent: float
instance-attribute
¶
success: bool
instance-attribute
¶
error: str | None = None
class-attribute
instance-attribute
¶
human_original_size: str
property
¶
human_optimized_size: str
property
¶
Methods:¶
to_dict() -> dict[str, Any]
¶
Source code in pixopt/io_bytes.py
PDF¶
images_to_pdf(images: Sequence[Path | str], output: Path | str, *, title: str | None = None) -> PdfExportResult
¶
Combine multiple images into a single PDF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Sequence[Path | str]
|
List of image file paths. Each image becomes one page. |
required |
output
|
Path | str
|
Output PDF file path. |
required |
title
|
str | None
|
Optional PDF document title metadata. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
PdfExportResult
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the image list is empty. |
Source code in pixopt/pdf_io.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
pdf_to_images(source: Path | str, output_dir: Path | str, *, dpi: int = 150, fmt: str = 'PNG', prefix: str | None = None) -> PdfImportResult
¶
Convert each page of a PDF to an image file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the PDF file. |
required |
output_dir
|
Path | str
|
Directory where page images will be saved. |
required |
dpi
|
int
|
Render resolution in DPI (default 150). |
150
|
fmt
|
str
|
Output image format ( |
'PNG'
|
prefix
|
str | None
|
Filename prefix. Defaults to the PDF stem. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
PdfImportResult
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the PDF does not exist. |
Source code in pixopt/pdf_io.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
Perceptual hashing¶
compute_hash(source: Path | str, *, algorithm: str = 'phash', hash_size: int = _DEFAULT_HASH_SIZE) -> HashResult
¶
Compute a perceptual hash of an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the image file. |
required |
algorithm
|
str
|
One of |
'phash'
|
hash_size
|
int
|
Hash size in bits per side (default 8 → 64-bit hash). |
_DEFAULT_HASH_SIZE
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
HashResult
|
class: |
Source code in pixopt/perceptual.py
phash(source: Path | str, *, hash_size: int = _DEFAULT_HASH_SIZE, highfreq_factor: int = 4) -> str
¶
Compute the perceptual hash (pHash) of an image.
Uses a DCT-based approach: resize to hash_size * highfreq_factor,
apply a slight blur, compute the DCT, and take the low-frequency
components compared to the median.
Returns:
| Type | Description |
|---|---|
str
|
A hex string representing the hash. |
Source code in pixopt/perceptual.py
ahash(source: Path | str, *, hash_size: int = _DEFAULT_HASH_SIZE) -> str
¶
Compute the average hash (aHash) of an image.
The image is resized to hash_size × hash_size grayscale, then each
pixel is compared to the mean: above → 1, below → 0.
Returns:
| Type | Description |
|---|---|
str
|
A hex string representing the hash. |
Source code in pixopt/perceptual.py
dhash(source: Path | str, *, hash_size: int = _DEFAULT_HASH_SIZE) -> str
¶
Compute the difference hash (dHash) of an image.
Compares each pixel to its right neighbor: left < right → 1.
Returns:
| Type | Description |
|---|---|
str
|
A hex string representing the hash. |
Source code in pixopt/perceptual.py
find_duplicates(hashes: list[HashResult], *, threshold: int = 5) -> list[DuplicateGroup]
¶
Find duplicate or near-duplicate images from a list of hashes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hashes
|
list[HashResult]
|
List of :class: |
required |
threshold
|
int
|
Maximum Hamming distance to consider images as duplicates. |
5
|
Returns:
| Type | Description |
|---|---|
list[DuplicateGroup]
|
A list of :class: |
list[DuplicateGroup]
|
that are within |
Source code in pixopt/perceptual.py
scan_duplicates(directory: Path | str, *, algorithm: str = 'phash', threshold: int = 5, recursive: bool = False, hash_size: int = _DEFAULT_HASH_SIZE) -> DuplicateReport
¶
Scan a directory for duplicate or near-duplicate images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path | str
|
Directory to scan. |
required |
algorithm
|
str
|
Hash algorithm: |
'phash'
|
threshold
|
int
|
Maximum Hamming distance for duplicates. |
5
|
recursive
|
bool
|
Scan subdirectories recursively. |
False
|
hash_size
|
int
|
Hash size per side. |
_DEFAULT_HASH_SIZE
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
DuplicateReport
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the directory does not exist. |
Source code in pixopt/perceptual.py
hamming_distance(hash_a: str, hash_b: str) -> int
¶
Compute the Hamming distance between two hex hash strings.
Returns:
| Type | Description |
|---|---|
int
|
The number of differing bits. 0 means identical. |
Source code in pixopt/perceptual.py
Async API¶
async_optimize_image(source: Path | str, output: Path | str | None = None, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, strip_metadata: bool = True, output_format: OutputFormat = OutputFormat.AUTO, keep_aspect_ratio: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE, auto_orient: bool = True, progressive: bool = True, optimize: bool = True, overwrite: bool = False, lossless: bool = False, backup_dir: Path | str | None = None, min_size_bytes: int | None = None, keep_exif_groups: set[EXIFGroup] | None = None) -> OptimizationResult
async
¶
Async variant of :func:pixopt.optimize_image.
Runs the synchronous optimization in a background thread.
Source code in pixopt/async_api.py
async_batch_optimize(sources: Sequence[Path | str], output_dir: Path | str, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, strip_metadata: bool = True, output_format: OutputFormat = OutputFormat.AUTO, keep_aspect_ratio: bool = True, progressive: bool = True, optimize: bool = True, overwrite: bool = False, lossless: bool = False, backup_dir: Path | str | None = None, min_size_bytes: int | None = None, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE, auto_orient: bool = True, keep_exif_groups: set[EXIFGroup] | None = None, max_concurrency: int = 4, on_progress: ProgressCallback | None = None) -> BatchReport
async
¶
Async variant of :func:pixopt.batch_optimize.
Processes images concurrently using max_concurrency parallel tasks,
each running in a background thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
Sequence[Path | str]
|
List of source image paths. |
required |
output_dir
|
Path | str
|
Output directory for optimized images. |
required |
max_concurrency
|
int
|
Maximum number of images to process in parallel. |
4
|
on_progress
|
ProgressCallback | None
|
Optional progress callback. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
BatchReport
|
class: |
Source code in pixopt/async_api.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
async_optimize_bytes(data: bytes, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, output_format: OutputFormat | str = OutputFormat.WEBP, progressive: bool = True, optimize: bool = True, strip_metadata: bool = True, lossless: bool = False, auto_orient: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE) -> BytesResult
async
¶
Async variant of :func:pixopt.optimize_bytes.
Runs in-memory optimization in a background thread.
Source code in pixopt/async_api.py
async_optimize_base64(b64_str: str, *, max_width: int | None = None, max_height: int | None = None, quality: int = 85, output_format: OutputFormat | str = OutputFormat.WEBP, progressive: bool = True, optimize: bool = True, strip_metadata: bool = True, lossless: bool = False, auto_orient: bool = True, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str = WHITE) -> Base64Result
async
¶
Async variant of :func:pixopt.optimize_base64.
Runs in-memory base64 optimization in a background thread.
Source code in pixopt/async_api.py
async_inspect_image(source: Path | str) -> ImageInfo
async
¶
Async variant of :func:pixopt.inspect_image.
Runs image inspection in a background thread.
Source code in pixopt/async_api.py
async_scan_directory(directory: Path | str, *, recursive: bool = False, extensions: Iterable[str] | None = None) -> ScanReport
async
¶
Async variant of :func:pixopt.scan_directory.
Runs directory scan in a background thread.
Source code in pixopt/async_api.py
async_scan_duplicates(directory: Path | str, *, algorithm: str = 'phash', threshold: int = 5, recursive: bool = True, hash_size: int = 8) -> DuplicateReport
async
¶
Async variant of :func:pixopt.scan_duplicates.
Runs duplicate scan in a background thread.
Source code in pixopt/async_api.py
Pipeline¶
Pipeline()
¶
Chainable image processing pipeline.
Build a sequence of operations and execute them in one call::
pipeline = (
Pipeline()
.open("photo.jpg")
.resize(max_width=800)
.watermark_text("© 2025", position="bottom-right")
.optimize(quality=85, output_format="webp")
.save("output/photo.webp")
)
result = pipeline.run()
Each method returns self for fluent chaining.
Source code in pixopt/pipeline.py
Attributes¶
steps: list[str]
property
¶
Return a human-readable list of queued operation names.
Methods:¶
open(source: Path | str) -> Pipeline
¶
Set the source image to process.
Source code in pixopt/pipeline.py
auto_orient() -> Pipeline
¶
resize(*, max_width: int | None = None, max_height: int | None = None, fit: FitMode | str | None = None, anchor: Anchor | str = Anchor.CENTER, aspect_ratio: tuple[int, int] | str | None = None, background_color: tuple[int, int, int] | str | None = None) -> Pipeline
¶
Resize the image with optional fit mode.
Source code in pixopt/pipeline.py
convert(mode: str = 'RGB') -> Pipeline
¶
watermark_text(text: str, *, position: WatermarkPosition | str = WatermarkPosition.BOTTOM_RIGHT, opacity: float = 0.5, padding: int = 20, font_size: int = 48, font_path: Path | str | None = None, color: tuple[int, int, int] = (255, 255, 255)) -> Pipeline
¶
Add a text watermark overlay.
Source code in pixopt/pipeline.py
watermark_image(watermark_path: Path | str, *, position: WatermarkPosition | str = WatermarkPosition.BOTTOM_RIGHT, opacity: float = 0.5, padding: int = 20, scale: float = 0.3) -> Pipeline
¶
Add an image watermark overlay.
Source code in pixopt/pipeline.py
optimize(*, quality: int = 85, output_format: OutputFormat | str = OutputFormat.WEBP, progressive: bool = True, optimize: bool = True, lossless: bool = False, strip_metadata: bool = True) -> Pipeline
¶
Configure optimization parameters for the final save.
Source code in pixopt/pipeline.py
save(output: Path | str) -> Pipeline
¶
Set the output path for the final image.
Source code in pixopt/pipeline.py
run() -> PipelineResult
¶
Execute all queued operations and return a :class:PipelineResult.
Raises:
| Type | Description |
|---|---|
ValueError
|
If no source image was set. |
FileNotFoundError
|
If the source image does not exist. |
Source code in pixopt/pipeline.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
PipelineResult(output_path: Path, width: int, height: int, format: str, size_bytes: int, steps_executed: list[str] = list())
dataclass
¶
Result of a pipeline execution.
Attributes¶
output_path: Path
instance-attribute
¶
width: int
instance-attribute
¶
height: int
instance-attribute
¶
format: str
instance-attribute
¶
size_bytes: int
instance-attribute
¶
steps_executed: list[str] = field(default_factory=list)
class-attribute
instance-attribute
¶
Methods:¶
to_dict() -> dict[str, Any]
¶
Image inspection¶
inspect_image(source: Path | str) -> ImageInfo
¶
Inspect an image file and return structured metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the image file to inspect. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
ImageInfo
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source does not exist. |
UnidentifiedImageError
|
If the file is not a valid image. |
Source code in pixopt/inspect.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
scan_directory(directory: Path | str, *, recursive: bool = False, extensions: Iterable[str] | None = None) -> ScanReport
¶
Scan a directory and return structured image info plus aggregate stats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path | str
|
Directory to scan. |
required |
recursive
|
bool
|
If True, scan subdirectories recursively. |
False
|
extensions
|
Iterable[str] | None
|
Optional list of file extensions to include (e.g. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ScanReport
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the directory does not exist. |
Source code in pixopt/inventory.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
Next-generation formats¶
convert_to_nextgen(source: Path | str, output: Path | str, *, fmt: NextGenFormat | str = NextGenFormat.JXL, quality: int = 85, fallback: bool = True, fallback_format: str = 'WEBP') -> ConversionResult
¶
Convert an image to a next-generation format.
If the target format is not supported and fallback is True,
converts to fallback_format instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path | str
|
Path to the source image. |
required |
output
|
Path | str
|
Output path. Extension will be adjusted if fallback occurs. |
required |
fmt
|
NextGenFormat | str
|
Target next-gen format ( |
JXL
|
quality
|
int
|
Quality for lossy compression (1-100). |
85
|
fallback
|
bool
|
If True, fall back to |
True
|
fallback_format
|
str
|
Format to use when falling back (default WEBP). |
'WEBP'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ConversionResult
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the source image does not exist. |
ValueError
|
If the format is not supported and fallback is disabled. |
Source code in pixopt/nextgen.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
Quality metrics¶
compare_images(original: Path | str, compared: Path | str) -> QualityMetrics
¶
Compare two images and return quality metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original
|
Path | str
|
Path to the original image. |
required |
compared
|
Path | str
|
Path to the image to compare against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
QualityMetrics
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If either path does not exist. |
ValueError
|
If images have different dimensions. |
Source code in pixopt/quality.py
compute_ssim(img1: npt.NDArray[Any], img2: npt.NDArray[Any], *, win_size: int = 7, data_range: float = 255.0) -> float
¶
Compute the Structural Similarity Index (SSIM) between two images.
Uses a sliding window approach with a Gaussian-free uniform window. Works on grayscale (2D) or multi-channel (3D) arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img1
|
NDArray[Any]
|
First image as a numpy array. |
required |
img2
|
NDArray[Any]
|
Second image as a numpy array (same shape as img1). |
required |
win_size
|
int
|
Side length of the sliding window (must be odd). |
7
|
data_range
|
float
|
Maximum pixel value (255 for 8-bit images). |
255.0
|
Returns:
| Type | Description |
|---|---|
float
|
SSIM value in [-1, 1], where 1 means identical structure. |
Source code in pixopt/quality.py
compute_psnr(mse: float, max_pixel: float = 255.0) -> float | None
¶
Compute PSNR from MSE.
Returns None if MSE is 0 (images are identical, PSNR = infinity).