The Kelly AI SDK handles images using base64 encoding for both input and output. The SDK automatically manages encoding and decoding, but understanding how it works will help you integrate images into your applications.
import asyncioimport base64from kellyapi import KellyAPIasync def upscale_image(): api = KellyAPI(api_key="your_api_key_here") # Read and encode input image with open("small_image.png", "rb") as f: image_bytes = f.read() image_base64 = base64.b64encode(image_bytes).decode('utf-8') # Upscale the image upscaled_data = await api.upscale(image_data=image_base64) # Save the result with open("upscaled_image.png", "wb") as f: f.write(upscaled_data) print("Image upscaled successfully!")asyncio.run(upscale_image())
You can work with images in memory using BytesIO instead of saving to disk:
import asyncioimport base64from io import BytesIOfrom kellyapi import KellyAPIfrom PIL import Imageasync def generate_and_process(): api = KellyAPI(api_key="your_api_key_here") # Generate image image_data = await api.generate( prompt="A colorful abstract painting", width=1024, height=1024 ) # Load into BytesIO image_buffer = BytesIO(image_data) # Open with PIL for processing img = Image.open(image_buffer) # Process the image img = img.resize((512, 512)) img = img.convert('RGB') # Save to another BytesIO output_buffer = BytesIO() img.save(output_buffer, format='JPEG', quality=90) output_buffer.seek(0) # Save to file with open("processed.jpg", "wb") as f: f.write(output_buffer.getvalue()) print("Image processed and saved!")asyncio.run(generate_and_process())
The SDK imports BytesIO from the io module but primarily uses it for type hints. When working with image data, you’ll typically receive and work with bytes objects.
The img2img() method lets you edit existing images based on a text prompt:
import asyncioimport base64from kellyapi import KellyAPIasync def edit_image(): api = KellyAPI(api_key="your_api_key_here") # Read and encode source image with open("original.png", "rb") as f: original_bytes = f.read() image_base64 = base64.b64encode(original_bytes).decode('utf-8') # Edit the image edited_data = await api.img2img( prompt="Add a rainbow in the sky", image_data=image_base64, width=1024, height=1024 ) # Save the edited image with open("edited.png", "wb") as f: f.write(edited_data) print("Image edited successfully!")asyncio.run(edit_image())
Create a reusable helper function to encode images:
import base64from pathlib import Pathdef encode_image(image_path: str) -> str: """ Read an image file and return base64-encoded string. Args: image_path: Path to the image file Returns: Base64-encoded string of the image """ with open(image_path, "rb") as f: image_bytes = f.read() return base64.b64encode(image_bytes).decode('utf-8')def save_image(image_data: bytes, output_path: str): """ Save raw image bytes to a file. Args: image_data: Raw image bytes output_path: Path where to save the image """ with open(output_path, "wb") as f: f.write(image_data) print(f"Image saved to {output_path}")# Usageasync def main(): api = KellyAPI(api_key="your_api_key_here") # Encode and upscale encoded = encode_image("input.png") upscaled = await api.upscale(image_data=encoded) save_image(upscaled, "output.png")