Ever wanted to see yourself as a cartoon character? Maybe something out of a comic book or Pixar movie? With just a few lines of Python code and a splash of creativity, you can transform any photo into a cartoon using OpenCV and Streamlit.
This post walks you through a step-by-step guide to build your own image cartoonifier โ a fun and practical intro to computer vision!
๐ก What You’ll Learn
By the end of this tutorial, you’ll know how to:
- Use OpenCV to process and transform images
- Build a cartoonifier using edge detection and color smoothing
- Create an interactive web app with Streamlit to upload and display images
- Customize filters and take this project further!
๐ ๏ธ Tools Weโll Use
| Library | Purpose |
|---|---|
OpenCV | For image processing (grayscale, blurring, edge detection) |
NumPy | For array manipulation |
PIL | For reading uploaded images |
Streamlit | To build an interactive web UI |
Make sure you have them installed:
pip install opencv-python streamlit numpy pillow
๐ง Step 1: Set Up Your Python File
Create a new Python file called cartoonifier.py. Weโll start by importing the necessary libraries:
import streamlit as st
import cv2
import numpy as np
from PIL import Image
๐จ Step 2: Define the Cartoonify Function
This function will do all the heavy lifting โ from edge detection to smoothing.
def cartoonify_image(image):
# 1. Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 2. Apply median blur
gray_blur = cv2.medianBlur(gray, 5)
# 3. Detect edges using adaptive thresholding
edges = cv2.adaptiveThreshold(
gray_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 9, 9
)
# 4. Apply bilateral filter to smoothen colors
color = cv2.bilateralFilter(image, 9, 300, 300)
# 5. Combine edges and color image
cartoon = cv2.bitwise_and(color, color, mask=edges)
return cartoon
๐ก How It Works:
| Step | Technique | Purpose |
|---|---|---|
| 1 | Grayscale Conversion | Simplifies the image |
| 2 | Median Blur | Reduces noise and preserves edges |
| 3 | Adaptive Thresholding | Finds sharp outlines |
| 4 | Bilateral Filter | Smooths color while keeping edges crisp |
| 5 | Bitwise AND | Combines cartoon edges with smooth color |
๐ผ๏ธ Step 3: Create the Streamlit UI
Letโs create a simple and interactive web UI using Streamlit.
def main():
st.title("๐ผ๏ธ Cartoonify an Image with Python!")
st.write("Upload a photo and turn it into a cartoon in seconds.")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Open and convert image
image = Image.open(uploaded_file)
image_np = np.array(image)
# Convert RGB to BGR for OpenCV
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
# Process the image
cartoon_cv = cartoonify_image(image_cv)
# Convert images back to RGB for Streamlit
original_rgb = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
cartoon_rgb = cv2.cvtColor(cartoon_cv, cv2.COLOR_BGR2RGB)
# Display original and cartoon images
st.image([original_rgb, cartoon_rgb],
caption=["Original", "Cartoonified"],
width=300)
Add this final snippet to run the app:
if __name__ == "__main__":
main()
๐ Step 4: Run the Cartoonifier App
To launch your app:
streamlit run cartoonifier.py
It will open up a browser window with your cartoonifier app ready to go!

๐งช Test It Out!
Try uploading different images:
- Portraits
- Landscapes
- Pet photos
- Even artwork
Youโll notice how the cartoonifier preserves edges while giving a fun, smooth comic-book feel.
๐ ๏ธ Bonus: Customize It Your Way
Want to give it your own flair? Try these:
- Add sliders in Streamlit to control blur, edge detection thresholds, and filter strength
- Switch to Canny edge detection for a different style
- Batch process a folder of images
- Add download button for the cartoonified image
- Apply it to video frames using
cv2.VideoCapture
๐ค FAQs
Q: Why do we convert between RGB and BGR?
OpenCV uses BGR format by default, while images from PIL or Streamlit are in RGB. We convert to ensure correct colors.
Q: Can I use this on a webcam feed?
Yes! Use cv2.VideoCapture(0) and process each frame similarly.
Q: Will it work with black and white photos?
Yes โ the effect still works, but the color smoothing will have minimal impact.
๐ Final Thoughts
This little project packs a punch โ it teaches you about:
โ
Image preprocessing
โ
Edge detection
โ
Color smoothing
โ
Interactive UI design with Streamlit
Not only is it fun, itโs also a great foundation if youโre exploring computer vision, digital art, or AI-based stylization.
๐ Try It Yourself!
If youโre ready to cartoonify yourself or your cat (why not?), clone this project and run it in under 2 minutes.
Want help deploying it to the web? Or want to explore how AI can supercharge your business?
๐ Letโs chat at Ossels AI