introduction
One 、 Read the picture from the outside and display
Two 、 The program comes with pictures
3、 ... and 、 Save the picture
Four 、 Image information
introductionskimage Provides io modular , seeing the name of a thing one thinks of its function , This module is used for image input and output operation . To facilitate practice , There is also a data modular , Some sample pictures are nested inside , We can use it directly .
introduce skimage Module available :
from skimage import io
One 、 Read the picture from the outside and display Read single color rgb picture , Use skimage.io.imread(fname) function , With one parameter , Indicates the file path to be read . Show pictures using skimage.io.imshow(arr) function , With one parameter , Indicates what needs to be displayed arr Array ( Read the picture to numpy Array form calculation ).
from skimage import ioimg=io.imread('d:/dog.jpg')io.imshow(img)
Read a single grayscale picture , Use skimage.io.imread(fname,as_grey=True) function , The first parameter is the image path , The second parameter is as_grey, bool Type value , The default is False
from skimage import ioimg=io.imread('d:/dog.jpg',as_grey=True)io.imshow(img)
Two 、 The program comes with pictures skimage The program comes with some sample pictures , If we don't want to read pictures from the outside , You can use these sample images directly :
astronaut
Astronaut picturescoffee
A cup of coffeelena
lena Beauty picturescamera
Picture of the person with the cameracoins
Coin picturemoon
Moon picturecheckerboard
Chessboard picturehorse
Horse picturespage
Page pictureschelsea
Kitten picturehubble_deep_field
Sky picturetext
Text imageclock
Clock pictureimmunohistochemistry
Picture of colonThe following code can be used to display these pictures , Without any parameters
from skimage import io,dataimg=data.lena()io.imshow(img)
The image name corresponds to the function name , Such as camera The function name corresponding to the picture is camera(). These sample pictures are stored in skimage Under the installation directory of , The path name is data_dir, We can print out this path to see :
from skimage import data_dirprint(data_dir)
Is shown as :D:\Anaconda3\lib\site-packages\skimage\data
in other words , The effect of the following two lines of code for reading images is the same :
from skimage import data_dir,data,ioimg1=data.lena() # Read lean picture img2=io.imread(data_dir+'/lena.png') # Read lena picture
3、 ... and 、 Save the picture Use io Modular imsave(fname,arr) Function to implement . The first parameter indicates the saved path and name , The second parameter represents the array variable to be saved .
from skimage import io,dataimg=data.chelsea()io.imshow(img)io.imsave('d:/cat.jpg',img)
While saving the picture, it also plays a role in converting the format . If the image format when reading is jpg picture , Save as png Format , Then change the picture from jpg Convert the picture to png Picture and save .
Four 、 Image informationIf we want to know some picture information , Can be in spyder The upper right corner of the editor shows :
It can also be printed out directly in the form of program
from skimage import io,dataimg=data.chelsea()io.imshow(img)print(type(img)) # Display type print(img.shape) # Display size print(img.shape[0]) # Image width print(img.shape[1]) # Picture height print(img.shape[2]) # Number of picture channels print(img.size) # Displays the total number of pixels print(img.max()) # Maximum pixel value print(img.min()) # Minimum pixel value print(img.mean()) # Pixel average
Results output :
<class 'numpy.ndarray'>
(300, 451, 3)
300
451
3
405900
231
0
115.305141661
That's all python digital image processing skimage Read the details of displaying and saving pictures , More about python skimage Please pay attention to other related articles of the software development network to read the information showing the saved pictures !