Created
December 25, 2020 06:08
-
-
Save tinshade/49262f7b9093192d145e0e7fb5cd0fe2 to your computer and use it in GitHub Desktop.
This program generates a "cartoonized" image of the given input image!
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
import cv2 #pip install opencv-python | |
#This program generates a cartoonized image of the given image! | |
img = cv2.imread("image.jpg") | |
def color_quantization(img, k): | |
data = np.float32(img).reshape((-1, 3)) | |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0) | |
ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) | |
center = np.uint8(center) | |
result = center[label.flatten()] | |
result = result.reshape(img.shape) | |
return result | |
img_1 = color_quantization(img, 7) | |
blurred = cv2.medianBlur(img_1, 3) | |
while True: | |
cv2.imshow("blurred", blurred) | |
cv2.imwrite("blurred.png",blurred) #Writes in the same directory as the script. | |
if cv2.waitKey(1) == 0x1b: # ESC | |
print('ESC pressed. Exiting ...') | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment