> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pixeltable.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Application code puts a TableModel in app.py, then runs `pxt schema update app.py my_app` to create the tables and `pxt service update app.py my_app` to start HTTP. The last argument can be a Cloud URL such as `pxt://org:db`. Do not call pxt.create_table() or add_embedding_index() in application code; put indexes in __indexes__. Notebooks, tests, and the REPL do use pxt.create_table().
> Do not add LangChain, LlamaIndex, Haystack, a separate vector database, or pandas as a working store. Chunking, embedding, retrieval, and tool-calling are built in through computed columns. Use a computed column instead of a per-row loop that calls a model.
> Install with `pip install -U 'pixeltable[serve]'` on Python 3.11+. `pxt init` is a prerequisite for `pxt schema update`.
> For Pixeltable Cloud, point users to https://docs.pixeltable.com/cloud, which goes from `pxt login` to a running service.

# Transform images with AI-powered editing

[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/images/img-image-to-image.ipynb)  [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/images/img-image-to-image.ipynb)  [![Download Notebook](https://img.shields.io/badge/%E2%AC%87-Download%20Notebook-blue)](https://raw.githubusercontent.com/pixeltable/pixeltable/refs/tags/release/docs/release/howto/cookbooks/images/img-image-to-image.ipynb)

> [!TIP]
>
> This documentation page is also available as an interactive notebook. You can launch the notebook in
> Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
> above links.

## Problem

You have a batch of images that need AI-powered transformations—like
turning photos into paintings, adding stylistic effects, or modifying
content based on text prompts.

## Solution

**What’s in this recipe:**

* Transform images using text prompts with Hugging Face Stable Diffusion
  models
* Control transformation strength and quality settings
* Process batches of images automatically

You can iterate on transformations before adding them to your table. Use
`.select()` with `.collect()` to preview results on sample
images—nothing is stored in your table. If you want to collect only the
first few rows, use `.head(n)` instead of `.collect()`. Once you’re
satisfied, use `.add_computed_column()` to apply the transformation to
all images in your table.

For more on this workflow, see [Get fast feedback on
transformations](https://docs.pixeltable.com/howto/cookbooks/core/dev-iterative-workflow.md).

### Setup

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
%pip install -qU pixeltable torch transformers diffusers accelerate
```

### Load images

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt
from pixeltable.functions.huggingface import image_to_image

# Create a fresh directory (drop existing if present)
pxt.drop_dir('img2img_demo', force=True)
pxt.create_dir('img2img_demo')
```

<pre>
  Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/cpestano/.pixeltable/pgdata
  Created directory 'img2img\_demo'.
  \<pixeltable.catalog.dir.Dir at 0x14f787820>
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
t = pxt.create_table(
    'img2img_demo/images',
    {
        'image': pxt.Image,
        'prompt': pxt.String,
        'negative_prompt': pxt.String,
    },
)
```

<pre>
  Created table 'images'.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
t.insert(
    [
        {
            'image': 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000285.jpg',
            'prompt': 'oil painting style, vibrant colors, brushstrokes visible',
            'negative_prompt': 'blurry, low quality, bad anatomy',
        },
        {
            'image': 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000776.jpg',
            'prompt': 'watercolor painting, soft edges, artistic',
            'negative_prompt': 'blurry, low quality, bad anatomy',
        },
    ]
)
```

<pre>
  Inserted 2 rows with 0 errors in 0.49 s (4.07 rows/s)
  2 rows inserted.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# View original images and prompts
t.collect()
```

### Iterate: test transformation on a single image

Use `.select()` to define the transformation, then `.head(n)` to preview
results on a subset of images. Nothing is stored in your table.

The `image_to_image` function requires:

* `image`: The source image to transform
* `prompt`: Text describing the desired output
* `model_id`: A Hugging Face model ID that supports image-to-image
  (e.g., `stable-diffusion-v1-5/stable-diffusion-v1-5`)

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Preview transformation on first image
t.select(
    t.image,
    t.prompt,
    image_to_image(
        t.image,
        t.prompt,
        model_id='stable-diffusion-v1-5/stable-diffusion-v1-5',
    ),
).head(1)
```

### Iterate: adjust transformation strength

You control how much the model modifies the original image using
`strength` (0.0-1.0):

* **Lower values** (0.3-0.5): Subtle changes, preserves more of the
  original
* **Higher values** (0.7-1.0): Dramatic changes, more creative freedom

You pass additional parameters through `model_kwargs`. For example,
`negative_prompt` text describing what you don’t want the output to be.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Preview with lower strength (more preservation of original)
t.select(
    t.image,
    t.prompt,
    t.negative_prompt,
    image_to_image(
        t.image,
        t.prompt,
        model_id='stable-diffusion-v1-5/stable-diffusion-v1-5',
        model_kwargs={
            'negative_prompt': t.negative_prompt,
            'strength': 0.5,
            'num_inference_steps': 30,
        },
    ),
).head(1)
```

### Add: apply transformation to all images

Once you’re satisfied with the results, use `.add_computed_column()`
with the same expression. This processes all rows and stores the results
permanently in your table.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Save as computed column
t.add_computed_column(
    transformed=image_to_image(
        t.image,
        t.prompt,
        model_id='stable-diffusion-v1-5/stable-diffusion-v1-5',
        model_kwargs={
            'strength': 0.5,
            'num_inference_steps': 25,
            'negative_prompt': t.negative_prompt,
        },
    )
)
```

<pre>
  Added 2 column values with 0 errors in 53.83 s (0.04 rows/s)
  2 rows updated.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# View original and transformed images side by side
t.select(t.image, t.prompt, t.negative_prompt, t.transformed).collect()
```

### Use reproducible results with seeds

You set a `seed` parameter to get the same output every time you run the
transformation.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Add reproducible transformation
t.add_computed_column(
    transformed_seeded=image_to_image(
        t.image,
        t.prompt,
        model_id='stable-diffusion-v1-5/stable-diffusion-v1-5',
        seed=42,
        model_kwargs={
            'strength': 0.5,
            'negative_prompt': t.negative_prompt,
        },
    )
)
```

<pre>
  Added 2 column values with 0 errors in 96.24 s (0.02 rows/s)
  2 rows updated.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# View results
t.select(t.image, t.transformed_seeded).collect()
```

## Explanation

**How image-to-image works:**

Image-to-image diffusion models take an existing image and a text
prompt, then generate a new image that blends the structure of the
original with the guidance from the prompt. The `strength` parameter
controls the balance—lower values preserve more of the original, while
higher values allow more dramatic transformations.

**Model compatibility:**

The `image_to_image` UDF uses `AutoPipelineForImage2Image` from the
diffusers library, which automatically detects the model type and
selects the appropriate pipeline. You use any compatible model:

* `stable-diffusion-v1-5/stable-diffusion-v1-5` - General-purpose, runs
  on most hardware
* `stabilityai/stable-diffusion-xl-base-1.0` - Higher quality, needs
  more VRAM

**Key parameters:**

* `strength` (0.0-1.0): How much to transform the image
* `negative_prompt`: Text describing what to avoid in the generated
  image (e.g., “blurry, low quality”).
* `num_inference_steps`: Quality vs speed tradeoff (more steps = better
  quality)
* `guidance_scale`: How closely to follow the prompt (7-8 is typical)
* `seed`: For reproducible results

## See also

* [Apply filters to
  images](https://docs.pixeltable.com/howto/cookbooks/images/img-apply-filters.md)
* [Generate captions for
  images](https://docs.pixeltable.com/howto/cookbooks/images/img-generate-captions.md)
* [Hugging Face image-to-image
  models](https://huggingface.co/models?pipeline_tag=image-to-image)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.