Skip to main content

A library for embedding code and plot properties into matplotlib images for LLM context

Project description

ImageForLLM

A package to embed code and plot properties into matplotlib images for LLM context.

Overview

ImageForLLM enables embedding source code and plot properties into matplotlib images, particularly useful when sharing plots with Large Language Models (LLMs). This allows the LLM to understand how the plot was generated and what it represents.

Installation

pip install imageforllm

The package requires Pillow for metadata embedding:

pip install Pillow

Features

  • Embed source code that generated a plot into the image metadata
  • Automatically extract and embed plot properties (titles, labels, etc.)
  • Extract embedded code and properties from images
  • Command-line tool for extracting metadata from images

Usage

Basic Workflow

import matplotlib.pyplot as plt
import numpy as np
import imageforllm

# 1. Hook matplotlib's savefig function
imageforllm.hook_image_save()

# 2. Define your plot code as a string
plot_source_code = """
It make work for a wave plot.
"""

# 3. Create your plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')

# 4. Save with embedded code and auto-extracted properties
plt.savefig('sine_wave_plot.png', create_comment=plot_source_code)

# 5. (Optional) Unhook when done
imageforllm.unhook_image_save()

Extracting Metadata

import imageforllm

# Get metadata from an image
info = imageforllm.get_image_info('sine_wave_plot.png')

# Access embedded code
code = info.get(imageforllm.METADATA_KEY_CODE)
print(code)

# Access plot properties
properties = info.get(imageforllm.METADATA_KEY_PROPERTIES)
print(properties)

Command-line Extraction

The package includes a command-line tool for extracting metadata:

# Extract and print code
python -m imageforllm.extract sine_wave_plot.png

# Extract and print all metadata
python -m imageforllm.extract sine_wave_plot.png --info

# Extract only plot properties
python -m imageforllm.extract sine_wave_plot.png --properties

# Output in JSON format
python -m imageforllm.extract sine_wave_plot.png --json

# Save extracted code to a file
python -m imageforllm.extract sine_wave_plot.png -o extracted_code.py

Limitations

  • Metadata embedding is primarily supported for PNG format
  • When saving to file-like objects, metadata embedding is not supported
  • The package cannot automatically determine the code that generated a plot; you must provide it as a string

How It Works

  1. The package hooks matplotlib's savefig function
  2. When saving, it captures any provided source code and automatically extracts plot properties
  3. It embeds this metadata into the PNG image using Pillow
  4. Metadata can later be extracted from the image using the provided functions or command-line tool

Example

See the included examples/saveandread.py for a complete example of saving and reading metadata.

API Reference

Main Functions

  • hook_image_save(): Replaces matplotlib's savefig with a version that embeds metadata
  • unhook_image_save(): Restores the original savefig function
  • get_image_info(image_path): Extracts metadata from an image file

Constants

  • METADATA_KEY_CODE: Key for source code in metadata dictionary
  • METADATA_KEY_PROPERTIES: Key for plot properties in metadata dictionary

License

[License information]

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

imageforllm-0.3.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

imageforllm-0.3.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file imageforllm-0.3.0.tar.gz.

File metadata

  • Download URL: imageforllm-0.3.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for imageforllm-0.3.0.tar.gz
Algorithm Hash digest
SHA256 38c3cb058fb56259b6768e3691550a4d3f447c3512d5cebadeb4b27236101ce2
MD5 37c21e482a2f670fefc5826b982834ce
BLAKE2b-256 73447f77b491ac9b7ae162f9eaab5d029db7686e61ff6150f466f72decce5013

See more details on using hashes here.

File details

Details for the file imageforllm-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: imageforllm-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for imageforllm-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7725c4c9fdae5c406119300f90498c53135ce5f9a8aff2a3a3d0f688220df0cf
MD5 48c0e48c2505c754c0c92be127a6a8e3
BLAKE2b-256 bc01f84d48011819224b7f1bcb3a5e9f550baf286720a806ef3dc8850ece12a1

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