Skip to content

Instantly share code, notes, and snippets.

@tinshade
Created December 25, 2020 06:08
Show Gist options
  • Save tinshade/49262f7b9093192d145e0e7fb5cd0fe2 to your computer and use it in GitHub Desktop.
Save tinshade/49262f7b9093192d145e0e7fb5cd0fe2 to your computer and use it in GitHub Desktop.
This program generates a "cartoonized" image of the given input image!
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