imgrs

🔄 Transform API

Complete reference for image transformation operations.

Geometric Transforms

img.resize(size, resample=None)

Resize image to new dimensions.

Parameters:

Returns: Image

Example:

# Resize to specific size
resized = img.resize((800, 600))

# High quality resize
hq = img.resize((1920, 1080), resample="LANCZOS")

# Fast resize for thumbnails
thumb = img.resize((150, 150), resample="NEAREST")

Tips:


img.crop(box)

Crop image to a rectangular region.

Parameters:

Returns: Image

Example:

# Crop center 400x300 region starting at (100, 100)
cropped = img.crop((100, 100, 400, 300))

# Crop top-left quarter
w, h = img.size
quarter = img.crop((0, 0, w // 2, h // 2))

# Extract face region
face = img.crop((250, 150, 300, 350))

Validation:


img.rotate(angle, expand=False, fillcolor=None, resample=None, center=None, translate=None)

Rotate image by specified angle with advanced options.

Parameters:

Returns: Image

Examples:

# Basic rotations
rotated_90 = img.rotate(90)      # 90° clockwise
rotated_180 = img.rotate(180)    # 180°
rotated_270 = img.rotate(270)    # 270° clockwise

# Arbitrary angles
rotated_45 = img.rotate(45)      # 45° rotation
rotated_30 = img.rotate(30)      # 30° rotation

# Expand vs crop behavior
cropped = img.rotate(45, expand=False)    # Keep original size
expanded = img.rotate(45, expand=True)    # Expand to fit

# Fill expanded areas
filled = img.rotate(45, expand=True, fillcolor=(255, 0, 0))  # Red background

# Negative angles (clockwise)
clockwise = img.rotate(-90)  # 90° clockwise

Rotation Behavior:

Convenience Methods:

# Easy aliases
img.rotate90()    # Same as rotate(90)
img.rotate180()   # Same as rotate(180)
img.rotate270()   # Same as rotate(270)
img.rotate_left() # Same as rotate(90)
img.rotate_right() # Same as rotate(-90)

Performance Notes:


img.transpose(method)

Flip or transpose the image.

Parameters:

Returns: Image

Example:

# Mirror horizontally
mirrored = img.transpose("FLIP_LEFT_RIGHT")

# Mirror vertically
upside_down = img.transpose("FLIP_TOP_BOTTOM")

# Rotate (alternative to rotate())
rotated = img.transpose("ROTATE_90")

Color Conversion

img.convert(mode)

Convert image to different color mode.

Parameters:

Returns: Image

Example:

# Convert to grayscale
gray = img.convert("L")

# Add alpha channel
with_alpha = img.convert("RGBA")

# Remove alpha channel
no_alpha = img.convert("RGB")

Conversion table:

RGB  → L     : Grayscale conversion
RGB  → RGBA  : Adds opaque alpha
RGBA → RGB   : Removes alpha
RGBA → L     : Grayscale, ignores alpha
L    → RGB   : Duplicates gray to RGB

img.split()

Split image into individual channels.

Parameters: None

Returns: List[Image] - List of grayscale images (one per channel)

Example:

# Split RGB image
r, g, b = img.split()
r.save("red_channel.png")

# Split RGBA image
r, g, b, a = img.split()
a.save("alpha_channel.png")

# Grayscale image
gray_channels = img.split()
# Returns: [img] (single channel)

Channel order:


Compositing

img.paste(other, position=None, mask=None)

Paste another image onto this image.

Parameters:

Returns: Image

Example:

# Paste at top-left
result = base.paste(overlay)

# Paste at specific position
result = base.paste(overlay, position=(100, 50))

# Paste with mask (alpha blending)
mask = Image.new("L", overlay.size, color=(128, 0, 0, 255))
result = base.paste(overlay, position=(50, 50), mask=mask)

Notes:


Utility Operations

img.copy()

Create an independent copy.

Returns: Image

Example:

original = Image.open("photo.jpg")
copy = original.copy()

# Modify copy, original unchanged
copy = copy.blur(5.0)

Transformation Pipelines

Chain transforms for complex operations:

# Create thumbnail pipeline
def create_thumbnail(img, size=(150, 150)):
    return (img
        .resize(size, resample="LANCZOS")
        .sharpen(1.2)
        .contrast(1.1))

# Use it
thumb = create_thumbnail(img)
# Photo enhancement pipeline
def enhance_photo(img):
    return (img
        .resize((1920, 1080), resample="LANCZOS")
        .brightness(5)
        .contrast(1.15)
        .saturate(1.1)
        .sharpen(1.3))

result = enhance_photo(img)

Performance Notes

Operation Complexity Notes
resize() O(n×m) LANCZOS slower than NEAREST
crop() O(1) Very fast
rotate() O(n×m) Arbitrary angles with bilinear interpolation
transpose() O(n×m) Fast
convert() O(n×m) Per-pixel conversion
split() O(n×m) Creates copies
paste() O(n×m) Alpha blending overhead

See Also: