Python各种图像库的图像读写方式的简单介绍(附代码)


本文摘自php中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于Python各种图像库的图像读写方式的简单介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

目前主流的图像库有几下几种:

1. OpenCV 2. PIL(Pillow) 3. matplotlib.image 4. skimage 5. scipy.misc

结论:以上图片库中当属OpenCV最为强大,成熟。

1.1 OpenCV 图像的读取与储存

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import cv2

#读取图像 直接是numpy矩阵格式

img = cv2.imread('horse.jpg',1) # 0表示读入灰色图片,1表示读入彩色图片

cv2.imshow('image',img) # 显示图像

print(img.shape)   # (height,width,channel)

print(img.size)    # 像素数量

print(img.dtype)   # 数据类型

print(img)         # 打印图像的numpy数组,3纬数组

 

#储存图像

# 当前目录储存

cv2.write(‘horse1.jpg',img)

# 自定义储存

cv2.write(‘/path_name/’ + str(image_name) + '.jpg',img)

           

cv2.waitKey()

1.2OpenCV 图像灰化处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import cv2

#方法一

img = cv2.imread('horse.jpg',0) # 0表示读入灰色图片,或者使用cv2.IMREAD_GRATSCALE 替代0

cv2.imshow('gray image',img)

 

#方法二

img = cv2.imread('horse.jpg')

gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

cv2.imshow('gray image',gray_img)

 

print(gray_img.shape) # (height, width)

print(gray_img.size)  # 像素数量

print(gray_img)       # 打印图像的numpy数组,2维

cv2.waitKey()

1.3 OpenCV 矩阵格式变换

Why?:OpenCV的矩阵格式 (height, width, channels) -->> 深度学习矩阵类型可能是 (channels,height,width)

1

2

3

4

5

6

7

8

import cv2

import numpy as np

img = cv2.imread('horse.jpg',1)

cv2.imshow('image',img)

# 矩阵格式的变换

print(img.shape)

img = img.transpose(2,0,1) #变换函数

print(img.shape)

1

2

3

4

# 矩阵扩展 (batch_size, channels, height, width) 预测单张图片的操作

# 加一列作为图片的个数

img = np.expand_dims(img, axis=0) #使用numpy函数

print(img.shape)

1

2

3

4

5

# 训练阶段构建batchdata_lst = []

loop:

    img = cv2.imread('xxx.jpg')

    data_lst.append(img)

data_arr = np.array(data_lst)

1.4 OpenCV 图片归一化 (Data Normalization)

1

2

3

4

5

6

import cv2

# 为了减少计算量,需要把像素值0-255转换到0-1之间

img = cv2.imread('horse.jpg')

img = img.astype('float') / 255.0 # 先转化数据类型为float

print(img.dtype)

print(img)

1.5 OpenCV BRG转换为RGB

1

2

3

4

import cv2

img = cv2.imread('horse.jpg')

img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 转为RGB format

print(img)

1.6 OpenCV 访问像素点

1

2

3

4

5

import cv2

img = cv2.imread('horse.jpg')

gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 转为Gray image

print(img[4,4])        # 3 channels

print(gray_img[4,4])   # 1 channel

1.7 OpenCV 感兴趣区域剪切(ROI)

1

2

3

4

5

6

7

import cv2

img = cv2.imread('horse.jpg')

print(img.shape)

roi = img[0:437,0:400] # [y:height,x:width]

 

cv2.imshow('roi',roi)

cv2.waitKey()

2.1 PIL 图像读取与储存

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

from PIL import Image

import numpy as np

#图像读取

img = Image.open('horse.jpg')

print(img.format) # 图片格式

print(img.size)   # (width,height)

print(img.mode)   # 图片通道类型

 

#将图像转化为矩阵格式

arr = np.array(img)

print(arr.shape)

print(arr.dtype)

 

#图像储存

new_img = Image.fromarray(arr)

new_img.save('test.jpg')

 

img.show()

2.2 PIL 图像灰化处理

1

2

3

4

5

6

7

#图像灰化处理

gray = Image.open('horse.jpg').convert('L')

gray_arr = np.array(gray)

print(gray_arr.shape) # (height,width)

print(gray_arr.dtype)

print(gray_arr)

gray.show()

2.3 PIL 感兴趣区域剪切

1

2

3

4

# 感兴趣区域剪切

img = Image.open('horse.jpg')

roi = img.crop((0,0,200,200)) # (左上x,左上y,右下x,右下y)

roi.show()

2.4 通道操作

1

2

3

4

# 通道处理

r,g,b = img.split() #分离

img = Image.merge("RGB",(b,g,r)) #合并

img = img.copy() #复制

3.1 Matplotlib 读取和存储图片

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import matplotlib.pyplot as plt

import numpy as np

# 图像读取为numpy数组格式

img = plt.imread('horse.jpg')

 

plt.axis('off') # 关闭刻度显示

 

print(img.shape) # (height, width, channel)

print(img.size) # 像素数量

print(img.dtype)

 

#储存图片

plt.savefig('./name.jpg')

 

figure = plt.figure(figsize=(20,10)) # 调整显示图片的大小

 

plt.imshow(img)

plt.show()

3.2 Matplotlib 图片灰化处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#图片灰化处理

# 平均值发

img_mean = img.mean(axis=2)

plt.imshow(img_mean,cmap='gray')

plt.show()

 

#最大值法

img_max = img.max(axis=-1)

plt.imshow(img_max,cmap='gray')

plt.show()

 

#RGB三原色法

gravity = np.array([0.299,0.587,0.114])

img_gravity = np.dot(img,gravity)

plt.imshow(img_gravity,cmap="gray")

plt.show()

4.1 skimage 读取和储存图像

1

2

3

4

5

6

7

8

9

10

11

from skimage import io

#读取图像numpy数组格式

img = io.imread('horse.jpg')

print(img.shape)

print(img.dtype)

print(img.size)

#print(img)

io.imshow(img)

 

#储存图像

io.imsave('test.jpg',img)

4.2 skimage 灰化处理

1

2

3

4

5

6

7

8

#图像灰化处理并归一化

img = io.imread('horse.jpg',as_gray=True)

print(img.shape)

print(img.dtype) # 数据类型位float

print(img.size)

print(img)

io.imshow(img)

io.show()

5.1 scipy.misc 读取和储存图像

1

2

3

4

5

6

7

8

9

10

11

12

13

#在1.2.0 之后统一用imageio模块

import imageio

import matplotlib.pyplot as plt

#读取图片为numpy数组

img = imageio.imread('horse.jpg')

print(img.dtype)

print(img.size)  # 像素数量

print(img.shape) #(height, width, channels)

plt.imshow(img)

plt.show()

print(img)

#储存图片

imageio.imsave('test.jpg',img)

以上就是Python各种图像库的图像读写方式的简单介绍(附代码)的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python编程怎么学

Python中的value是什么

Python模块介绍

Python怎么读取文件夹

如何在Python中添加自定义模块的方法介绍

Python函数与方法的区别

Python与access选哪个

Python语言能做什么工作

Python类对象实例对象的区别

在cmd中Python如何卸载模块

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...