Skip to content

Instantly share code, notes, and snippets.

@hashABCD
Created July 29, 2021 12:40
Show Gist options
  • Save hashABCD/58f7c94bc5b22c0bd3e1192a7690e1d1 to your computer and use it in GitHub Desktop.
Save hashABCD/58f7c94bc5b22c0bd3e1192a7690e1d1 to your computer and use it in GitHub Desktop.
Function to convert image to sketch
def img2sketch(photo, k_size):
#Read Image
img=cv2.imread(photo)
# Convert to Grey Image
grey_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Invert Image
invert_img=cv2.bitwise_not(grey_img)
#invert_img=255-grey_img
# Blur image
blur_img=cv2.GaussianBlur(invert_img, (k_size,k_size),0)
# Invert Blurred Image
invblur_img=cv2.bitwise_not(blur_img)
#invblur_img=255-blur_img
# Sketch Image
sketch_img=cv2.divide(grey_img,invblur_img, scale=256.0)
# Save Sketch
cv2.imwrite('sketch.png', sketch_img)
# Display sketch
cv2.imshow('sketch image',sketch_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Function call
img2sketch(photo='image.png', k_size=7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment