0% found this document useful (0 votes)
14 views2 pages

Cartoon Effect

The document outlines a step-by-step guide to cartoonify an image using Python packages such as OpenCV, EasyGUI, and Matplotlib. It includes code snippets for importing necessary libraries, uploading an image, processing it through various transformations, and applying cartoon effects. The final output visualizes the transition through subplots of the processed images.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Cartoon Effect

The document outlines a step-by-step guide to cartoonify an image using Python packages such as OpenCV, EasyGUI, and Matplotlib. It includes code snippets for importing necessary libraries, uploading an image, processing it through various transformations, and applying cartoon effects. The final output visualizes the transition through subplots of the processed images.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cartoonify The Image

Packages to study.

• opencv for processing an image


• easygui to browse image
• numpy to manage image numpy array
• imageio to read image file from easygui
• matplotlib to visualize the results
• os to work at os level

1. Import basic packages

import cv2 #for image processing


import easygui #to open the filebox
import numpy as np #to store image
import imageio #to read image stored at particular path
import sys
import matplotlib.pyplot as plt
import os
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image

2. Create window explorer with easygui

""" fileopenbox opens the box to choose file


and help us store file path as string """
def upload():
ImagePath=easygui.fileopenbox()
cartoonify(ImagePath)

3. Read cv2 image

def cartoonify(ImagePath):
# read the image
originalmage = cv2.imread(ImagePath)
originalmage = cv2.cvtColor(originalmage, cv2.COLOR_BGR2RGB)
#print(image) # image is stored in form of numbers

# confirm that image is chosen


if originalmage is None:
print("Can not find any image. Choose appropriate file")
sys.exit()

ReSized1 = cv2.resize(originalmage, (960, 540))


#plt.imshow(ReSized1, cmap='gray')

4. Image Transformation

#converting an image to grayscale


grayScaleImage = cv2.cvtColor(originalmage, cv2.COLOR_BGR2GRAY)
ReSized2 = cv2.resize(grayScaleImage, (960, 540))
#plt.imshow(ReSized2, cmap='gray')
5. Edge Smoothing

#applying median blur to smoothen an image


smoothGrayScale = cv2.medianBlur(grayScaleImage, 5)
ReSized3 = cv2.resize(smoothGrayScale, (960, 540))
#plt.imshow(ReSized3, cmap='gray')

6. Determining Image Edges

#retrieving the edges for cartoon effect


#by using thresholding technique
getEdge = cv2.adaptiveThreshold(smoothGrayScale, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 9, 9)

ReSized4 = cv2.resize(getEdge, (960, 540))


#plt.imshow(ReSized4, cmap='gray')

There are two techniques in cartoon effect.


1. Highlighted edges
2. smooth colors
we are doing 1st one by using adaptive thresholding technique

threshold value is the mean of the neighborhood pixel values area minus the
constant C. C is the constant subtracted from the weighted sum of
neighborhood pixels.

7. Make a mask

#applying bilateral filter to remove noise


#and keep edge sharp as required
colorImage = cv2.bilateralFilter(originalmage, 9, 300, 300)
ReSized5 = cv2.resize(colorImage, (960, 540))
#plt.imshow(ReSized5, cmap='gray')

8. Giving Cartoonic effect

#masking edged image with our "BEAUTIFY" image


cartoonImage = cv2.bitwise_and(colorImage, colorImage, mask=getEdge)

ReSized6 = cv2.resize(cartoonImage, (960, 540))


#plt.imshow(ReSized6, cmap='gray')

9. Sub Plotting
# Plotting the whole transition
images=[ReSized1, ReSized2, ReSized3, ReSized4, ReSized5, ReSized6]
fig, axes = plt.subplots(3,2, figsize=(8,8), subplot_kw={'xticks':[], 'yticks':[]}, gridspec_kw=dict(hspace=0.1,
wspace=0.1))
for i, ax in enumerate(axes.flat):
ax.imshow(images[i], cmap='gray')
//save button code
plt.show()

You might also like