Skip to content

Commit 78cfa03

Browse files
committed
add cartoonify images tutorial
1 parent 325dacf commit 78cfa03

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9494
- [How to Edit Images using InstructPix2Pix in Python](https://www.thepythoncode.com/article/edit-images-using-instruct-pix2pix-with-huggingface). ([code](machine-learning/edit-images-instruct-pix2pix))
9595
- [How to Upscale Images using Stable Diffusion in Python](https://www.thepythoncode.com/article/upscale-images-using-stable-diffusion-x4-upscaler-huggingface). ([code](machine-learning/stable-diffusion-upscaler))
9696
- [Real-Time Vehicle Detection, Tracking and Counting in Python](https://thepythoncode.com/article/real-time-vehicle-tracking-and-counting-with-yolov8-opencv). ([code](https://github.com/python-dontrepeatyourself/Real-Time-Vehicle-Detection-Tracking-and-Counting-in-Python/))
97+
- [How to Cartoonify Images in Python](https://thepythoncode.com/article/make-a-cartoonifier-with-opencv-in-python). ([code](machine-learning/cartoonify-images))
9798
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
9899
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
99100
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Cartoonify Images in Python](https://thepythoncode.com/article/make-a-cartoonifier-with-opencv-in-python)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import cv2, argparse, sys
2+
3+
4+
# In this function, we accept an image and convert it to a cartoon form.
5+
def cartoonizer(image_name):
6+
# Load the image to cartoonize.
7+
image_to_animate = cv2.imread(image_name)
8+
9+
# Apply a bilateral filter to smoothen the image while preserving edges.
10+
smoothened_image = cv2.bilateralFilter(image_to_animate, d=9, sigmaColor=75, sigmaSpace=75)
11+
12+
# Convert image to gray and create an edge mask using adaptive thresholding.
13+
gray_image = cv2.cvtColor(smoothened_image, cv2.COLOR_BGR2GRAY)
14+
edges = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
15+
16+
# Combine the smoothened image and the edge mask to create a cartoon-like effect.
17+
to_cartoon = cv2.bitwise_and(smoothened_image, smoothened_image, mask=edges)
18+
19+
# Save the cartoon image in our current directory. A new Image would be generated in your current directory.
20+
cartooned_image = f"cartooned_{image_name}"
21+
cv2.imwrite(cartooned_image, to_cartoon)
22+
23+
# Display the result.
24+
cv2.imshow("Cartooned Image", to_cartoon)
25+
cv2.waitKey(0)
26+
cv2.destroyAllWindows()
27+
28+
29+
# In this function, we accept user's argument from the terminal. -i or --image to specify the image.
30+
def get_image_argument():
31+
parser = argparse.ArgumentParser(description="Please specify an image to 'cartoonify'.")
32+
parser.add_argument('-i', '--image', help="Please use -h or --help to see usage.", dest='image')
33+
argument = parser.parse_args()
34+
35+
if not argument.image:
36+
print("[-] Please specify an image. Use --help to see usage.")
37+
sys.exit() # Exit the program
38+
39+
return argument
40+
41+
42+
# We get the user's input (image) from the terminal and pass it into cartoonizer function.
43+
image_args = get_image_argument()
44+
cartoonizer(image_args.image)
56.9 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python

0 commit comments

Comments
 (0)