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.
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.
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:
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.
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:
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.
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.
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.
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.
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.Imagefor 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.Imagefor 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
- How can I display full non-truncated dataframe information in HTML when converting from Pandas dataframe to HTML?
- How can I do efficient range searching counting with latitude/longitude data?
- How can I do scatter and gather operations in NumPy?
- How can I efficiently use an R prediction model from Java?
- How can I do a line break (line continuation) in Python (split up a long line of source code)?
- How can I do a line break line continuation in Python split up a long line of source code?
- How can I find the center of a cluster of data points?
- How can I fit a Bézier curve to a set of data?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.