Automatic Rotoscoping…For Free

Create a mind-blowingly useful automatic rotoscoping tool while learning basic Python in this 40 minute guide. No coding experience necessary.

We’re still a few years away from where machine learning AI can produce rotoscoping that rivals what human artists can achieve (though only a very few), but even today machine learning offers results that are more than adequate for color correction, camera tracking garbage mattes, or temp roto for slap comps while waiting on final outsourced roto. And with just a couple of lines of code you—the unskilled, non-programmer—can have that machine learning goodness at your command. What’s better, you can start to learn a little Python in the process.

Most beginner programming tutorials start with some lame text-based guessing game. I say skip to the meat. In this ‘Impossible Shot’ video you’ll get a good intro to programming in Python and come out the other side with an amazing rotoscoping tool that you can put into use in production today.

Half the battle is getting everything installed right, so I’ve made sure to clearly lay out all the details in the video. Below is a summary of what’s covered.

Step 1: Install Anaconda

Anaconda is an amazing installer package for Python that allows you to install third party software without having to search the web for it. It also makes virtual environments easy: virtual environments are development ‘sandboxes’ you can experiment in without messing up any other software installed on your computer. (Click here to download Anaconda for your platform)

Once you have Anaconda installed on your system, open the Anaconda prompt (or the regular Terminal on OS X) and execute the following commands to create and activate a custom virtual environment (which we’ll call ‘autoroto’—you can choose any name you like):

conda create --name autoroto
conda activate autoroto

To exit out of a virtual environment, just type:

conda deactivate

Step 2: Install the libraries we need

We’ll be working with code provided by, an amazing website for learning about computer vision and machine learning. The great thing about modern programming is that you don’t need to fully understand how something works to use the code. You can treat things like “black boxes” and just reap the benefits.

To use the code, we need to install some of the third party libraries required for it to run. Make sure your virtual environment is activated (see above) and type:

conda install torchvision -c pytorch
conda install -c anaconda pillow

Step 3: Download the sample code

Click here to download the code that we modified from We’ll use this as the basis for the auto rotoscoping. Now create a Python file. We’ll be using Wind Personal IDE here, but you can use another if you prefer. An IDE is an ‘Integrated Development Environment,’ basically a word processor specifically designed for writing code. It’ll help error check your programs and give you helpful hints and auto-completion of code as you type. Go ahead and download and install Wing if you want to follow along as closely as possible.

Save an new, empty Python file to the same directory as the sample code you just downloaded ( Call it ‘ ‘. As long as it’s in the same directory as, Python will have no problem linking to the FindMattes functions.

Step 4: Do an initial auto rotoscoping test

Let’s test out our rotoscoper to see if it actually works before sending a whole image sequence through. Just two lines of code is all it takes to create an automatic roto of an image. In add the following lines, then save the file:

import as fm"Path/to/your/source/image.png", "Path/to/resulting/matte_file.png", 256)

Replace the first part with the path to your test image (keep the quotes around the path. e.g. “C:/Users/yourusername/Documents/myImage.png” or “/Users/yourusername/Documents/myImage.png”), the second with the intended path and name for the resulting matte image (e.g. “C:/Users/yourusername/Documents/myImage_matte.png” or “/Users/yourusername/Documents/myImage_matte.png”), and the last number with the vertical resolution of the output image. My recommendation is to keep this at 256 for testing. Full res 2K and 4K images could take a long time to process on more modest machines.

Now run your program by typing into the command line prompt:

python "Path/to/your/program/"

When you look in the directory you should now find a matte image, automatically rotoscoping identified features of the shot, color-coded according to the 8 bit values listed in

Step 5: Ask the user for folders

So the thing works, but you want to be able to use for any old image sequence, right? Well, here’s the full code. I step through the entire thing in reasonable detail in the video, but if you’re desperate just to have the working version…type away.

import FindMattes as fm
import tkinter as tk
from tkinter import filedialog
from tkinter import simpledialog as sd
from os import listdir
# Get the file directory
root = tk.Tk()
directoryName = filedialog.askdirectory(parent=root, initialdir="/", title = 'Please select a directory')
matteHeight = sd.askinteger("Set the matte size", "Output matte vertical resolution (256 recommended for quick tests)?", parent=root,initialvalue=256)
listOfFiles = listdir(directoryName)
for currentFile in listOfFiles:
    sourceFile = directoryName + "/" + currentFile
    mainNameEnd = currentFile.find('.')
    nameForMatte = currentFile[:mainNameEnd] + "_matte" + currentFile[mainNameEnd:]
    fullPathMatteName = directoryName + "/" + nameForMatte
    fm.createMatte(sourceFile, fullPathMatteName, matteHeight)
    print("Just created: " + nameForMatte)


Step 6: Test the results in a compositor

After you’ve entered the above code in your file, save it, then run the code again from the command line:

python "Path/to/your/program/"

Then hop in to your favorite compositor, use a chroma key to isolate specific colors in the matte, then use it for whatever creative or utilitarian purpose you have in mind.

Step 0: Save time and watch the Impossible Shots walkthrough at

Was This Post Helpful:

0 votes, 0 avg. rating

Support ProVideo Coalition
Shop with Filmtools Logo

Share Our Article

Damian Allen is a VFX supervisor and pipeline consultant based in LA. He specializes in picture-lock emergency effects work and AR and VR tool development through his company Pixerati LLC. In addition to his hands-on…

Leave a Reply

3 Comment threads
11 Thread replies
Most reacted comment
Hottest comment thread
7 Comment authors
samDamian AllenSamMartin GossetSam Recent comment authors
newest oldest most voted
Notify of

Thanks for this! I got it working pretty well on windows! on mac however it skips around and doesn’t go in order of the image sequence? any idea what I’m doing wrong?

Damian Allen
Damian Allen

Actually, that’s going to be true on OS X–we haven’t required that the list of files be in alphabetical order. To fix that, just add the additional line:


immediately after listOfFiles = listdir(directoryName). That should arrange things from first file up.


Hi Damien, first thank for this wonderfull tutorial.
When a tr with just a single image i have this message from my IDE : “builtins.RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0”
It seems it come from the fonction trochvision.transforms.Normalize

Damian Allen
Damian Allen

That sounds like the image is in a format that the PIL image management library can’t handle. PNG and JPEG are the safest, with PNG obviously providing the least compression artifacting. If it’s already in one of these formats, try “re-saving” it as a PNG with the most basic export settings (no compressions etc.) Perhaps there’s an existing alpha channel that is causing the mismatch? If so try stripping it off the image before feeding it to the script. If that doesn’t work there’s the chance it might not like the aspect ration of the image; you can try padding… Read more »

martin Gosset

Thank you very much Damien. It was sure a problem with the png. Now with jpg it works !!! And it’s so incredible… Thank for sharing that with such simple explanation. Martin

Martin Gosset

So i made a test with a small group of images and use it with Final Cut Pro X…
Thank Damian Allen.


Great, but I wanted to test and :

“ModuleNotFoundError: No module named ‘’; ‘FindMattes’ is not a package
(autoroto) MPSAM:~ sam$”

(Yes I’m Sam BTW)

Any input ?

Thank you 😉

Damian Allen
Damian Allen

That means you don’t have the file saved to the same directory as your main Python script.


Hey Damian,
Thx for your answer but yes, is in the same (Documents) dir as the Python script… I’m so disappointed because it would be so handy for my projects…
Do you think I should without the step 5 included ?

Martin Gosset

Hi Sam. Be care, the code “” indicatde here” is not good. i think it’s just “fm.createMatte”. I don’t know if it’s the reason of your problem.


No, unfortunately it’s not the issue; still the same with just “fm.create”… But (on my mac at least), “import as fm” should be “import FindMattes as fm”, then it goes further, else see the message above… Now, as I wanted to make it work on a short clip and not an image : …. File “/Users/sam/Documents/”, line 40, in createMatte img = File “/Users/sam/anaconda3/lib/python3.7/site-packages/PIL/”, line 2822, in open raise IOError(“cannot identify image file %r” % (filename if filename else fp)) OSError: cannot identify image file ‘/Users/sam/Desktop/’ So how to make it work on a clip (= what to… Read more »


Ok, I made another attempt with simple file names and a space corrected…

python “/Users/sam/Documents/”
Downloading: “” to /Users/sam/.cache/torch/checkpoints/resnet101-5d3b4d8f.pth
100%|█████████████████████████████████████████████████████████████| 170M/170M [00:05<00:00, 32.2MB/s]
Downloading: "; to /Users/sam/.cache/torch/checkpoints/fcn_resnet101_coco-7ecb50ca.pth
100%|█████████████████████████████████████████████████████████████| 208M/208M [00:06<00:00, 35.1MB/s]
Traceback (most recent call last):
File "/Users/sam/Documents/", line 2, in
fm.createMatte(“/Users/sam/Desktop/abc.jpg”, “Users/sam/Desktop/a.jpg”, 256)
File “/Users/sam/Documents/”, line 51, in createMatte
File “/Users/sam/anaconda3/lib/python3.7/site-packages/PIL/”, line 2085, in save
fp =, “w+b”)
FileNotFoundError: [Errno 2] No such file or directory: ‘Users/sam/Desktop/a.jpg’
(base) MPSAM:~ sam$

Now I’ll really give up (though I feel myself close to success)…
Thx anyway

Damian Allen
Damian Allen

Not sure why your save name truncates to ‘a.jpg’…that sounds like a bug in the way you’re parsing the file. Nonetheless, it should still be able to save it. I’m just wondering if it’s a permissions issue somehow that it can’t open the file to write to. Try putting the images in a folder inside of your User/Documents and see if that helps. In theory your desktop should have the same write privileges, but perhaps there’s something funky with the permissions. If you still can’t get it to work, try copying and pasting my direct code below into a file… Read more »


Hey, First, really really thx for your answer, I’m honnestly interested in this technique which may be very handy in some projects of mine (I’m into Fusion, Mocha Pro and Resolve to remain short)… Now, even if it’s a script-kiddy copy/paste trick, I’m familiar with OS level code and power user stuff, and I can say it’s no permission issue… BUT, something rang a bell in what I noticed here : I put “a.jpg” as an output file name, am I not supposed to change it for the source file not to be replaced ? Or “matte_abc.jpg” should be the… Read more »