欢迎访问宙启技术站
智能推送

Python中实现图像处理的函数

发布时间:2023-05-27 07:40:05

Python是一个高级编程语言,其在图像处理领域也有着广泛的应用。Python中提供了许多工具和库来进行图像处理,包括Pillow、OpenCV、scikit-image等。本文介绍了一些常用的Python函数,用于实现图像处理。

一、图像加载和保存函数

1. 加载图片:使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg')

其中,'path/to/image.jpg'为待加载的图片路径。

2. 保存图片:同样使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg')
img.save('path/to/output.png')

其中,'path/to/output.png'为保存图片的路径和文件名。

二、图像缩放函数

1. 使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg')
img_resized = img.resize((new_width, new_height))
img_resized.save('path/to/output.png')

其中,new_width为缩放后的宽度,new_height为缩放后的高度。

2. 使用OpenCV库中的resize函数进行操作。

import cv2

img = cv2.imread('path/to/image.jpg')
img_resized = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
cv2.imwrite('path/to/output.png', img_resized)

三、图像旋转函数

1. 使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg')
img_rotated = img.rotate(angle)
img_rotated.save('path/to/output.png')

其中,angle为旋转的角度。

2. 使用OpenCV库中的getRotationMatrix2D和warpAffine函数进行操作。

import cv2
import numpy as np

img = cv2.imread('path/to/image.jpg')
(h, w) = img.shape[:2]
(center_x, center_y) = (w // 2, h // 2)
M = cv2.getRotationMatrix2D((center_x, center_y), angle, 1.0)
img_rotated = cv2.warpAffine(img, M, (w, h))
cv2.imwrite('path/to/output.png', img_rotated)

四、图像平移函数

1. 使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg')
img_translated = img.transform(img.size, Image.AFFINE, (1, 0, dx, 0, 1, dy))
img_translated.save('path/to/output.png')

其中,dx为水平方向上的位移,dy为垂直方向上的位移。

2. 使用OpenCV库中的warpAffine函数进行操作。

import cv2
import numpy as np

img = cv2.imread('path/to/image.jpg')
(h, w) = img.shape[:2]
M = np.float32([[1, 0, dx], [0, 1, dy]])
img_translated = cv2.warpAffine(img, M, (w, h))
cv2.imwrite('path/to/output.png', img_translated)

其中,dx为水平方向上的位移,dy为垂直方向上的位移。

五、图像翻转函数

1. 使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg')
img_flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
img_flipped.save('path/to/output.png')

其中,FLIP_LEFT_RIGHT表示水平翻转。

2. 使用OpenCV库中的flip函数进行操作。

import cv2

img = cv2.imread('path/to/image.jpg')
img_flipped = cv2.flip(img, 1)
cv2.imwrite('path/to/output.png', img_flipped)

其中,1表示水平翻转。

六、图像滤波函数

1. 使用scikit-image库中的filters模块进行操作。

from skimage import io, filters

img = io.imread('path/to/image.jpg', as_gray=True)
img_filtered = filters.gaussian(img, sigma=1.0)
io.imsave('path/to/output.png', img_filtered)

其中,sigma为高斯核的标准差。

2. 使用OpenCV库中的filter2D函数进行操作。

import cv2
import numpy as np

img = cv2.imread('path/to/image.jpg')
kernel = np.ones((kernel_size, kernel_size), np.float32) / (kernel_size**2)
img_filtered = cv2.filter2D(img, -1, kernel)
cv2.imwrite('path/to/output.png', img_filtered)

其中,kernel_size为卷积核的大小。

七、图像灰度化函数

1. 使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg').convert('L')
img.save('path/to/output.png')

2. 使用OpenCV库中的cvtColor函数进行操作。

import cv2

img = cv2.imread('path/to/image.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('path/to/output.png', img_gray)

八、图像二值化函数

1. 使用Pillow库中的Image模块进行操作。

from PIL import Image

img = Image.open('path/to/image.jpg').convert('L')
img_thresh = img.point(lambda x: 255*(x > threshold))
img_thresh.save('path/to/output.png')

其中,threshold为二值化的阈值。

2. 使用OpenCV库中的threshold函数进行操作。

import cv2

img = cv2.imread('path/to/image.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, img_thresh) = cv2.threshold(img_gray, threshold, 255, cv2.THRESH_BINARY)
cv2.imwrite('path/to/output.png', img_thresh)

其中,threshold为二值化的阈值。

以上是Python中实现图像处理的常用函数,读者可以根据需求选择相应的函数进行操作。