Skip to main content
The Direct class can process image URLs and download images for fast, simple operations without tool overhead.

Extracting Images from URLs

from upsonic import Direct, Task
from upsonic.utils.image import extract_and_save_images_from_response

# Create Direct instance
direct = Direct(model="openai/gpt-4o")

# Get response with image URLs
task = Task(
    description=(
        "Provide a markdown formatted image. "
        "Format as: ![Image](https://example.com/image.jpg)"
    )
)
result = direct.do(task)

# Extract and save images from response (folder created automatically)
saved_images = extract_and_save_images_from_response(
    response_text=result,
    folder_path="downloaded_images",
    base_filename="downloaded"
)

print(f"Saved {len(saved_images)} images")

Saving Base64 Images

from upsonic.utils.image import save_image_to_folder

# Save base64 encoded image
base64_string = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB..."

saved_path = save_image_to_folder(
    image_data=base64_string,
    folder_path="my_images",
    filename="decoded.png",
    is_base64=True
)

Saving Raw Image Bytes

from upsonic.utils.image import save_image_to_folder

# Save raw image bytes
image_bytes = b'\x89PNG\r\n...'

saved_path = save_image_to_folder(
    image_data=image_bytes,
    folder_path="my_images",
    filename="raw_image.png",
    is_base64=False
)

Batch Processing URLs

from upsonic.utils.image import extract_image_urls, urls_to_base64, save_image_to_folder

# Extract URLs from text
text = "Check out ![Image1](https://example.com/1.jpg) and ![Image2](https://example.com/2.jpg)"
urls = extract_image_urls(text)

# Download all as base64
base64_images = urls_to_base64(urls)

# Save all images
for i, b64_img in enumerate(base64_images, 1):
    save_image_to_folder(
        image_data=b64_img,
        folder_path="downloaded",
        filename=f"image_{i}.png",
        is_base64=True
    )

Managing Image Folders

from upsonic.utils.image import list_images_in_folder, open_images_from_folder

# List all images (newest first)
images = list_images_in_folder("my_images")

# Open all images (limit to 5)
opened = open_images_from_folder("my_images", limit=5)

print(f"Opened {len(opened)} images")