Python digital image processing (2): image reading, display and saving
Reprinted from https://www.cnblogs.com/denny402/p/5121897.htmlskimage 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:
1
|
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 one 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).
from skimage import io img=io.imread('d:/dog.jpg') io.imshow(img)
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
from skimage import io img=io.imread('d:/dog.jpg',as_grey=True) io.imshow(img)
Second, the program comes with pictures
The skimage program comes with some sample images. If we don't want to read the images from the outside, we can use these sample images directly:
astronaut |
astronaut pictures | coffee |
cup of coffee | lena |
Lena beauty picture |
camera |
man holding camera | coins |
coin picture | moon |
moon pictures |
checkerboard |
chessboard pictures | horse |
horse pictures | page |
book page pictures |
chelsea |
kitten pictures | hubble_deep_field |
starry sky | text |
text picture |
clock |
clock pictures | immunohistochemistry |
Colon picture |
|
To display these images, the following code can be used without any parameters
from skimage import io,data img=data.lena() 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 as: D:\Anaconda3\lib\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.lena() #读取lean图片 img2=io.imread(data_dir+'/lena.png') #读取lena图片
三、保存图片
使用io模块的imsave(fname,arr)函数来实现。第一个参数表示保存的路径和名称,第二个参数表示需要保存的数组变量。
from skimage import io,data img=data.chelsea() io.imshow(img) io.imsave('d:/cat.jpg',img)
保存图片的同时也起到了转换格式的作用。如果读取时图片格式为jpg图片,保存为png格式,则将图片从jpg图片转换为png图片并保存。
四、图片信息
如果我们想知道一些图片信息,可以在spyder编辑器的右上角显示:
也可以直接以程序方式打印输出
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()) #像素平均值
结果输出:
<class 'numpy.ndarray'>
(300, 451, 3)
300
451
3
405900
231
0
115.305141661