程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python digital image processing skimage reading, displaying and saving pictures

編輯:Python

Catalog

introduction

One 、 Read the picture from the outside and display

Two 、 The program comes with pictures

3、 ... and 、 Save the picture

Four 、 Image information

introduction

skimage 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 pictures

coffee

A cup of coffee

lena

lena Beauty pictures

camera

Picture of the person with the camera

coins

Coin picture

moon

Moon picture

checkerboard

Chessboard picture

horse

Horse pictures

page

Page pictures

chelsea

Kitten picture

hubble_deep_field

Sky picture

text

Text image

clock

Clock picture

immunohistochemistry

Picture of colon  

The 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 information

If 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 !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved