Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib reads jpg into int8 and png into normalized float

Why do Matplotlib load .png into float32 (from 0 to 1):

img = mpimg.imread('some.png')
print(img.dtype)
>>> float32

and .jpg to int8 (from 0 to 255):

img = mpimg.imread('some.jpg')
print(img.dtype)
>>> int8

Why? On the basis of what considerations is it realized this way?

like image 228
Maximus Avatar asked Sep 02 '17 12:09

Maximus


People also ask

How do I read an image in Python matplotlib?

The imread() function in pyplot module of matplotlib library is used to read an image from a file into an array. Parameters: This method accepts the following parameters. fname : This parameter is the image file to read. format: This parameter is the image file format assumed for reading the data.

How do I show multiple images in matplotlib?

MatPlotLib with PythonCreate random data using numpy. Add a subplot to the current figure, nrows=1, ncols=4 and at index=1. Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Blues_r". Add a subplot to the current figure, nrows=1, ncols=4 and at index=2.

What is matplotlib in image processing?

The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .

How do I show grayscale image in matplotlib?

Displaying Grayscale image Now open the image using PIL image method and convert it to L mode If you have an L mode image, that means it is a single-channel image – normally interpreted as grayscale. It only stores a grayscale, not color. Plotting the image as cmap = 'gray' converts the colors.


1 Answers

As the imread documenation states:

matplotlib can only read PNGs natively, but if PIL is installed, it will use it to load the image and return an array (if possible) [...]

So for png images, matplotlib has its own code to read the image, while for jpg it relies on PIL.

import matplotlib.pyplot as plt

im = plt.imread("house.png")
print im.dtype               # float32

im = plt.imread("house.jpg")
print im.dtype               # uint8

The internal _png module which is responsible for loading the png image, has two functions, read_png_float and read_png_int. While by default read_png_float is used, you may as well manually use read_png_int to obtain an integer image.

import matplotlib._png as png

im = png.read_png_float("house.png")
print im.dtype              # float32

im = png.read_png_int("house.png")
print im.dtype              # uint8

Finally, for a consistent behaviour you may use PIL for both, png and jpg images instead of relying on matplotlib.

from PIL import Image
import numpy as np

im = np.asarray(Image.open("house.png"))
print im.dtype              # uint8

im = np.asarray(Image.open("house.jpg"))
print im.dtype              # uint8
like image 77
ImportanceOfBeingErnest Avatar answered Oct 21 '22 19:10

ImportanceOfBeingErnest