Skip to main content

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

Project description

ImageForLLM

A free lunch for LLM recognition images

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.

Easyuse

With just TWO lines, it can automatically add the information of the generated image in the metadata of the image generated by matplotlib for you without any additional operations.

For LLM or other readers, the content of the picture can be quickly understood through this information.

import imageforllm
imageforllm.hook_image_save()

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
comment = info.get(imageforllm.METADATA_KEY_COMMENT)
print(comment)

# 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.1.tar.gz (13.7 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.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: imageforllm-0.3.1.tar.gz
  • Upload date:
  • Size: 13.7 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.1.tar.gz
Algorithm Hash digest
SHA256 98b2ef3390d0351c9901f54c9fa1437092936ee75d4b26bf046bf8cb23ffc198
MD5 37b3ed462f98f8223fc18cff3ba4a845
BLAKE2b-256 c6cdd5207fc4ee624334ebd7f5c0cadba873fa1ab577da01b6549b5696c4c6f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: imageforllm-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4db8f1fc57603c0e0a73fb15a62669b52c9f3e3a2d5e01b340f8876d4172752e
MD5 4763d3130fa2dd62adf1a28bb6647f73
BLAKE2b-256 5bddf6c0fc2f11127cf0102b6eb1af25835267c4b1332fdf40d85518f0330436

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