How to convert image into sketch using python

0

 



First Install opencv-python module
pip install opencv-python
SOURCE CODE HERE 👇
import cv2

def convert_to_sketch(image_path, output_path):
    # Read the image
    image = cv2.imread(image_path)

    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Invert the grayscale image
    inverted_image = cv2.bitwise_not(gray_image)

    # Blur the inverted image
    blurred_image = cv2.GaussianBlur(inverted_image, (111, 111), 0)

    # Invert the blurred image
    inverted_blurred_image = cv2.bitwise_not(blurred_image)

    # Create the sketch by blending the grayscale image with the inverted blurred image
    sketch = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)

    # Save the sketch as an image file
    cv2.imwrite(output_path, sketch)
    print("Sketch saved successfully.")

# Specify the path to your image
image_path = "/home/tdyno/myWorks/supra_mk4.webp"
# Specify the output path for the sketch
output_path = "/home/tdyno/myWorks/sketch.jpg"

# Convert the image to a sketch and save it
convert_to_sketch(image_path, output_path)
Tags

Post a Comment

0Comments

If you have any doubts, please let me know

Post a Comment (0)