Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib imread() Method

Matplotlib imread() Method

The imread() method is a function in the Matplotlib library used to read image data from an image file.

The imread() method returns a numpy.ndarray object whose shape is (nrows, ncols, nchannels), representing the number of rows, columns, and channels of the read image:

The syntax of the imread() method is as follows:

matplotlib.pyplot.imread(fname, format=None)

Parameter Description:

The following example demonstrates how to use the imread function to read image data from an image file and display it:

Example

import

matplotlib.

pyplot

as

plt

Read the image file, download address: https://static.jyshare.com/images/demo/map.jpeg

img

=

plt.

imread

(

'map.jpeg'

)

Displays an image

plt.

imshow

(

img

)

plt.

show

(

)

In the example above, we first use the imread() method to read image data from an image file named map.jpeg and store it in the img variable.

Then we use the imshow() method to display this image.

Note: We did not specify a color map when displaying the image, because the imread() method has already converted the image data into RGB format with the correct color mapping, so we can use the default color map directly to display the image.

The display result is as follows:

We can modify an image by changing the numpy array.

For example, if we multiply the array by a number 0≤≤1, we darken the image:

Example

import

matplotlib.

pyplot

as

plt

Read the image file, download address: https://static.jyshare.com/images/mix/tiger.jpeg

img_array

=

plt.

imread

(

'tiger.jpeg'

)

tiger

=

img_array/

255

#print(tiger)

Displays an image

plt.

figure

(

figsize

=

(

10

,

6

)

)

for

i

in

range

(

1

,

5

)

:

    plt.

subplot

(

2

,

2

,

i

)

    x

=

1

0.2

(

i-

1

)

    plt.

axis

(

'off'

)

#hide coordinate axes

    plt.

title

(

'x={:.1f}'

.

format

(

x

)

)

    plt.

imshow

(

tiger*x

)

plt.

show

(

)

The display result is as follows:

The following example is used to crop an image:

Example

import

matplotlib.

pyplot

as

plt

Read the image file, download address: https://static.jyshare.com/images/mix/tiger.jpeg

img_array

=

plt.

imread

(

'tiger.jpeg'

)

tiger

=

img_array/

255

#print(tiger)

Displays an image

plt.

figure

(

figsize

=

(

6

,

6

)

)

plt.

imshow

(

tiger

[

:

300

,

100

:

400

,

:

]

)

plt.

axis

(

'off'

)

plt.

show

(

)

The display result is as follows:

If we set the array elements for the green and blue coordinates of the RGB color to 0, we get a red image:

Example

import

matplotlib.

pyplot

as

plt

Read the image file, download address: https://static.jyshare.com/images/mix/tiger.jpeg

img_array

=

plt.

imread

(

'tiger.jpeg'

)

tiger

=

img_array/

255

#print(tiger)

Displays an image

red_tiger

=

tiger.

copy

(

)

red_tiger

[

:

,

:

,

[

1

,

2

]

]

=

0

plt.

figure

(

figsize

=

(

10

,

10

)

)

plt.

imshow

(

red_tiger

)

plt.

axis

(

'off'

)

plt.

show

(

)

The display result is as follows: