imgrs

📦 Image Class API

Complete reference for the Image class - the core of imgrs.

Class: Image

The main image object that represents a raster image.

from imgrs import Image

Constructors

Image.open(fp, mode=None, formats=None)

Open an image file.

Parameters:

Returns: Image

Example:

# From file path
img = Image.open("photo.jpg")

# From Path object
from pathlib import Path
img = Image.open(Path("photo.png"))

# From bytes
with open("photo.jpg", "rb") as f:
    img = Image.open(f.read())

Image.new(mode, size, color=0)

Create a new image.

Parameters:

Returns: Image

Example:

# White RGB image
img = Image.new("RGB", (800, 600), color=(255, 255, 255))

# Transparent RGBA image
img = Image.new("RGBA", (400, 300), color=(0, 0, 0, 0))

# Gray image
img = Image.new("L", (640, 480), color=(128, 0, 0, 255))

Image.fromarray(array, mode=None)

Create an image from a NumPy array.

Parameters:

Returns: Image

Example:

import numpy as np

# RGB image from array
array = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
img = Image.fromarray(array)

# Grayscale image
gray = np.zeros((480, 640), dtype=np.uint8)
img = Image.fromarray(gray)

Properties

img.size

Get image dimensions.

Returns: Tuple[int, int] - (width, height)

Example:

width, height = img.size
print(f"Image is {width}x{height} pixels")

img.width

Get image width.

Returns: int

Example:

w = img.width  # e.g., 1920

img.height

Get image height.

Returns: int

Example:

h = img.height  # e.g., 1080

img.mode

Get image color mode.

Returns: str - One of: “L”, “LA”, “RGB”, “RGBA”, “I”

Example:

mode = img.mode  # "RGB"
if mode == "RGBA":
    print("Image has transparency")

img.format

Get original file format (if opened from file).

Returns: Optional[str] - e.g., “JPEG”, “PNG”, or None

Example:

fmt = img.format
if fmt == "JPEG":
    print("Opened from JPEG file")

I/O Operations

img.save(fp, format=None)

Save image to file.

Parameters:

Returns: None

Example:

# Save (format auto-detected from extension)
img.save("output.png")

# Force format
img.save("output.jpg", format="JPEG")

img.to_bytes()

Get raw pixel data as bytes.

Returns: bytes

Example:

data = img.to_bytes()
print(f"Image data: {len(data)} bytes")

img.copy()

Create a copy of the image.

Returns: Image

Example:

copy = img.copy()
# Modify copy without affecting original

See Also


Next: Explore specific operations in the API sections above.