Skip to main content

Pandas Image Methods

Image methods for pandas dataframes using Pillow.

Features:

  • Use PIL.Image objects in pandas dataframes
  • Call PIL.Image methods on a column, for example:
    • .crop()
    • .filter()
    • .resize()
    • .rotate()
    • .transpose()
  • Save dataframes with PIL.Image objects to Parquet
  • Process images in parallel with Dask
  • Manipulate image datasets from Hugging Face

Installation

pip install pandas-image-methods

Usage

You can open images as PIL.Image objects using the .open() method.

Once the images are opened, you can call any PIL Image method:

import pandas as pd
from pandas_image_methods import PILMethods

pd.api.extensions.register_series_accessor("pil")(PILMethods)

df = pd.DataFrame({"file_path": ["path/to/image.png"]})
df["image"] = df["file_path"].pil.open()
df["image"] = df["image"].pil.rotate(90)
# 0    <PIL.Image.Image size=200x200>
# Name: image, dtype: object, PIL methods enabled

Here is how to enable PIL methods for PIL Images created manually:

df = pd.DataFrame({"image": [PIL.Image.open("path/to/image.png")]})
df["image"] = df["image"].pil.enable()
df["image"] = df["image"].pil.rotate(90)
# 0    <PIL.Image.Image size=200x200>
# Name: image, dtype: object, PIL methods enabled

Save

You can save a dataset of PIL Images to Parquet:

# Save
df = pd.DataFrame({"file_path": ["path/to/image.png"]})
df["image"] = df["file_path"].pil.open()
df.to_parquet("data.parquet")

# Later
df = pd.read_parquet("data.parquet")
df["image"] = df["image"].pil.enable()

This doesn't just save the paths to the image files, but the actual images themselves !

Under the hood it saves dictionaries of {"bytes": <bytes of the image file>, "path": <path or name of the image file>}. The images are saved as bytes using their image encoding or PNG by default. Anyone can load the Parquet data even without pandas-image-methods since it doesn't rely on extension types.

Note: if you created the PIL Images manually, don't forget to enable the PIL methods to enable saving to Parquet.

Run in parallel

Dask DataFrame parallelizes pandas to handle large datasets. It enables faster local processing with multiprocessing as well as distributed large scale processing. Dask mimics the pandas API:

import dask.dataframe as dd
from distributed import Client
from pandas_image_methods import PILMethods

dd.api.extensions.register_series_accessor("pil")(PILMethods)

if __name__ == "__main__":
    client = Client()
    df = dd.read_csv("path/to/large/dataset.csv")
    df = df.repartition(npartitions=1000)  # divide the processing in 1000 jobs
    df["image"] = df["file_path"].pil.open()
    df["image"] = df["image"].pil.rotate(90)
    df["image"].head(1)
    # 0    <PIL.Image.Image size=200x200>
    # Name: image, dtype: object, PIL methods enabled
    df.to_parquet("data_folder")

Hugging Face support

Most image datasets in Parquet format on Hugging Face are compatible with pandas-image-methods. For example you can load the CIFAR-100 dataset:

df = pd.read_parquet("hf://datasets/uoft-cs/cifar100/cifar100/train-00000-of-00001.parquet")
df["image"] = df["image"].pil.enable()

Datasets created with pandas-image-methods and saved to Parquet are also compatible with the Dataset Viewer on Hugging Face and the datasets library:

df.to_parquet("hf://datasets/username/dataset_name/train.parquet")

Display in Notebooks

You can display a pandas dataframe of images in a Jupyter Notebook or on Google Colab in HTML:

from IPython.display import HTML
HTML(df.head().to_html(escape=False, formatters={"image": df.image.pil.html_formatter}))

Example on the julien-c/impressionists dataset for painting classification:

output of the html formatter on Colab

Metadata

Release files for pandas-image-methods 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pandas-image-methods 0.3.3
File Size Uploaded
pandas_image_methods-0.3.3.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pandas-image-methods 0.3.3
File Interpreter ABI Platform
pandas_image_methods-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / pandas_image_methods-0.3.3.tar.gz

Download URL pandas_image_methods-0.3.3.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f48fb7b3c5abc17c055ff7000917c31958acaa1a86c1b39c730171d9431deb5b
BLAKE2b-256 checksum
How to use checksums
0452a5c9f8425307213171f8c7b82f2f6a8e62c3f96c6fe54caa44e94e3c6565
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.2 Darwin/23.4.0

Release files / pandas_image_methods-0.3.3-py3-none-any.whl

Download URL pandas_image_methods-0.3.3-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e12bd8cc235041c25fa31fe35de910f6c0f0d85ecf542b953f3a22cdd4c5556f
BLAKE2b-256 checksum
How to use checksums
1733687aee71d63ad8844eaaaab3b5a809edfcb06421331c044e21ee605d3862
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.2 Darwin/23.4.0

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page