CodeGym /Courses /Python SELF EN /Using Visual Filters and Effects: Adjusting Brightness, C...

Using Visual Filters and Effects: Adjusting Brightness, Contrast, and Color

Python SELF EN
Level 48 , Lesson 1
Available

1. Visual Effects

Intro to Visual Effects

Today we’ll learn not just how to tweak brightness and contrast, but also how to give your creations a special vibe that rivals Hollywood blockbusters. Or, you know, just make your video a bit more watchable if it was shot on an outdated phone.

When it comes to video editing, knowing how to apply visual effects is your secret sauce to crafting content that grabs attention. Effects can make videos pop, highlight key details, or totally transform the overall vibe of your clip.

The Basics of Adjusting Brightness, Contrast, and Color

Before diving into the code, let’s take a refresher on what brightness, contrast, and color correction mean in the context of videos.

  • Brightness — this is how light or dark the image looks overall. Increasing brightness makes it lighter, and decreasing it makes it darker.
  • Contrast — this is the difference between the lightest and darkest parts of the video. High contrast makes it more vivid, while low contrast makes it look flatter.
  • Color correction — this refers to tweaking the color balance to improve video quality.

2. Adjusting Video Brightness

Alright, let’s dive into some code and see how we can use MoviePy to apply these effects.

Adjusting Video Brightness with fl_image()

You can adjust video brightness using the fl_image method, which lets you apply custom functions to all video frames. To increase brightness by 20%, simply use a multiplier of 1.2.

Python

from moviepy.editor import VideoFileClip

# Function to adjust brightness
def change_brightness(image, factor=1.2):
    return image * factor

# Load the video file
clip = VideoFileClip("video.mp4")

# Apply brightness adjustment
brightened_clip = clip.fl_image(lambda frame: change_brightness(frame, 1.2))

# Save the result
brightened_clip.write_videofile("brightened_video.mp4")

Adjusting Video Brightness Using moviepy.video.fx.all

You can also adjust brightness with the lum_contrast effect, which lets you increase or decrease video brightness.

Increasing Video Brightness

Python

from moviepy.editor import VideoFileClip
from moviepy.video.fx.all import lum_contrast

# Load the video
video = VideoFileClip("sample_video.mp4")

# Apply brightness effect
bright_video = lum_contrast(video, lum=30)  # Increase brightness by 30 units

# Save the result
bright_video.write_videofile("bright_video.mp4")

In this example:

  • lum_contrast(video, lum=30) increases brightness by 30 units. Brightness values can be positive (to brighten) or negative (to darken).

Decreasing Video Brightness

Python

# Decrease video brightness
dark_video = lum_contrast(video, lum=-30)  # Decrease brightness by 30 units

# Save the result
dark_video.write_videofile("dark_video.mp4")

3. Adjusting Video Contrast

Contrast is about how vivid and rich the light and dark parts of a video appear. Increasing contrast makes light areas brighter and dark areas darker, while decreasing it reduces the difference between those regions.

Adjusting Contrast with fl_image()

You can tweak contrast similarly using an image transformation function. Here’s how:

Python

import numpy as np

# Function to adjust contrast
def change_contrast(image, factor=1.5):
    mean = np.mean(image)
    return (image - mean) * factor + mean

# Apply contrast adjustment
contrasted_clip = clip.fl_image(lambda frame: change_contrast(frame, 1.5))

# Save the result
contrasted_clip.write_videofile("contrasted_video.mp4")

Adjusting Contrast Using lum_contrast()

Boosting Video Contrast

Python

# Increase contrast
high_contrast_video = lum_contrast(video, contrast=1.5)  # Boost contrast by 50%

# Save the result
high_contrast_video.write_videofile("high_contrast_video.mp4")

Here:

  • contrast = 1.5 boosts contrast by 50%. Any value greater than 1 increases contrast.

Lowering Video Contrast

Python

# Decrease contrast
low_contrast_video = lum_contrast(video, contrast=0.5)  # Lower contrast by 50%

# Save the result
low_contrast_video.write_videofile("low_contrast_video.mp4")

Here:

  • contrast = 0.5 reduces contrast by 50%. Values less than 1 lower contrast, making the image look less vivid.

4. Adjusting Video Color

MoviePy has tools for tweaking the color palette, converting footage to black and white, and other color effects.

Color Correction Using fl_image()

For color correction, you can use the rgb_color function to modify the video’s color palette.

Python

def adjust_color(image, factor=0.8):
    return image * np.array([1, factor, factor])  # reduce green and blue channels

# Apply color correction
color_corrected_clip = clip.fl_image(lambda frame: adjust_color(frame, 0.8))

# Save the result
color_corrected_clip.write_videofile("color_corrected_video.mp4")
1
Task
Python SELF EN, level 48, lesson 1
Locked
Adjusting Video Brightness
Adjusting Video Brightness
2
Task
Python SELF EN, level 48, lesson 1
Locked
Increase contrast and convert to black and white
Increase contrast and convert to black and white
3
Task
Python SELF EN, level 48, lesson 1
Locked
Applying Sepia Effect to Video
Applying Sepia Effect to Video
4
Task
Python SELF EN, level 48, lesson 1
Locked
Creating a custom video filter
Creating a custom video filter
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION