MixtrainDocsBlog
from mixtrain import Image

Overview

Use Image for image inputs and outputs. Mixtrain displays returned images inline in the app, and you can download them locally for processing.

Constructor

Image(
    url: str,
    *,
    content_type: str | None = None,
    filename: str | None = None,
    width: int | None = None,
    height: int | None = None,
    format: str | None = None,
)
ParameterTypeDescription
urlstrWorkspace path or URL (https://, gs://, s3://)
content_typestr | NoneMIME type
filenamestr | NoneOriginal filename
widthint | NoneWidth in pixels
heightint | NoneHeight in pixels
formatstr | NoneFormat hint (png, jpg, webp)
image = Image("gallery/photo.jpg", width=1024, height=768, format="jpg")

Properties

PropertyTypeDescription
urlstrWorkspace path or remote URL
content_typestr | NoneMIME type
filenamestr | NoneOriginal filename
widthint | NoneWidth in pixels
heightint | NoneHeight in pixels
formatstr | NoneFormat (png, jpg, webp)

Methods

to_file()

Inherited from File. Downloads the image and returns the local path. Cached across calls.

path = image.to_file()

to_pil()

Open the image as a PIL Image.

image.to_pil() -> PIL.Image.Image

Downloads the image if needed and opens it with PIL.

from mixtrain import Image

image = Image("https://example.com/photo.jpg")
pil_img = image.to_pil()
print(pil_img.size)  # (1024, 768)

to_numpy()

Convert the image to a numpy array.

image.to_numpy() -> np.ndarray

Returns the image as a numpy array in (H, W, C) order.

import numpy as np

arr = image.to_numpy()
print(arr.shape)  # (768, 1024, 3)

Using Image

You can use Image in your models, datasets, workflows, and routines.

As output

from mixtrain import MixModel, Image

class ImageGenerator(MixModel):
    def run(self, inputs=None):
        image_url = self._generate(inputs["prompt"])
        return {
            "result": Image(image_url, width=1024, height=1024)
        }

As input

from mixtrain import MixModel, Image

class ImageAnalyzer(MixModel):
    def run(self, image: Image):
        # Download and process
        arr = image.to_numpy()
        # ... analyze image
        return {"label": "cat"}

From model result

from mixtrain import Model

model = Model("flux-pro")
result = model.run({"prompt": "A sunset"})

print(result.image.url)
print(f"{result.image.width}x{result.image.height}")

Storing Images

Use Image.from_file() for a local image, then call save() to persist it to workspace storage:

image = Image.from_file("photo.jpg", width=1024, height=768)
saved_image = image.save("gallery/photo.jpg")

On this page