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?