Fundamentals of Video Processing and Image Manipulation
From the swap faces in video curriculum
Fundamentals of Video Processing and Image Manipulation
TL;DR
Video processing breaks a video down into individual images (frames) for manipulation. Image manipulation involves changing these frames using techniques like detecting features, transforming them, and blending. Putting these altered frames back together creates the final, modified video.
1. The Mental Model
Think of a video as a flipbook: a sequence of many still pictures shown quickly. To change something in the video, you're essentially changing one or more of these individual pictures (frames) before putting the flipbook back together.
2. The Core Material
When you're trying to do something like swap faces in a video, you're really performing a series of steps on individual images. A video is just a rapid succession of still images, called frames. So, video processing often boils down to processing each frame as an image.
2.1 Video Decomposition and Reconstruction

Photo by DS stories on Pexels
First, you need to break the video down into its constituent frames. Once you've manipulated those frames, you'll put them back together into a new video.
import cv2
def video_to_frames(video_path, output_folder):
"""Decomposes a video into individual frames."""
vidcap = cv2.VideoCapture(video_path)
count = 0
success = True
while success:
success, image = vidcap.read()
if success:
cv2.imwrite(f"{output_folder}/frame_{count:04d}.jpg", image)
count += 1
print(f"Extracted {count} frames from {video_path}")
return count
def frames_to_video(input_folder, output_video_path, fps, img_size):
"""Reconstructs frames into a video."""
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
out = cv2.VideoWriter(output_video_path, fourcc, fps, img_size)
for i in range(len([name for name in os.listdir(input_folder) if name.startswith('frame_')])):
img_path = f"{input_folder}/frame_{i:04d}.jpg"
frame = cv2.imread(img_path)
if frame is not None:
out.write(frame)
else:
print(f"Warning: Could not read {img_path}")
out.release()
print(f"Created video: {output_video_path}")
import os
# Example usage placeholder (assuming 'input.mp4' and 'frames_output' dir exist)
# video_to_frames('input.mp4', 'frames_output')
# frames_to_video('frames_output', 'output.mp4', 30, (1920, 1080)) # adjust fps and size
2.2 Image Manipulation Essentials

Photo by Carlos Jairo on Pexels
Once you have individual frames, you can apply image manipulation techniques. For something like face swapping, you'll need:
a) Feature Detection (e.g., Face Detection, Landmark Detection)
This involves finding specific points or regions in an image. For faces, this means identifying where a face is, and then finding key points like eyes, nose, and mouth corners (called facial landmarks). These landmarks help you align faces perfectly.
b) Geometric Transformations
Once you've detected a face and its landmarks, you often need to change its size, rotation, or position to match another face. This is where transformations like scaling, rotation, and translation come in.
c) Color Correction and Blending
Simply pasting one face onto another usually looks unnatural. You'll need to adjust the colors and brightness of the swapped face to match the target frame. Blending techniques, like seamless blending or alpha blending, help merge the new face smoothly into the background, avoiding harsh edges.
graph TD
A["Input Video"] --> B("Decompose Video into Frames")
B --> C{For Each Frame}
C --> D("Detect Face(s)")
D --> E("Detect Facial Landmarks (on each face)")
E --> F{"Perform Image Manipulation (e.g., Face Swap)"}
F --> G("Geometric Transformation (Align Faces)")
G --> H("Color Correction & Blending")
H --> I("Output Manipulated Frame")
I --> C
C --> J["Reconstruct Frames into New Video"]
J --> K["Output Video"]
3. Worked Example
Let's imagine you have frame_0001.jpg and you want to swap a face onto it.
- Face Detection: You use a library like
dliborOpenCVto find the main face inframe_0001.jpg. Let's say it finds a face at coordinates(100, 100)with width200and height200. - Landmark Detection: On this detected face, you find 68 specific points (like the corners of the eyes, tip of the nose, etc.).
- Source Face Preparation: You have a
source_face.jpgthat you want to put ontoframe_0001.jpg. You detect its face and landmarks too. - Alignment (Geometric Transformation): You calculate how much you need to scale, rotate, and move the
source_face.jpgso its landmarks perfectly align with the landmarks on the target face inframe_0001.jpg. For instance, if the source face's eyes are closer together than the target's, you'll scale it up. If it's tilted, you'll rotate it. -
Blending: After transforming the source face, you paste it onto
frame_0001.jpg. To make it look natural, you'd apply a technique like Poisson blending. This technique looks at the texture and color gradient of the target area and adjusts the pasted face to blend seamlessly. Instead of just plopping it on, it subtly changes the pasted pixels' values based on their surroundings.Imagine pasting a red square onto a blue background. Without blending, you get a harsh edge. With blending, the edge pixels would gradually shift from red to blue, making it look integrated.
4. Key Takeaways
- Videos are sequences of still images called frames; processing a video often means processing each frame individually.
- Decomposing a video extracts its frames, and reconstructing a video combines frames back into a moving picture.
- Face swapping involves detecting faces and their specific features (landmarks) within each frame.
- Geometric transformations (scaling, rotating, translating) are crucial for aligning a source face with a target face.
- Blending techniques are essential for smoothly integrating a manipulated face into a frame, preventing a "pasted-on" look.
- Libraries like OpenCV and dlib provide tools for these fundamental image and video processing tasks.
Common Mistakes to Avoid

Photo by KATRIN BOLOVTSOVA on Pexels
- Ignoring frame rate: Not setting the correct frames per second (fps) when reconstructing a video can make it play too fast or too slow.
- Poor alignment: Simply pasting a face without proper geometric alignment (scaling, rotation) will look artificial.
- No blending: Skipping the blending step results in obvious, harsh edges around the swapped face.
- Processing every frame identically: Sometimes only specific frames or regions need manipulation, and processing everything can be inefficient.
5. Now Try It
Choose a short video (5-10 seconds). Use the video_to_frames function provided to extract all its frames into a new folder. Then, open one of the extracted frames in an image editor (like Paint, GIMP, or Photoshop) and draw a silly mustache on a face. Save this modified frame back into the folder, making sure it overwrites the original frame with the exact same filename. Finally, use the frames_to_video function to reconstruct the video, using the same original FPS and frame size.
Success looks like: A new video where, for one frame, the person suddenly has a drawn-on mustache.
Frequently asked about Fundamentals of Video Processing and Image Manipulation
Study this next
Get the full swap faces in video curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account