Skip to main content

Image methods for pandas dataframes using Pillow

Project description

Pandas Image Methods

Image methods for pandas dataframes using Pillow

Installation

pip install pandas-image-methods

Usage

You can open images as PIL.Image.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({"image": ["path/to/image.png"]})
df["image"] = df["image"].pil.open()
df["image"] = df["image"].pil.rotate(90)

Save

You can save a dataset of PIL Images in Parquet:

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

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

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

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.open()

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

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

Parquet content

Thanks to a pandas ExtensionArray, pandas-image-methods is able to read and write dataframes of PIL Images to Parquet automatically. 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. This doesn't rely on extension types on purpose to allow people that don't have pandas-image-methods to load the Parquet data anyway.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandas_image_methods-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandas_image_methods-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file pandas_image_methods-0.1.0.tar.gz.

File metadata

  • Download URL: pandas_image_methods-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.2 Darwin/23.4.0

File hashes

Hashes for pandas_image_methods-0.1.0.tar.gz
Algorithm Hash digest
SHA256 63953c3d5c49b3e89728b33c8d576cc82f75159c076165fe395cb3a5937ced41
MD5 53b867a8907cb53e19f645e14b4f6dc4
BLAKE2b-256 ae361724387c10d3adac81b783c678ac64e1a058953c2a2786353053d3acf534

See more details on using hashes here.

File details

Details for the file pandas_image_methods-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pandas_image_methods-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9ded501be879a1234f3072709021b02520352184572df2dc84c7978aba3807d
MD5 53c71bef1820d269b0fd707d77955ea3
BLAKE2b-256 ddd39de8ecaf67aeeef07f5e5171b8ee10870695d00a39b58c9c07ca233696e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page