Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78cfa03

Browse files
committed
add cartoonify images tutorial
1 parent325dacf commit78cfa03

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
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).
Lines changed: 1 addition & 0 deletions
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)
134 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
importcv2,argparse,sys
2+
3+
4+
# In this function, we accept an image and convert it to a cartoon form.
5+
defcartoonizer(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+
defget_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+
ifnotargument.image:
36+
print("[-] Please specify an image. Use --help to see usage.")
37+
sys.exit()# Exit the program
38+
39+
returnargument
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp