TensorBoard
Image Extraction
Event Summary
Python
Data Visualization

How to extract and save images from tensorboard event summary?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

TensorBoard event files can contain image summaries alongside scalars, histograms, and other logged data. If you want to save those images back to disk, the basic job is to iterate through the event file, find summary values tagged as images, decode the stored bytes, and write them as image files.

The older TensorFlow event-summary format stores image data in Summary.Image. For those files, tf.compat.v1.train.summary_iterator is a practical way to extract the images.

Iterate Through the Event File

A basic extraction script looks like this:

python
1import io
2from pathlib import Path
3
4from PIL import Image
5import tensorflow as tf
6
7
8def extract_images(event_file, output_dir, tag_filter=None):
9    output_dir = Path(output_dir)
10    output_dir.mkdir(parents=True, exist_ok=True)
11
12    image_count = 0
13
14    for event in tf.compat.v1.train.summary_iterator(event_file):
15        if not event.summary:
16            continue
17
18        for value in event.summary.value:
19            if tag_filter is not None and value.tag != tag_filter:
20                continue
21
22            if value.HasField("image"):
23                encoded = value.image.encoded_image_string
24                image = Image.open(io.BytesIO(encoded))
25                filename = output_dir / f"{value.tag}_{image_count:04d}.png"
26                image.save(filename)
27                image_count += 1
28
29    return image_count
30
31
32count = extract_images(
33    event_file="runs/events.out.tfevents.example",
34    output_dir="saved_images",
35)
36print(f"saved {count} images")

This walks through every event, checks its summary values, and saves each image to a PNG file.

How the Image Data Is Stored

In classic summary files, image summaries appear in the image field of a summary value. The actual bytes are already encoded, which is why the script can pass them directly into PIL.Image.open using an in-memory buffer.

The tag name becomes useful metadata. If your event file contains several image streams, naming the output files with the tag helps you keep them organized.

Filter by Tag

TensorBoard logs can contain many summary entries. If you only want one specific image stream, filter by tag:

python
1count = extract_images(
2    event_file="runs/events.out.tfevents.example",
3    output_dir="saved_images",
4    tag_filter="examples/input_images",
5)

That makes extraction much faster and avoids dumping unrelated summaries.

When Newer TensorFlow Logging Looks Different

Modern TensorFlow code sometimes logs data through tensor summaries rather than the older Summary.Image field. In those cases, the event file may not look exactly like the legacy examples.

So if value.HasField("image") never matches, inspect the summary contents first and check whether the image data was logged in a tensor-based form instead.

The general lesson is that the extraction strategy depends on how the summary was originally written.

Practical Workflow Tips

A few habits make image extraction easier:

  • start by printing available tags before saving anything
  • extract from one event file first before processing a whole run directory
  • keep output filenames deterministic so reruns do not become confusing
  • filter early if the log file is large

If the event file is huge, image extraction can take time because the iterator must scan all events sequentially.

It is also worth saving into a fresh output directory for each run so you can compare extracted images across experiments without mixing files.

Common Pitfalls

The biggest mistake is assuming every summary value is an image. Event files often contain mostly non-image data.

Another mistake is ignoring tags and then overwriting outputs or mixing several image streams into one folder without meaningful names.

A third issue is using a script written for old-style image summaries on a run that logged images differently. If extraction returns zero images, inspect the actual summary structure before assuming the file is empty.

Summary

  • Use tf.compat.v1.train.summary_iterator to walk through TensorBoard event files.
  • Look for summary values that contain an image field.
  • Decode encoded_image_string and save it with PIL.
  • Filter by tag when you only want one image stream.
  • If no images appear, verify how the summaries were originally logged.

Course illustration
Course illustration

All Rights Reserved.