API Reference
photoff.core.types
CudaImage
Represents an image stored in GPU memory with optional dimension constraints.
The image has an underlying GPU buffer and stores its logical and allocated dimensions. Use .width
and .height
to manage the actual used size, while the allocation size is fixed on creation. Memory is managed via create_buffer
and free_buffer
.
Attributes:
Name | Type | Description |
---|---|---|
width | int | Logical width (can be set lower than allocated width). |
height | int | Logical height (can be set lower than allocated height). |
buffer | CudaBuffer | Pointer to the underlying CUDA buffer. |
Methods:
Name | Description |
---|---|
init_image | Allocates the GPU buffer if not already allocated. |
free | Frees the associated GPU buffer. |
Example
img = CudaImage(512, 512) img.width = 256 # Use only part of the allocation img.free()
Source code in photoff\core\types.py
__init__(width, height, auto_init=True)
Initializes a new CudaImage with specified dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
width | int | Allocation and initial logical width in pixels. | required |
height | int | Allocation and initial logical height in pixels. | required |
auto_init | bool | Whether to automatically allocate the buffer. Defaults to True. | True |
Source code in photoff\core\types.py
RGBA
dataclass
Represents an RGBA color with 8-bit channels.
Attributes:
Name | Type | Description |
---|---|---|
r | int | Red channel (0 – 255). |
g | int | Green channel (0 – 255). |
b | int | Blue channel (0 – 255). |
a | int | Alpha channel (0 – 255), defaults to 255 (opaque). |
Example
color = RGBA(255, 0, 0) # Opaque red transparent_black = RGBA(0, 0, 0, 0)
Source code in photoff\core\types.py
photoff.io
image_to_pil(image)
Converts a CudaImage to a PIL.Image in RGBA format.
The image is copied from GPU memory to host memory and returned as a Pillow image object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The image in GPU memory. | required |
Returns:
Type | Description |
---|---|
Image | PIL.Image: A new PIL Image with RGBA channels. |
Example
pil_img = image_to_pil(cuda_img) pil_img.show()
Source code in photoff\io\__init__.py
load_image(filename, container=None)
Loads an image from disk and transfers it to a CudaImage.
The image is loaded using Pillow and converted to RGBA format. It is then copied to GPU memory. Optionally, a pre-allocated CudaImage container can be used to avoid allocation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename | str | Path to the image file to load. | required |
container | CudaImage | Pre-allocated image buffer. Must be large enough to hold the image. | None |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | A new or reused image object with the loaded data. |
Raises:
Type | Description |
---|---|
ValueError | If the input image is larger than the provided container. |
Example
cuda_img = load_image("texture.png")
Source code in photoff\io\__init__.py
save_image(image, filename)
Saves a CudaImage to disk as a standard image file.
This function converts the image from GPU memory to a Pillow image and saves it using the given filename. The format is inferred from the file extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The image to save. | required |
filename | str | Destination path, including extension (e.g., 'output.png'). | required |
Returns:
Type | Description |
---|---|
None | None |
Example
save_image(cuda_img, "output.png")
Source code in photoff\io\__init__.py
photoff.operations.blend
blend(background, over, x, y)
Blends an image (over
) on top of another (background
) at a specified position.
The blending respects the alpha channel of the overlaid image. No clipping is performed if the over
image exceeds the bounds of the background
; behavior in such cases depends on the underlying CUDA implementation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
background | CudaImage | The base image to draw onto. | required |
over | CudaImage | The image to blend on top. | required |
x | int | Horizontal position in the background where the top-left corner of | required |
y | int | Vertical position in the background where the top-left corner of | required |
Returns:
Type | Description |
---|---|
None | None |
Example
blend(bg_img, icon_img, x=100, y=50)
Source code in photoff\operations\blend.py
photoff.operations.fill
fill_color(image, color)
Fills the entire image with a solid color.
This operation overwrites all pixels in the image with the specified RGBA color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The image to fill. | required |
color | RGBA | The fill color to apply. | required |
Returns:
Type | Description |
---|---|
None | None |
Example
fill_color(img, RGBA(255, 255, 255, 255)) # Fill with solid white
Source code in photoff\operations\fill.py
fill_gradient(image, color1, color2, direction=0, seamless=False)
Fills the image with a linear gradient between two colors.
The gradient direction and style (seamless or linear) can be customized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The target image to fill. | required |
color1 | RGBA | The starting color of the gradient. | required |
color2 | RGBA | The ending color of the gradient. | required |
direction | int | Gradient direction: - 0 = vertical (top to bottom) - 1 = horizontal (left to right) - 2 = diagonal (top-left to bottom-right) - 3 = diagonal (bottom-left to top-right) Defaults to 0. | 0 |
seamless | bool | Whether the gradient should repeat seamlessly. Defaults to False. | False |
Returns:
Type | Description |
---|---|
None | None |
Raises:
Type | Description |
---|---|
ValueError | If an invalid direction value is provided. |
Example
fill_gradient(img, RGBA(0, 0, 0, 255), RGBA(255, 255, 255, 255), direction=1)
Source code in photoff\operations\fill.py
photoff.operations.filters
apply_chroma_key(image, key_image, channel='A', threshold=128, invert=False, zero_all_channels=False)
Applies a chroma key mask based on a channel of another image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The target image to apply transparency. | required |
key_image | CudaImage | Image whose channel values are used as a mask. | required |
channel | str | Channel to use from the key image ('R', 'G', 'B', 'A'). Defaults to 'A'. | 'A' |
threshold | int | Threshold (0–255) to apply masking. Defaults to 128. | 128 |
invert | bool | Invert the mask logic. Defaults to False. | False |
zero_all_channels | bool | If True, sets RGB to zero where mask applies. Defaults to False. | False |
Raises:
Type | Description |
---|---|
ValueError | If the provided channel is invalid. |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_corner_radius(image, size)
Applies a rounded corner mask to an image in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to be modified. | required |
size | int | Radius of the corner in pixels. | required |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_flip(image, flip_horizontal=False, flip_vertical=False)
Flips an image horizontally or vertically in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to flip. | required |
flip_horizontal | bool | Flip the image horizontally. Defaults to False. | False |
flip_vertical | bool | Flip the image vertically. Defaults to False. | False |
Raises:
Type | Description |
---|---|
ValueError | If both |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_gaussian_blur(image, radius, image_copy_cache=None)
Applies a Gaussian blur effect to an image in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to blur. | required |
radius | float | Radius of the blur in pixels. | required |
image_copy_cache | CudaImage | Optional buffer. Must match image size. | None |
Raises:
Type | Description |
---|---|
ValueError | If the cache does not match the image dimensions. |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_grayscale(image)
Converts an image to grayscale in-place using luminosity method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to convert. | required |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_opacity(image, opacity)
Modifies the alpha channel of an image to apply global opacity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to modify. | required |
opacity | float | Opacity value between 0.0 (transparent) and 1.0 (opaque). | required |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_shadow(image, radius, intensity, shadow_color, image_copy_cache=None, inner=False)
Applies a shadow effect around the opaque regions of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to apply the shadow to. | required |
radius | float | Blur radius of the shadow. | required |
intensity | float | Intensity multiplier of the shadow. | required |
shadow_color | RGBA | Color of the shadow. | required |
image_copy_cache | CudaImage | Optional image copy buffer. Must match original image size. | None |
inner | bool | Whether to draw the shadow inside the shape. Defaults to False. | False |
Raises:
Type | Description |
---|---|
ValueError | If the cache does not match the image dimensions. |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
apply_stroke(image, stroke_width, stroke_color, image_copy_cache=None, inner=True)
Draws a stroke (outline) around the non-transparent areas of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | Image to which the stroke will be applied. | required |
stroke_width | int | Width of the stroke in pixels. | required |
stroke_color | RGBA | Color of the stroke. | required |
image_copy_cache | CudaImage | Optional cache of the original image. Must match dimensions. | None |
inner | bool | If True, stroke is drawn inside the shape; otherwise outside. Defaults to True. | True |
Raises:
Type | Description |
---|---|
ValueError | If the provided cache does not match image dimensions. |
Returns:
Type | Description |
---|---|
None | None |
Source code in photoff\operations\filters.py
photoff.operations.resize
ResizeMethod
Bases: Enum
Enum representing supported image resizing algorithms.
Attributes:
Name | Type | Description |
---|---|---|
BILINEAR | Bilinear interpolation (smooth, reasonably fast). | |
NEAREST | Nearest neighbor interpolation (fastest, lowest quality). | |
BICUBIC | Bicubic interpolation (higher quality, slower). |
Usage
method = ResizeMethod.BICUBIC
Source code in photoff\operations\resize.py
crop_margins(image, left=0, top=0, right=0, bottom=0, crop_image_cache=None)
Crops margins from the edges of a CudaImage.
Margins are specified in pixels. The resulting image will be smaller and positioned relative to the top-left corner of the cropped area.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The input image to crop. | required |
left | int | Pixels to remove from the left edge. Defaults to 0. | 0 |
top | int | Pixels to remove from the top edge. Defaults to 0. | 0 |
right | int | Pixels to remove from the right edge. Defaults to 0. | 0 |
bottom | int | Pixels to remove from the bottom edge. Defaults to 0. | 0 |
crop_image_cache | CudaImage | Pre-allocated buffer for the cropped image. Must match the expected result dimensions. | None |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | The cropped image. |
Raises:
Type | Description |
---|---|
ValueError | If any margin is negative. |
ValueError | If margins exceed the image's size. |
ValueError | If the cache image does not match the result size. |
Example
cropped = crop_margins(img, left=10, top=10, right=10, bottom=10)
Source code in photoff\operations\resize.py
resize(image, width, height, method=ResizeMethod.BICUBIC, resize_image_cache=None)
Resizes a CudaImage to the specified dimensions using the chosen interpolation method.
Supports bilinear, nearest-neighbor, and bicubic resampling. A cache image can be reused for performance to avoid memory allocation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The input image to resize. | required |
width | int | Target width. | required |
height | int | Target height. | required |
method | ResizeMethod | Resampling method. Defaults to BICUBIC. | BICUBIC |
resize_image_cache | CudaImage | Pre-allocated image for the resized result. Must match target dimensions if provided. | None |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | A new (or reused) image resized to the given dimensions. |
Raises:
Type | Description |
---|---|
ValueError | If the cache image dimensions do not match the target size. |
ValueError | If the interpolation method is not supported. |
Example
resized = resize(img, 256, 256, method=ResizeMethod.BILINEAR)
Source code in photoff\operations\resize.py
photoff.operations.text
render_text(text, font_path, font_size=24, color=RGBA(0, 0, 0, 255))
Renders a string of text into a CudaImage using a specified TrueType font.
The function uses Pillow (PIL) to rasterize the text and transfers the resulting RGBA image into GPU memory as a CudaImage
. Font metrics are computed to fit the rendered text exactly, avoiding unnecessary padding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text | str | The string to render. | required |
font_path | str | Path to a TrueType (.ttf) or OpenType (.otf) font file. | required |
font_size | int | Font size in points. Defaults to 24. | 24 |
color | RGBA | Text color. Defaults to opaque black (0, 0, 0, 255). | RGBA(0, 0, 0, 255) |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | GPU image containing the rendered text. |
Raises:
Type | Description |
---|---|
ValueError | If the font file cannot be loaded. |
Example
img = render_text("Hello GPU!", "/fonts/Roboto-Regular.ttf", 32, RGBA(255, 255, 255, 255))
Source code in photoff\operations\text.py
photoff.operations.utils
blend_aligned(background, image, align='center', offset_x=0, offset_y=0)
Blends an image onto a background at a specified alignment and optional offset.
The image is positioned relative to the background based on the align
parameter, which supports standard alignment strings (e.g., "center", "top-left", etc.). Offsets can be applied to adjust the final position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
background | CudaImage | The background image where the input image will be blended. | required |
image | CudaImage | The image to blend. | required |
align | str | Alignment method. Supported values: - "center" / "middle" - "top", "bottom", "left", "right" - "top-left", "top-right", "bottom-left", "bottom-right" Defaults to "center". | 'center' |
offset_x | int | Horizontal pixel offset to apply after alignment. Defaults to 0. | 0 |
offset_y | int | Vertical pixel offset to apply after alignment. Defaults to 0. | 0 |
Returns:
Type | Description |
---|---|
None | None |
Example
blend_aligned(bg, img, align="bottom-right", offset_x=-10, offset_y=-10)
Source code in photoff\operations\utils.py
cover_image_in_container(image, container_width, container_height, offset_x=0, offset_y=0, background_color=RGBA(0, 0, 0, 0), container_image_cache=None, resize_image_cache=None, resize_mode=ResizeMethod.BICUBIC)
Resizes an image to fully cover a container while maintaining aspect ratio, and blends it centered (or offset) within the container.
The image is scaled up proportionally so that it completely fills the container dimensions (container_width
x container_height
). The resized image may overflow on one axis, similar to CSS's background-size: cover
. Optional caching and resizing method are supported for performance and quality tuning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The input image to be resized and placed. | required |
container_width | int | Width of the container image. | required |
container_height | int | Height of the container image. | required |
offset_x | int | Horizontal offset to apply after centering. Defaults to 0. | 0 |
offset_y | int | Vertical offset to apply after centering. Defaults to 0. | 0 |
background_color | RGBA | Background color to fill the container. Defaults to transparent black. | RGBA(0, 0, 0, 0) |
container_image_cache | CudaImage | Pre-allocated container image buffer. Must match the container dimensions. | None |
resize_image_cache | CudaImage | Pre-allocated image for resized content. Must match computed resize dimensions. | None |
resize_mode | ResizeMethod | Resize algorithm to use (e.g., BICUBIC, NEAREST). Defaults to BICUBIC. | BICUBIC |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | A new image with the resized input blended over the background. |
Raises:
Type | Description |
---|---|
ValueError | If the number of images exceeds the grid capacity. |
ValueError | If provided |
Source code in photoff\operations\utils.py
create_image_collage(images, grid_width, grid_height, spacing=0, background_color=RGBA(0, 0, 0, 0), collage_image_cache=None)
Creates a collage from a list of CUDA images arranged in a grid.
All images must have identical dimensions. The function supports optional spacing between images and a custom background color. It also allows reuse of a pre-allocated image buffer to avoid reallocations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images | list[CudaImage] | List of images to include in the collage. | required |
grid_width | int | Number of columns in the collage grid. | required |
grid_height | int | Number of rows in the collage grid. | required |
spacing | int | Space in pixels between images in the grid. Defaults to 0. | 0 |
background_color | RGBA | Background color to fill the collage. Defaults to transparent black (0, 0, 0, 0). | RGBA(0, 0, 0, 0) |
collage_image_cache | CudaImage | Optional pre-allocated image to use as the collage buffer. Must match the final dimensions. If not provided, a new image will be created. | None |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | A new image containing the collage. |
Raises:
Type | Description |
---|---|
ValueError | If the number of images exceeds the grid capacity. |
ValueError | If input images do not have the same dimensions. |
ValueError | If the provided collage_image_cache does not match the required dimensions. |
Source code in photoff\operations\utils.py
create_image_grid(image, grid_width, grid_height, num_images, spacing=0, background_color=RGBA(0, 0, 0, 0), grid_image_cache=None)
Creates a grid layout by duplicating a single image multiple times.
The image is repeated num_images
times and arranged in a grid with the specified width and height. Optional spacing and background color can be configured. A cached output image can be reused for efficiency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The image to be repeated across the grid. | required |
grid_width | int | Number of columns in the grid. | required |
grid_height | int | Number of rows in the grid. | required |
num_images | int | Total number of image copies to include in the grid. Must not exceed grid capacity. | required |
spacing | int | Number of pixels between images in both directions. Defaults to 0. | 0 |
background_color | RGBA | Background color to fill empty areas. Defaults to transparent black. | RGBA(0, 0, 0, 0) |
grid_image_cache | CudaImage | Pre-allocated image buffer to use. Must match final grid size if provided. | None |
Returns:
Name | Type | Description |
---|---|---|
CudaImage | CudaImage | A new image containing the grid of repeated images. |
Raises:
Type | Description |
---|---|
ValueError | If |
ValueError | If |
Source code in photoff\operations\utils.py
get_cover_resize_dimensions(image, container_width, container_height)
Calculates the new dimensions to resize an image so that it fully covers a container while preserving its aspect ratio.
The resulting dimensions ensure that the image completely fills the target container (container_width
x container_height
), potentially exceeding one of the container's dimensions. This mimics the behavior of background-size: cover
in CSS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The original image to be resized. | required |
container_width | int | Target container width. | required |
container_height | int | Target container height. | required |
Returns:
Type | Description |
---|---|
tuple[int, int] | tuple[int, int]: The new width and height that the image should be resized to. |
Example
get_cover_resize_dimensions(img, 1920, 1080) (1920, 1280)
Source code in photoff\operations\utils.py
get_no_padding_size(image, padding)
Calculates the size of an image after removing equal padding from all sides.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The padded image. | required |
padding | int | Padding in pixels to remove from each side. | required |
Returns:
Type | Description |
---|---|
tuple[int, int] | tuple[int, int]: The resulting width and height without padding. |
Example
get_no_padding_size(img, 10) (image.width - 20, image.height - 20)
Source code in photoff\operations\utils.py
get_padding_size(image, padding)
Calculates the size of an image after adding equal padding on all sides.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image | CudaImage | The original image. | required |
padding | int | Padding in pixels to add on each side. | required |
Returns:
Type | Description |
---|---|
tuple[int, int] | tuple[int, int]: The new width and height including padding. |
Example
get_padding_size(img, 10) (image.width + 20, image.height + 20)
Source code in photoff\operations\utils.py
photoff.core.buffer
copy_buffers_same_size(dst, src, width, height)
Copies data between two CUDA buffers of the same size.
This is useful for in-GPU memory operations like duplicating an image or preparing a temporary working buffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dst | CudaBuffer | Destination buffer on the device. | required |
src | CudaBuffer | Source buffer on the device. | required |
width | int | Width of the image. | required |
height | int | Height of the image. | required |
Returns:
Type | Description |
---|---|
None | None |
Example
copy_buffers_same_size(tmp_buf, original_buf, 512, 512)
Source code in photoff\core\buffer.py
copy_to_device(d_dst, h_src, width, height)
Copies image data from host (CPU) to device (GPU) memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d_dst | CudaBuffer | Destination buffer in device memory. | required |
h_src | CudaBuffer | Source buffer in host memory. | required |
width | int | Width of the image. | required |
height | int | Height of the image. | required |
Returns:
Type | Description |
---|---|
None | None |
Example
copy_to_device(gpu_buf, cpu_buf, 256, 256)
Source code in photoff\core\buffer.py
copy_to_host(h_dst, d_src, width, height)
Copies image data from device (GPU) to host (CPU) memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h_dst | CudaBuffer | Destination buffer in host memory. | required |
d_src | CudaBuffer | Source buffer in device memory. | required |
width | int | Width of the image. | required |
height | int | Height of the image. | required |
Returns:
Type | Description |
---|---|
None | None |
Example
copy_to_host(cpu_buf, gpu_buf, 256, 256)
Source code in photoff\core\buffer.py
create_buffer(width, height)
Allocates a new CUDA buffer for an image of given dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
width | int | Width of the buffer in pixels. | required |
height | int | Height of the buffer in pixels. | required |
Returns:
Name | Type | Description |
---|---|---|
CudaBuffer | CudaBuffer | A pointer to the allocated device memory buffer. |
Example
buffer = create_buffer(512, 512)
Source code in photoff\core\buffer.py
free_buffer(buffer)
Frees a CUDA buffer previously allocated on the device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
buffer | CudaBuffer | The buffer to free. | required |
Returns:
Type | Description |
---|---|
None | None |
Example
free_buffer(buffer)