imgutils.preprocess.pillow
This module provides utilities for image processing using the PIL library, allowing for transformations such as resizing, cropping, converting to tensor, normalization, and more. It supports both basic and composite transformations, which can be registered and parsed dynamically. The module also offers functionality to convert images to tensors and normalize them, making it suitable for preprocessing tasks in machine learning applications.
Transforms can be applied individually or composed into sequences using the PillowCompose class, which applies a list of transforms sequentially to an image. Each transformation can be registered with decorators to facilitate dynamic creation and parsing based on configuration dictionaries or lists, supporting flexible and configurable preprocessing pipelines.
register_pillow_transform
register_pillow_parse
create_pillow_transforms
- imgutils.preprocess.pillow.create_pillow_transforms(tvalue: list | dict)[source]
Create a transformation or a composition of transformations based on the input value.
- Parameters:
tvalue (Union[list, dict]) – A list or dictionary describing the transformation(s).
- Returns:
A transformation or a composition of transformations.
- Return type:
Union[PillowCompose, Any]
- Raises:
TypeError – If the input value is not a list or dictionary.
- Example:
>>> from imgutils.preprocess import create_pillow_transforms >>> >>> create_pillow_transforms({ ... 'type': 'resize', ... 'size': 384, ... 'interpolation': 'bicubic', ... }) PillowResize(size=384, interpolation=bicubic, max_size=None, antialias=True) >>> create_pillow_transforms({ ... 'type': 'resize', ... 'size': (224, 256), ... 'interpolation': 'bilinear', ... }) PillowResize(size=(224, 256), interpolation=bilinear, max_size=None, antialias=True) >>> create_pillow_transforms({'type': 'center_crop', 'size': 224}) PillowCenterCrop(size=(224, 224)) >>> create_pillow_transforms({'type': 'to_tensor'}) PillowToTensor() >>> create_pillow_transforms({'type': 'maybe_to_tensor'}) PillowMaybeToTensor() >>> create_pillow_transforms({'type': 'normalize', 'mean': 0.5, 'std': 0.5}) PillowNormalize(mean=[0.5], std=[0.5]) >>> create_pillow_transforms({ ... 'type': 'normalize', ... 'mean': [0.485, 0.456, 0.406], ... 'std': [0.229, 0.224, 0.225], ... }) PillowNormalize(mean=[0.485 0.456 0.406], std=[0.229 0.224 0.225]) >>> create_pillow_transforms([ ... {'antialias': True, ... 'interpolation': 'bicubic', ... 'max_size': None, ... 'size': 384, ... 'type': 'resize'}, ... {'size': (224, 224), 'type': 'center_crop'}, ... {'type': 'maybe_to_tensor'}, ... {'mean': 0.5, 'std': 0.5, 'type': 'normalize'} ... ]) PillowCompose( PillowResize(size=384, interpolation=bicubic, max_size=None, antialias=True) PillowCenterCrop(size=(224, 224)) PillowMaybeToTensor() PillowNormalize(mean=[0.5], std=[0.5]) )
parse_pillow_transforms
- imgutils.preprocess.pillow.parse_pillow_transforms(value)[source]
Parse transformations into a serializable format.
- Parameters:
value (Any) – The transformation or composition to parse.
- Returns:
A serializable representation of the transformation.
- Return type:
Union[list, dict]
- Raises:
TypeError – If the transformation cannot be parsed.
- Example:
>>> from PIL import Image >>> >>> from imgutils.preprocess import parse_pillow_transforms >>> from imgutils.preprocess.pillow import PillowResize, PillowCenterCrop, PillowMaybeToTensor, PillowToTensor, ... PillowNormalize >>> >>> parse_pillow_transforms(PillowResize( ... size=384, ... interpolation=Image.BICUBIC, ... )) {'type': 'resize', 'size': 384, 'interpolation': 'bicubic', 'max_size': None, 'antialias': True} >>> parse_pillow_transforms(PillowResize( ... size=(224, 256), ... interpolation=Image.BILINEAR, ... )) {'type': 'resize', 'size': (224, 256), 'interpolation': 'bilinear', 'max_size': None, 'antialias': True} >>> parse_pillow_transforms(PillowCenterCrop(size=224)) {'type': 'center_crop', 'size': [224, 224]} >>> parse_pillow_transforms(PillowToTensor()) {'type': 'to_tensor'} >>> parse_pillow_transforms(PillowMaybeToTensor()) {'type': 'maybe_to_tensor'} >>> parse_pillow_transforms(PillowNormalize(mean=0.5, std=0.5)) {'type': 'normalize', 'mean': [0.5], 'std': [0.5]} >>> parse_pillow_transforms(PillowNormalize( ... mean=[0.485, 0.456, 0.406], ... std=[0.229, 0.224, 0.225], ... )) {'type': 'normalize', 'mean': [0.48500001430511475, 0.4560000002384186, 0.4059999883174896], 'std': [0.2290000021457672, 0.2240000069141388, 0.22499999403953552]} >>> parse_pillow_transforms(PillowCompose([ ... PillowResize( ... size=384, ... interpolation=Image.BICUBIC, ... ), ... PillowCenterCrop(size=224), ... PillowMaybeToTensor(), ... PillowNormalize(mean=0.5, std=0.5), ... ])) [{'antialias': True, 'interpolation': 'bicubic', 'max_size': None, 'size': 384, 'type': 'resize'}, {'size': [224, 224], 'type': 'center_crop'}, {'type': 'maybe_to_tensor'}, {'mean': [0.5], 'std': [0.5], 'type': 'normalize'}]