In this article, we are Going to Show you How to convert Normal Images to Sketch art images using python Module opencv-python. We'll write the code step by step, with explanations along the way.
Before Begining the Steps Let's look at The Module OpenCV
What is OpenCV?
OpenCV is a programming library geared mostly at real-time computer vision and machine learning software library
We can Install OpenCV to Our Project using the command
pip install opencv-python
# Step- 1 Let's Import OpenCV to our Project using the Following Code
import cv2
# Step-2 Now let's Find an Image to convert to Sketch Art. We are Going to Choose Car image to convert to Sketch art Image
This is the Image We are Going to Convert to Sketch art. Now Let's Read the Image
# Step - 3 Reading the Image using the Following Command
import cv2
image = cv2.imread("car.jpg")
Let's now convert the image to grayscale using the Following command
# Step - 4 Converting the Image to grayscale
import cv2
image = cv2.imread("car.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Now This will be our inverted grayscale image. Invert the grayscale image, commonly known as the negative image. Inversion is a technique for enhancing details.
# Step - 5 Inverting the grayscale Image
import cv2
image = cv2.imread("car.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted_image = 255 - gray_image
Finally, combine the grayscale image with the inverted blurry image to make the pencil sketch. This is accomplished by dividing the grayscale image by the fuzzy image reversed.
# Step - 6 Creating the Pencil Sketch Image Using the Following Command
blurred = cv2.GaussianBlur(inverted_image, (21, 21), 0)
inverted_blurred = 255 - blurred
sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
# Step - 7 Displaying the Image using OpenCV using the Following Command
cv2.imshow("Original Image", image)
cv2.imshow("Pencil Sketch of Car", sketch)
cv2.waitKey(0)
Now Let's Combine All our Code
import cv2
image = cv2.imread("car.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverted_image = 255 - gray_image
blurred = cv2.GaussianBlur(inverted_image, (21, 21), 0)
inverted_blurred = 255 - blurred
sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
cv2.imshow("Original Image", image)
cv2.imshow("Pencil Sketch of Car", sketch)
cv2.waitKey(0)
Now Let's Look at the Output Of the Program