Skip to content

Adding alt text to images from text embedded in image metadata #14482

@psychemedia

Description

@psychemedia

Via jupyter/enhancement-proposals#120 which mentions previously added support to IPython's rendering of image alt text (#12864), albeit apparently limited to images pulled from URLs (#13577), I was wondering whether it might be useful to optionally seed the alt tag from description text embedded within an image?

For example, description data could be accessed as description data from a PNG file as:

from PIL import Image
import PIL.PngImagePlugin
 
def read_png_metadata(filename):
    with Image.open(filename) as img:
        metadata = {
            "text": img.text,
            "info": img.info
        }
    return metadata
 
# Assuming you've saved your image as 'tips_scatter.png'
filename = 'tips_scatter.png'
metadata = read_png_metadata(filename)
 
print("Image Metadata:")
for key, value in metadata.items():
    print(f"{key}:")
    if isinstance(value, dict):
        for k, v in value.items():
            print(f"  {k}: {v}")
    else:
        print(f"  {value}")
 
alt_text = metadata['info'].get('Description', '')

So it might be useful to be able to render something like Image(..., extract_alt="Description').

The JEP issue also points to matplotalt, a simple project that mooted seeding matplotlib alt text from image object properties and then embedding that text inside the generated images. It is easy enough to imagine extending that sort of approach to other charting packages, such as seaborn. There would be even more of drive for this if IPython handled this natively, rather than eg a requirement to build custom mimetype handlers in JupyterLab that can extract text from images and then render it as alt text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions