Python 3 OpenCV Script to Cartoonify an Image or Add Special Cartoon Effects to Image Files Full Project For Beginners

Python 3 OpenCV Script to Cartoonify an Image or Add Special Cartoon Effects to Image Files Full Project For Beginners

 

Welcome folks today in this post we will be cartoonify an image in python using opencv library in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install opencv-python

 

After installing this make an app.py file and copy paste the following code

 

app.py

 

 

import cv2
import os
from pathlib import Path



color_image = cv2.imread("profile.jpg")
##cv2.imshow("image_not_processed",color_image) ## Uncomment this to see the image without the process.
##cv2.waitKey()
##cv2.destroyAllWindows()

cartoon_style_selection = input("This script currently has 2 sytles. Please type 1 or 2.   ")

if (cartoon_style_selection == "1"):
    cartoon_image_style_1 = cv2.stylization(color_image, sigma_s=150, sigma_r=0.25) ## Cartoonify process. 
    cv2.imshow('cartoon_1', cartoon_image_style_1)
    cv2.waitKey()
    cv2.destroyAllWindows()
elif (cartoon_style_selection == "2"):
    cartoon_image_style_2  = cv2.stylization(color_image, sigma_s=60, sigma_r=0.5) ## Cartoonify process. 
    cv2.imshow('cartoon_2', cartoon_image_style_2)
    cv2.waitKey()
    cv2.destroyAllWindows()

else:
    print("Invalid style selection.")

 

See also  Python 3 PyQt5 Advanced Table With Rows & Columns Using QTableWidget GUI Desktop App Full Project For Beginners

 

Now in the above python script you need to replace the path of the image and then you can execute the python script by the following command

 

python app.py

 

 

Leave a Reply