Skip to main content
Modify and enhance existing images using AI. The image-to-image feature allows you to transform photos based on text prompts while preserving the original composition and structure.

Edit an image

Transform existing images using the img2img() method.
1

Prepare your image

Convert your image to base64 format:
import base64
from kellyapi import KellyAPI

# Read and encode the image
with open("input.png", "rb") as f:
    image_bytes = f.read()
image_base64 = base64.b64encode(image_bytes).decode("utf-8")

kelly = KellyAPI(api_key="your_api_key")
2

Apply transformation

edited_image = await kelly.img2img(
    prompt="Turn this into a watercolor painting",
    image_data=image_base64,
    width=1024,
    height=1024
)
3

Save the result

with open("edited.png", "wb") as f:
    f.write(edited_image)

Parameters

prompt
str
required
Description of how you want to transform the image. This guides the AI’s editing process.
image_data
str
required
The source image encoded as a base64 string.
negative_prompt
str
Describes what you don’t want in the edited image. The default value excludes common unwanted elements.
width
str
default:"1024"
Output image width in pixels.
height
str
default:"1024"
Output image height in pixels.

Complete example

import asyncio
import base64
from kellyapi import KellyAPI

async def edit_image():
    kelly = KellyAPI(api_key="your_api_key")
    
    # Load and encode the source image
    with open("photo.jpg", "rb") as f:
        image_bytes = f.read()
    image_base64 = base64.b64encode(image_bytes).decode("utf-8")
    
    # Transform the image
    result = await kelly.img2img(
        prompt="Convert to anime art style with vibrant colors",
        image_data=image_base64,
        negative_prompt="blurry, low quality, distorted, realistic",
        width=1024,
        height=1024
    )
    
    # Save the edited image
    with open("anime_style.png", "wb") as f:
        f.write(result)
    
    print("Image edited successfully!")

# Run the async function
asyncio.run(edit_image())

Use cases

Style transfer

Transform photos into different artistic styles:
result = await kelly.img2img(
    prompt="Oil painting style with impressionist brushstrokes",
    image_data=image_base64,
    width=1024,
    height=768
)

Photo enhancement

Enhance and improve existing photos:
result = await kelly.img2img(
    prompt="Professional studio portrait, perfect lighting, high detail, sharp focus",
    image_data=image_base64,
    width=512,
    height=768
)

Scene modification

Change elements within an image:
result = await kelly.img2img(
    prompt="Same scene but at sunset with golden hour lighting",
    image_data=image_base64,
    width=1024,
    height=1024
)

Creative variations

Generate alternative versions of an image:
result = await kelly.img2img(
    prompt="Fantasy version with magical elements and glowing effects",
    image_data=image_base64,
    width=1024,
    height=1024
)

Image description

You can also extract text descriptions from images using the img2text() method:
description = await kelly.img2text(
    prompt="Describe this image in detail",
    image_data=image_base64
)

print(description)

Parameters

prompt
str
required
Instructions for the description. For example, “Describe this image” or “What objects are in this photo?”
image_data
str
required
The image encoded as a base64 string.
The img2img() method preserves the general composition and structure of your input image while applying the transformation described in your prompt.
For more accurate edits, use specific prompts that describe the desired style, mood, or changes. Include quality descriptors like “high detail” or “professional quality” for better results.

Build docs developers (and LLMs) love