Building a cartoon-styled video using Python ๐Ÿ

Building a cartoon-styled video using Python ๐Ÿ

ยท

3 min read

Quick Intro

In this article, you will learn how to build a cartoon styled version of a video by using OpenCV!

๐Ÿ“ŒNote : OpenCV stands for Open Computer Vision. It is an open-source library written in Python, which can analyze images and videos.

Actually, I came across this article where a video can transform into a "pencil sketch form" & it got me inspired to build the same but with a new style! ๐Ÿ˜‰

๐Ÿš€ Let's dive in as I'm super excited to walk you through it!

๐Ÿ‘ฉโ€๐Ÿ’ป Coding begins

gif

0. Installation

I assume you must have installed Python on your machine. If not then please install & set it up from here! After this, open your Command Prompt as you have to install OpenCV library using the command --

pip install opencv-python

1. Imports

Open VSCode or any code editor of your choice, and start writing code inside a new .py file!

  • First, import the installed library,i.e, cv2 (aka opencv)
  • Next, specify the video you want to load (I'm using a dolphin ๐Ÿฌ clip here) under function, cv2.VideoCapture()
import cv2

cap = cv2.VideoCapture("dolphin.mp4")

2. Video Capture Properties

Get the height & width of the video frames & frame rate as well. cap is the variable you loaded the video in the previous step!

height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = cap.get(cv2.CAP_PROP_FPS)

3. Create VideoWriter Object

Here comes the most important step, where you have to specify the following info ๐Ÿ‘‡

  • filename: the place to save final results
  • fourcc: 4-character code of codec used to compress the frames. If you are a Windows User like me, then do use DIVX only! Learn more here
  • Add thefps, height & width properties too!
    video_writer = cv2.VideoWriter('results\output.avi', cv2.VideoWriter_fourcc('D','I','V','X'),  fps, (width, height))
    

    4. Creating a Loop

    Follow the code steps below ๐Ÿ‘‡
#Loop through each frame
for frame_idx in range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))):

    #Reads each frame
    ret, frame = cap.read()

    #Convert the video into a cartoon styled effect
    cartoon_image = cv2.stylization(frame, sigma_s=150, sigma_r=0.25) 

     #Display the video
    cv2.imshow('Video Player', cartoon_image)

    #Write (saves) out frame 
    video_writer.write(cartoon_image)

    #Breaks the loop when you press the 'Q'key
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

#Closes down everything
cap.release()
cv2.destroyAllWindows()

#Releases video writer
video_writer.release()

Save all the above code & make sure you have the 'original video file' as well as the code all in one place. Open Command Prompt, run the below command --

python cartoon_video.py

๐Ÿ‘†cartoon_video.py is the code file. A window will come up running the cartoon effect & have patience until it's done!

5. Demo

๐Ÿฅ Presenting, the combined demo for both of the videos! It's a canva design for a stylish showdown ๐Ÿคฉ

  • ๐Ÿ‘‰ AWESOME DEMO ๐Ÿ‘ˆ

  • P.S: The background sound comes from the original video. Notice the cartoon effect video, isn't it amazing? ๐Ÿ˜ฎ

Wrap Up

Congratulations! ๐ŸŽ‰๐ŸŽŠ

You have learned how to give a cartoon-styled effect in a video using Python. I hope you must have learned about the awesome magic of OpenCV here โœจ & also had fun along the way.

You are free to tweak the code and add your own twist.

โญ Please do share a demo, if you added any changes or tried something different with the code above! I would love to read them ๐Ÿค—

Don't forget to drop your reactions ๐Ÿ’œ, sweet thoughts, demos & any questions in the comments below!

๐Ÿค Reach me out on Twitter, Github & LinkedIn. for more such type of magical hacks!

ย