Created
July 29, 2021 12:40
Function to convert image to sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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