forked from powerexploit/Awesome-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartoonify.py
37 lines (32 loc) · 1.07 KB
/
Cartoonify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Importing Libraries
import cv2
import numpy as np
from skimage import io
# Class Defination
class Cartoon:
def __init__(self):
img = io.imread("images/sunset.jpg")
# 1) Edges Image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 9, 7)
# 2) Color Image
color = cv2.bilateralFilter(img, 10, 300, 300)
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 3) Cartoon Image
cartoon = cv2.bitwise_and(color, color, mask=edges)
cartoon_img = cv2.cvtColor(cartoon, cv2.COLOR_BGR2RGB)
# Re-sizeing Image
resize = cv2.resize(RGB_img, (600, 450))
resize2 = cv2.resize(cartoon_img, (600, 450))
self.resize = resize
self.resize2 = resize2
# Displaying Image
# Creating an object of class Cartoon
c1 = Cartoon()
c1.resize
c1.resize2
cv2.imshow("Original_Image", c1.resize)
cv2.imshow("Cartoonified_Image", c1.resize2)
cv2.waitKey(0)
cv2.destroyAllWindows()