Jupyter Notebook
Python
Image Display
Data Visualization
Tutorial

How can I display an image from a file in Jupyter Notebook?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Displaying an image from disk inside Jupyter Notebook is a routine task in data science, machine learning, and debugging workflows. The best method depends on what you need: quick inline display, plotting with axes, or pixel-level image processing before rendering. In practice, the most common tools are IPython.display, Pillow, and Matplotlib.

Use IPython.display.Image for the Simplest Inline Display

If you only want to show an image file and do not need plotting controls, IPython.display.Image is the most direct option.

python
from IPython.display import Image, display

display(Image(filename="images/sample.png"))

This renders the file inline in the output cell. It is lightweight and usually the best choice for quick notebook inspection.

You can also control display width or height:

python
from IPython.display import Image, display

display(Image(filename="images/sample.png", width=300))

That changes notebook rendering size without modifying the underlying file.

Use Pillow When You Need to Load or Inspect the Image

Pillow is useful when you want to open the file, inspect metadata, or preprocess the image before display.

python
1from PIL import Image
2from IPython.display import display
3
4img = Image.open("images/sample.png")
5print(img.size)
6print(img.mode)
7display(img)

This approach is convenient when you need to check dimensions, color mode, or image format while working interactively.

If you need a converted version, do that before display:

python
1from PIL import Image
2from IPython.display import display
3
4img = Image.open("images/sample.png").convert("L")
5display(img)

That example converts the image to grayscale in memory and shows the result immediately.

Use Matplotlib for Plotting Workflows

Matplotlib is a better fit when the image is part of a plot or when you want titles, axes, or side-by-side comparisons.

python
1import matplotlib.pyplot as plt
2import matplotlib.image as mpimg
3
4img = mpimg.imread("images/sample.png")
5
6plt.figure(figsize=(6, 4))
7plt.imshow(img)
8plt.axis("off")
9plt.title("Sample image")
10plt.show()

This is especially useful in computer vision notebooks where you compare raw inputs, masks, and predictions in the same figure.

Display Multiple Images in One Notebook Cell

For experiments, you often want to compare files side by side. Matplotlib makes that straightforward.

python
1import matplotlib.pyplot as plt
2from PIL import Image
3
4paths = ["images/a.png", "images/b.png"]
5
6fig, axes = plt.subplots(1, 2, figsize=(8, 4))
7
8for ax, path in zip(axes, paths):
9    img = Image.open(path)
10    ax.imshow(img)
11    ax.set_title(path)
12    ax.axis("off")
13
14plt.tight_layout()
15plt.show()

This pattern scales well for inspection workflows such as dataset verification and augmentation review.

Work with Relative and Absolute Paths Carefully

Image display errors in Jupyter are often just path mistakes. The notebook kernel resolves relative paths from the current working directory, not necessarily from the notebook file location you expect.

python
1from pathlib import Path
2from IPython.display import Image, display
3
4image_path = Path("images") / "sample.png"
5print(image_path.resolve())
6display(Image(filename=str(image_path)))

Printing the resolved path is a quick way to confirm the notebook is looking in the right place.

Use Numpy Arrays When the Image Comes from Processing Code

If the image is already loaded as a Numpy array, display it with Matplotlib directly.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4img = np.zeros((100, 100, 3), dtype=np.uint8)
5img[:, :50] = [255, 0, 0]
6img[:, 50:] = [0, 0, 255]
7
8plt.imshow(img)
9plt.axis("off")
10plt.show()

That is common in model debugging where images are generated in memory instead of read from files.

Choose the Right Method

In practice:

  • use IPython.display.Image for quick inline rendering from disk
  • use Pillow when you need file metadata or transformations
  • use Matplotlib when the image is part of a larger visualization

Choosing the simplest tool that matches the task keeps notebooks easier to read and maintain.

Common Pitfalls

The most common mistake is using the wrong file path. A missing file looks like an image-display problem when it is really a working-directory problem.

Another issue is forgetting that some libraries return arrays in different channel orders or value ranges. If colors look wrong, verify whether the image is RGB, grayscale, or normalized float data.

Large images can also make notebooks slow. When browsing many files, resize for display rather than rendering full-resolution versions every time.

Summary

  • Use IPython.display.Image for the quickest way to show a file inline.
  • Use Pillow when you need to inspect or transform the image before rendering.
  • Use Matplotlib when the image belongs in a plot or comparison layout.
  • Confirm file paths with Path.resolve() when display fails.
  • Keep notebook rendering responsive by resizing very large images for display.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.