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 convert color images to grayscale for analysis, preprocessing, or model inputs that require single-channel images. Different conversion methods produce different results—you need to choose the right approach for your use case.Solution
What’s in this recipe:- Simple conversion with PIL
- Perceptually accurate grayscale (weighted RGB channels)
- Custom UDF for advanced conversion
.convert() method for standard conversion, or a
custom UDF (relies on NumPy and PIL/Pillow) for gamma-corrected
conversion when scientific accuracy matters.
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 conversion to all
images in your table.
For more on this workflow, see Get fast feedback on
transformations.
Conversion methods:
The simple method uses PIL’s built-in conversion. The gamma-corrected
method requires a custom UDF (not built into PIL) that applies
perceptual weighting in linear color space.
For technical details on gamma correction and grayscale conversion, see
Wikipedia: Grayscale.
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 0x16c637d90>
Created table ‘gray’.
Inserting rows into `gray`: 0 rows [00:00, ? rows/s]Inserting rows into `gray`: 3 rows [00:00, 617.66 rows/s]
Inserted 3 rows with 0 errors.
3 rows inserted, 6 values computed.
Iterate: convert with linear approximation for a few images first
Add: convert with linear approximation for all images in your table
Added 3 column values with 0 errors.
3 rows updated, 3 values computed.
Iterate: convert with gamma decompression for a few images first
Add: convert with gamma decompression for all images in your table
Added 3 column values with 0 errors.
3 rows updated, 3 values computed.
Explanation
Two approaches:-
Simple (
.convert('L')): PIL’s built-in. Fast, good for most use cases (model preprocessing, general analysis). -
Gamma-corrected (custom UDF): Not built into PIL. Requires a
custom UDF that:
- Gamma-decompresses to linear space
- Applies perceptual weights: 0.2126 × R + 0.7152 × G + 0.0722 × B
- Gamma-compresses back for display
- Slower but most perceptually accurate
- Use for scientific imaging, professional photography
.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.