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.
Problem
You need to make hundreds of images semi-transparent for backgrounds, overlays, or watermarks.Solution
What’s in this recipe:- Set image opacity (transparency level)
- Test transformations before applying
- Apply to multiple images automatically
.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 opacity adjustment
to all images in your table.
For more on this workflow, see Get fast feedback on
transformations.
Setup
Load images
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘image_demo’.
<pixeltable.catalog.dir.Dir at 0x302d6af80>
Created table ‘opacity’.
Inserting rows into `opacity`: 0 rows [00:00, ? rows/s]Inserting rows into `opacity`: 3 rows [00:00, 545.05 rows/s]
Inserted 3 rows with 0 errors.
3 rows inserted, 6 values computed.
Iterate: adjust opacity for a few images first
You define a custom function using the@pxt.udf decorator to make it
available in Pixeltable. Inside the function, you use standard PIL
(Pillow) operations to manipulate images. Pixeltable handles applying
your function to every row in your table.
How it works:
- All image manipulation (
.convert(),.split(),.point(),.putalpha()) comes from the PIL/Pillow library - These are standard Python image operations—see Pillow docs for reference
- The
@pxt.udfdecorator lets Pixeltable apply your function to table rows - The opacity parameter (0.0 = fully transparent, 1.0 = fully opaque) controls the alpha scaling
Add: adjust opacity for all images in your table
Added 3 column values with 0 errors.
3 rows updated, 3 values computed.
Explanation
How the opacity technique works: The UDF modifies the alpha channel to control transparency. The function converts the image to RGBA mode (which includes an alpha channel for transparency), extracts the alpha channel with.split()[3], scales all
values by the desired opacity factor using
.point(lambda p: int(p * opacity)), and applies it back with
.putalpha(). This preserves the original image while adjusting only
the transparency level.
To customize the UDF:
- Opacity levels: Use 0.25 for very faint backgrounds, 0.5 for standard transparency, 0.75 for subtle effects
- Selective transparency: Modify the lambda function in
.point()to apply different transparency to different pixel values - Preserve regions: Add conditional logic to keep certain areas fully opaque
.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.