Skip to content

Commit bd5aac4

Browse files
committed
Insert the image loading section
1 parent be96d1d commit bd5aac4

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

episodes/02-image-basics.md

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ but that each pixel can have a different colour from its neighbors.
5555
Viewed from a distance,
5656
these pixels seem to blend together to form the image we see.
5757

58+
Real-world images are typically made up of a vast number of pixels,
59+
and each of these pixels is one of potentially millions of colours.
60+
While we will deal with pictures of such complexity in this lesson,
61+
let's start our exploration with just 15 pixels in a 5 x 3 matrix with 2 colours,
62+
and work our way up to that complexity.
63+
5864
::::::::::::::::::::::::::::::::::::::::: callout
5965

6066
## Matrices, arrays, images and pixels
@@ -70,30 +76,64 @@ the terms matrix and array will be used interchangeably.
7076

7177
::::::::::::::::::::::::::::::::::::::::::::::::::
7278

73-
## Working with pixels
79+
## Loading images
7480

75-
As noted, in practice,
76-
real-world images will typically be made up of a vast number of pixels,
77-
and each of these pixels will be one of potentially millions of colours.
78-
While we will deal with pictures of such complexity shortly,
79-
let's start our exploration with 15 pixels in a 5 x 3 matrix with 2 colours and
80-
work our way up to that complexity.
81+
As noted, images we want to analyze (process) with Python are loaded into arrays.
82+
There are multiple ways to load images. In this lesson, we use imageio, a Python
83+
library for reading (loading) and writing (saving) image data, and more specifically
84+
its version 3. But, really, we could use any image loader which would return a
85+
NumPy array.
8186

82-
First, the necessary imports:
87+
```python
88+
"""Python library for reading and writing images."""
89+
90+
import imageio.v3 as iio
91+
```
92+
93+
The `v3` module of imageio (`imageio.v3`) is imported as `iio` (see note in
94+
the next section).
95+
Version 3 of imageio has the benefit of supporting nD (multidimensional) image data
96+
natively (think of volumes, movies).
97+
98+
Let us load our image data from disk using
99+
the `imread` function from the `imageio.v3` module.
100+
101+
```python
102+
eight = iio.imread(uri="data/eight.tif")
103+
print(type(eight))
104+
```
105+
106+
Note that, using the same image loader or a different one, we could also read in
107+
remotely hosted data.
108+
109+
::::::::::::::::::::::::::::::::::::::::: callout
110+
111+
## Why not use `skimage.io.imread()`
112+
113+
The scikit-image library has its own function to read an image,
114+
so you might be asking why we don't use it here.
115+
Actually, `skimage.io.imread()` uses `iio.imread()` internally when loading an image into Python.
116+
It is certainly something you may use as you see fit in your own code.
117+
In this lesson, we use the imageio library to read or write images,
118+
while scikit-image is dedicated to performing operations on the images.
119+
Using imageio gives us more flexibility, especially when it comes to
120+
handling metadata.
121+
122+
::::::::::::::::::::::::::::::::::::::::::::::::::
123+
124+
## Working with pixels
125+
126+
First, let us add the necessary imports:
83127

84128
```python
85129
"""Python libraries for learning and performing image processing."""
86130

87-
import imageio.v3 as iio
88131
import ipympl
89132
import matplotlib.pyplot as plt
90133
import numpy as np
91134
import skimage as ski
92135
```
93136

94-
The `v3` module of imageio (`imageio.v3`) is imported as `iio`. This module
95-
enables us to read and write images.
96-
97137
:::::::::::::::::::::::::::::::::::::::: callout
98138

99139
## Import statements in Python
@@ -162,32 +202,10 @@ more efficiently run commands later in the session.
162202
%matplotlib widget
163203
```
164204

165-
With that taken care of,
166-
let's load our image data from disk using
167-
the `imread` function from the `imageio.v3` module and display it using
205+
With that taken care of, let us display the image we have loaded, using
168206
the `imshow` function from the `matplotlib.pyplot` module.
169-
Imageio is a Python library for reading and writing image data.
170-
`imageio.v3` is specifying that we want to use version 3 of imageio. This
171-
version has the benefit of supporting nD (multidimensional) image data
172-
natively (think of volumes, movies).
173-
174-
::::::::::::::::::::::::::::::::::::::::: callout
175-
176-
## Why not use `skimage.io.imread()`
177-
178-
The scikit-image library has its own function to read an image,
179-
so you might be asking why we don't use it here.
180-
Actually, `skimage.io.imread()` uses `iio.imread()` internally when loading an image into Python.
181-
It is certainly something you may use as you see fit in your own code.
182-
In this lesson, we use the imageio library to read or write (save) images,
183-
while scikit-image is dedicated to performing operations on the images.
184-
Using imageio gives us more flexibility, especially when it comes to
185-
handling metadata.
186-
187-
::::::::::::::::::::::::::::::::::::::::::::::::::
188207

189208
```python
190-
eight = iio.imread(uri="data/eight.tif")
191209
plt.imshow(eight)
192210
```
193211

0 commit comments

Comments
 (0)