In this tutorial I will show you how to convert image to pencil sketch in python.
Read the input image and convert it to grayscale image and then invert it. Afterwards apply smoothing. create the sketch
PYTHON CODE:
import cv2 def create_sketch(input_image_path, output_sketch_path): try: image = cv2.imread(input_image_path) window_name = 'Original image' cv2.imshow(window_name,image) grey_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) invert = cv2.bitwise_not(grey_img) blur = cv2.GaussianBlur(invert, (21, 21), 0) invertedblur = cv2.bitwise_not(blur) sketch = cv2.divide(grey_img, invertedblur, scale=256.0) cv2.imwrite(output_sketch_path, sketch) window_name = 'Sketch image' cv2.imshow(window_name, sketch) cv2.waitKey(0) cv2.destroyAllWindows() except Exception as e: print(f"An error occurred: {e}") if __name__ == "__main__": input_image_path = 'angelina.jpg' output_sketch_path = 'sketch.png' create_sketch(input_image_path, output_sketch_path)
OUTPUT:
VIDEO GUIDE:
Post your comments / questions
Recent Article
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
Related Article