Home >Backend Development >Python Tutorial >How to Gaussian blur an image using Python
How to use Python to perform Gaussian blur on pictures
Introduction:
Gaussian blur is a commonly used image processing technique that can make images smoother and blur, used to reduce noise or add certain artistic effects. In this article, we will use Python to write code to implement Gaussian blur on images.
import cv2 import numpy as np
image = cv2.imread('image.jpg') blur_radius = 10
GaussianBlur()
function in OpenCV, we can perform Gaussian blur processing on the image. The parameters of this function include the original image, blur radius, and standard deviation (used to calculate the weights of the Gaussian function). The returned result is the blurred image. blurred_image = cv2.GaussianBlur(image, (blur_radius, blur_radius), 0)
imshow()
function. You can run the following code to implement this step and see the final result. cv2.imwrite('blurred_image.jpg', blurred_image) cv2.imshow('Blurred Image', blurred_image) cv2.waitKey(0) cv2.destroyAllWindows()
The complete code is as follows:
import cv2 import numpy as np # 加载图片和设置模糊效果参数 image = cv2.imread('image.jpg') blur_radius = 10 # 进行高斯模糊处理 blurred_image = cv2.GaussianBlur(image, (blur_radius, blur_radius), 0) # 保存并显示结果 cv2.imwrite('blurred_image.jpg', blurred_image) cv2.imshow('Blurred Image', blurred_image) cv2.waitKey(0) cv2.destroyAllWindows()
Summary:
By using Python and OpenCV, we can easily achieve the effect of Gaussian blur on images. By adjusting the blur radius, we can freely control the degree of blur. Gaussian blur can not only be used to reduce noise in images, but can also create some beautiful artistic effects. If you're interested in image processing, this is an interesting and practical technique worth checking out.
The above is the detailed content of How to Gaussian blur an image using Python. For more information, please follow other related articles on the PHP Chinese website!