> ## 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.

# Adjust image brightness and contrast

[![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-brightness-contrast.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-brightness-contrast.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-brightness-contrast.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 need to fix inconsistent lighting across hundreds of
images—adjusting brightness, contrast, and color saturation.

## Solution

**What’s in this recipe:**

* Adjust brightness, contrast, and saturation
* Test adjustments before applying
* Process multiple images in batch

You adjust brightness, contrast, and saturation for images in your table
using custom UDFs that wrap Pillow’s `ImageEnhance` module (relies on
PIL/Pillow). This lets you control enhancement levels to match your
needs.

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 adjustments 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
```

### Load images

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt
from PIL import ImageEnhance

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

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

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
t = pxt.create_table('image_demo/enhancements', {'image': pxt.Image})
```

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

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
t.insert(
    [
        {
            'image': 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000016.jpg'
        },
        {
            'image': 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000049.jpg'
        },
        {
            'image': 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000036.jpg'
        },
    ]
)
```

<pre>
  Inserting rows into \`enhancements\`: 0 rows \[00:00, ? rows/s]
  Inserting rows into \`enhancements\`: 3 rows \[00:00, 601.16 rows/s]
  Inserted 3 rows with 0 errors.
  3 rows inserted, 6 values computed.
</pre>

### Iterate: adjust brightness and contrast for a few images first

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
@pxt.udf
def adjust_brightness(img: pxt.Image, factor: float) -> pxt.Image:
    """Adjust brightness. factor < 1 = darker, > 1 = brighter."""
    return ImageEnhance.Brightness(img).enhance(factor)


@pxt.udf
def adjust_contrast(img: pxt.Image, factor: float) -> pxt.Image:
    """Adjust contrast. factor < 1 = lower, > 1 = higher."""
    return ImageEnhance.Contrast(img).enhance(factor)


@pxt.udf
def adjust_saturation(img: pxt.Image, factor: float) -> pxt.Image:
    """Adjust saturation. factor < 1 = less saturated, > 1 = more saturated."""
    return ImageEnhance.Color(img).enhance(factor)
```

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Test brightness adjustments
t.select(
    t.image,
    adjust_brightness(t.image, 0.5),
    adjust_brightness(t.image, 1.5),
).head(1)
```

### Add: adjust brightness and contrast for all images in your table

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Brightness adjustments (1.0 = original)
t.add_computed_column(darker=adjust_brightness(t.image, 0.5))
t.add_computed_column(brighter=adjust_brightness(t.image, 1.5))

# Contrast adjustments
t.add_computed_column(low_contrast=adjust_contrast(t.image, 0.5))
t.add_computed_column(high_contrast=adjust_contrast(t.image, 2.0))

# Color saturation
t.add_computed_column(desaturated=adjust_saturation(t.image, 0.3))
t.add_computed_column(saturated=adjust_saturation(t.image, 2.0))
```

<pre>
  Added 3 column values with 0 errors.
  Added 3 column values with 0 errors.
  Added 3 column values with 0 errors.
  Added 3 column values with 0 errors.
  Added 3 column values with 0 errors.
  Added 3 column values with 0 errors.
  3 rows updated, 3 values computed.
</pre>

### View results

Compare different enhancement levels side-by-side.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Compare brightness levels
t.select(t.image, t.darker, t.brighter).collect()
```

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Compare contrast levels
t.select(t.image, t.low_contrast, t.high_contrast).collect()
```

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Compare saturation levels
t.select(t.image, t.desaturated, t.saturated).collect()
```

## Explanation

**How the enhancement technique works:**

The UDFs wrap PIL’s `ImageEnhance` module to adjust visual properties of
images. Each enhancement type creates an enhancer object for the image,
then applies a multiplication factor. A factor of 1.0 leaves the image
unchanged, values below 1.0 decrease the property (darker, less
contrast, desaturated), and values above 1.0 increase it (brighter, more
contrast, saturated). You can apply different factors to the same image
to create multiple variations for comparison or different use cases.

**To customize the UDFs:**

* **Brightness factors**: Use 0.5 for darker images, 1.5 for brighter,
  or adjust to match your lighting needs
* **Contrast factors**: Use 0.5 for lower contrast, 2.0 for higher
  contrast, or fine-tune for image clarity
* **Saturation factors**: Use 0.3 for desaturated/muted colors, 2.0 for
  vibrant colors, or 0.0 for complete grayscale
* **Combine adjustments**: Apply multiple enhancements to create complex
  transformations

**The Pixeltable workflow:**

In traditional databases, `.select()` just picks which columns to view.
In Pixeltable, `.select()` also lets you compute new transformations on
the fly—define new columns without storing them. This makes `.select()`
perfect for testing transformations before you commit them.

When you use `.select()`, you’re creating a query that doesn’t execute
until you call `.collect()`. You must use `.collect()` to execute the
query and return results—nothing is stored in your table. If you want to
collect only the first few rows, use `.head(n)` instead of `.collect()`
to test on a subset before processing your full dataset. Once satisfied,
use `.add_computed_column()` with the same expression to persist results
permanently.

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

## See also

* [Test transformations with fast feedback
  loops](https://docs.pixeltable.com/howto/cookbooks/core/dev-iterative-workflow.md)
* [Apply image
  filters](https://docs.pixeltable.com/howto/cookbooks/images/img-apply-filters.md)
* *Pillow techniques from [Real Python: Image Processing With the Python
  Pillow
  Library](https://realpython.com/image-processing-with-the-python-pillow-library/)*


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