python -- digital image processing

1. Environment installation and
configuration

2. Image reading, display and saving 1. Read the picture
from the outside and display it


skimage provides the io module. As the name suggests, this module is used for image input and output operations. For the convenience of practice, a data module is also provided, with some sample images nested in it, which we can use directly.

The introduction of the skimage module is available:

from skimage import io

1. Read the picture from the outside and display it

To read a single color rgb image, use the skimage.io.imread(fname) function with a parameter indicating the file path to be read.
To display the image , use the skimage.io.imshow(arr) function with one parameter, indicating the arr array to be displayed (the read image is calculated in the form of a numpy array).
Read a single grayscale image , use the skimage.io.imread(fname, as_grey=True) function, the first parameter is the image path, the second parameter is as_grey, bool value, the default is False

2. The program comes with pictures
The skimage program comes with some sample pictures. If we don't want to read the pictures from the outside, we can use these example pictures directly: To
write picture description here
display these pictures, the following code can be used without any parameters

from skimage import io,data
img=data.astronaut()
io.imshow(img)

The image name corresponds to the function name. For example, the function name corresponding to the camera image is camera(). These sample images are stored under the installation directory of skimage, and the path name is data_dir. We can print this path to see:

from skimage import data_dir
print(data_dir)

Shows up as: /home/mwp/anaconda3/lib/python3.5/site-packages/skimage/data

That is to say, the following two lines of code for reading pictures have the same effect:

from skimage import data_dir,data,io
img1=data.astronaut()  #读取lean图片
img2=io.imread(data_dir+'/astronaut.png')  #读取lena图片

3. Save the picture
Use the imsave (fname, arr) function of the io module to implement. The first parameter indicates the saved path and name, and the second parameter indicates the array variable to be saved.

from skimage import io,data
img=data.chelsea()
io.imshow(img)
io.imsave('/home/mwp/cat.jpg',img)

It also plays a role in converting the format while saving the image. If the image format is jpg when read and saved as png, convert the image from jpg to png and save it.

4. Picture information
If we want to know some picture information, it can be displayed in the upper right corner of the spyder editor:

You can also print the output directly programmatically:

from skimage import io,data
img=data.chelsea()
io.imshow(img)
print(type(img))  #显示类型
print(img.shape)  #显示尺寸
print(img.shape[0])  #图片宽度
print(img.shape[1])  #图片高度
print(img.shape[2])  #图片通道数
print(img.size)   #显示总像素个数
print(img.max())  #最大像素值
print(img.min())  #最小像素值
print(img.mean()) #像素平均值

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520187&siteId=291194637