How to Extract an Image from a Video using Python?

It's pretty likely that when working with video files in your Python app, you'll want to grab a screenshot of a particular moment in the video, basically extracting a single frame. This is especially true when making video thumbnails and still previews. Python makes this task incredibly easy, involving only a few lines of code.

Input Code
import requests

json = {
 'source': {
  'output_format': 'jpg',
  'snapshot_time': 1.94,
  'elements': [
    {
      'type': 'video',
      'source': 'https://cdn.creatomate.com/demo/bird.mp4'
    }
  ]
 }
}

response = requests.post(
 'https://api.creatomate.com/v1/renders',
 headers={
  # Find your API key under 'Project Settings' in your account:
  # https://creatomate.com/docs/api/rest-api/authentication
  'Authorization': 'Bearer Your-API-Key',
  'Content-Type': 'application/json',
 },
 json=json
)

# Wait a minute, then visit the URL provided in the response:
print(response.json())

Output Image

How it works: First, import the "requests" library, which is already included in every Python installation. Next, create a composition that includes a single video element and use the "source" property to specify the video URL. By default, the resulting image will have the same resolution as the video. But if you want a different image size, you can specify the "width" and "height" properties. Check out the related articles below for an example.

If you want to grab an image from the video, simply set the "output_format" property to "jpg". This will capture a JPEG snapshot at the specified time indicated by the "snapshot_time" property, which is set to 1.94 seconds from the start of the video. Alternatively, if you prefer a PNG image, you can use the "png" output format.

In addition, if you want to add a logo or text to the resulting image, below are some Python examples demonstrating how to do that. And if you want to create your own custom overlays, be sure to check out the online video editor below.

Edit this Code Example in the Video Editor

Easily design your own video templates using the online editor. Then, export your templates as JSON and integrate them into your Python application to create any kind of video – completely through code.

Related Python Video Editing Questions

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free