Make Images Blurry and Convert to Grayscale
Learn to make images blurry and grayscale.
We'll cover the following
You might’ve blurred photos or converted them to grayscale before with camera features or photo editing tools. These effects are commonplace and used to make images more suitable for a number of contexts. For example, flashbacks are often in grayscale. The OpenCV library provides us with functions to make images blurry or convert them to grayscale.
Make the image blurry
To make images blurry, we use the cv2.GaussianBlur() method of the OpenCV library. This method requires three parameters—img
, kernelSize
, and sigmaX
. These represent the image, its kernel size, and its sigma X, respectively.
Note:
sigmaX
represents the kernel’s standard deviation in the X-direction.
kernelSize = (7,7)
sigmaX = 0
blurredImg = cv2.GaussianBlur(img,kernelSize,sigmaX)
Note: We use the
imread
function of thecv2
library to read the image asimg
.
Here, kernelSize
is the size of the window where the blur effect is applied. It should be in odd numbers. A higher number makes the image blurrier.
sigmaX
represents the kernel’s standard deviation in the X-direction.
Make the image grayscale
To make the image grayscale, we use the cv2.cvtColor()
method of the OpenCV library. This method requires two parameters—the color code, and img
, which represent the image.
grayImg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
Here, the color code is cv2.COLOR_BGR2GRAY
, which converts the image from the BGR
color code to the GRAY
color code.
Display the blurred and grayscale images
We use the following code to display the blurred and grayscale images:
cv2.imshow("Main",img)
cv2.imshow("BlurredImg",blurredImg)
cv2.imshow("GrayImg",grayImg)
The following piece of code explains the overall flow of code and displays the images.
Get hands-on with 1200+ tech skills courses.