SoatDev IT Consulting
SoatDev IT Consulting
  • About us
  • Expertise
  • Services
  • How it works
  • Contact Us
  • News
  • October 27, 2023
  • Rss Fetcher

Presenting images in Jupyter Notebook cells

Photo by the author

There are times when data science practitioners and their target audiences need to check images while working on code in Jupyter files. This article lays out various ways to present images in Jupyter Notebooks cells, as well as the tradeoffs between each method.

Content

  • Add Images to Markdown Cells
  • Add Images to Python Cells

Add Images to Markdown Cells

Method 1: Display the Images with Markdown Syntax

The most straightforward method is switching a Jupyter cell into Markdown format, and passing in the images with Markdown syntax.

![alt text](image_path)

The format comprises 3 parts:

  1. Exclamation mark !
  2. Square bracket [] with alternate texts to be presented if the image is not found
  3. Parentheses () with the desired image to be displayed

The image path can either be

  • an image URL
  • a local image path (absolute/relative)

Below are examples of each variant.

**Image URL**

![Sample Image](https://i.pinimg.com/564x/88/67/a4/8867a454935fa63595614875e48a6b53--samoyed-funny-samoyed-dogs.jpg)

**Local Relative Path**

![Sample Image](metadata/samoyed.jpg)

**Local Absolute Path**

![Sample Image](mypath/to/metadata/samoyed.jpg)

Check out the code in Jupyter Notebook

Tradeoffs:

Pros:

  1. Straightforward
  2. Simple syntax, easy to remember

Cons:

  1. Not able to make any changes, such as adjusting the side

The perk of this approach is that it is really simple. Do it a few times, and you will remember it at the back of your mind. The downside, however, is the lack of knobs to adjust the image size. This is often necessary when it comes to working with images from multiple sources.

Method 2: Display the Images with HTML Syntax

If you are familiar with the frontend stack, HTML should be no stranger to you.

<img src="image_path" width="width_length" height="height_length" alt="alt text">

The src parameter is necessary while the rest are optional.

<img src="image_path">

Like the previous method, the image_path can be from different sources as illustrated below:

**Image URL**

<img src="https://i.pinimg.com/564x/88/67/a4/8867a454935fa63595614875e48a6b53--samoyed-funny-samoyed-dogs.jpg">

**Local Relative Path**

<img src="metadata/samoyed.jpg">

**Local Absolute Path**

<img src="mypath/to/metadata/samoyed.jpg">

Check out the code in Jupyter Notebook

When it comes to adjusting the size of the image, changing either one (width/height) keeps the aspect ratio of the image. This has been useful instead of trying for a few runs (between the combination of different widths and heights) to check on the outcome of the image.

<img src="metadata/samoyed.jpg" width="250">

Check out the code in Jupyter Notebook

Tradeoffs:

Pros:

  1. Able to adjust the size (width, height, or both)

Cons:

  1. Small adoption curves if users are not familiar with HTML syntax

Add Images to Python Cells

Import the necessary libraries

To display images using Python, install the libraries with requirements.txt

pip install -r requirements.txt

The dependencies are laid out below.

numpy==1.25.0
imread-from-url==0.1.3
opencv-python==4.8.0.74
matplotlib==3.7.2
ipympl==0.9.3

Likewise, the image source can be either of the options below.

# Image URL

Image(url = image_url, width=value, height=value)

# Local Relative/Absolute Path

Image(filename = "path/to/image", width=value, height=value)

Adjusting of image can be performed with the parameter width and height.

Examples are shown below.

# URL

Image(url="https://i.pinimg.com/564x/88/67/a4/8867a454935fa63595614875e48a6b53--samoyed-funny-samoyed-dogs.jpg")

# Adjust the size

Image(filename="metadata/samoyed.jpg", width="250") # use either one to keep the aspect ratio

Check out the code in Jupyter Notebook

Tradeoffs:

Pros:

  1. Works seamlessly during the development stage, especially if Python is preferred.

Cons:

  1. Small adoption curves for first-time users.

For more sophisticated tasks such as drawing rectangles on images.

Check out the code below:

%matplotlib inline
%matplotlib widget

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as patches

#change R, B axis to correctly display color
outimg = img[:,:,::-1]

imgplot = plt.imshow(outimg, aspect = "auto")

# Create figure and axes
fig, ax = plt.subplots(1)

ax.set_axis_off()
# Display the image
ax.imshow(outimg)

rectleftup = (50, 120)
rectwidth = 260
rectheight = 220
linewidth = 2

# Create a Rectangle patch
# https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html
rect = patches.Rectangle(rectleftup, rectwidth, rectheight, linewidth=linewidth,
edgecolor='b', fill = False)

# Add the patch to the Axes
_ = ax.add_patch(rect)

Check out the code in Jupyter Notebook

Stay tuned for the next post on Part 2: Displaying Videos in Jupyter Notebook.

  • GitHub Repository
  • Profile

Thanks for reading.


Part 1: Displaying Images in Jupyter Notebook was originally published in Better Programming on Medium, where people are continuing the conversation by highlighting and responding to this story.

Previous Post
Next Post

Recent Posts

  • Computing the Euler-Mascheroni Constant
  • Golden ratio base numbers
  • Pioneering Apple engineer Bill Atkinson dies at 74
  • Lawyers could face ‘severe’ penalties for fake AI-generated citations, UK court warns
  • At the Bitcoin Conference, the Republicans were for sale

Categories

  • Industry News
  • Programming
  • RSS Fetched Articles
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Tap into the power of Microservices, MVC Architecture, Cloud, Containers, UML, and Scrum methodologies to bolster your project planning, execution, and application development processes.

Solutions

  • IT Consultation
  • Agile Transformation
  • Software Development
  • DevOps & CI/CD

Regions Covered

  • Montreal
  • New York
  • Paris
  • Mauritius
  • Abidjan
  • Dakar

Subscribe to Newsletter

Join our monthly newsletter subscribers to get the latest news and insights.

© Copyright 2023. All Rights Reserved by Soatdev IT Consulting Inc.