mediapy

1.2.6 · active · verified Sun Apr 12

mediapy is a Python library designed to simplify reading, writing, and displaying images and videos, especially within IPython/Jupyter notebooks. Developed by Google LLC, it is currently on version 1.2.6 and receives updates periodically.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read and display both local/remote images and videos. For video functionality, ensure that the `ffmpeg` command-line tool is installed and accessible in your system's PATH. An example of writing an image to a file is also included.

import mediapy as media
import numpy as np
import shutil
import os

# --- Image Example ---
image_url = 'https://github.com/hhoppe/data/raw/main/image.png'
print(f"Reading image from: {image_url}")
image = media.read_image(image_url)
print(f"Image shape: {image.shape}, dtype: {image.dtype}")
media.show_image(image, title='Remote Image')

# Example of creating and saving an image
checkerboard = np.kron([[0, 1] * 16, [1, 0] * 16] * 16, np.ones((4, 4)))
output_image_path = '/tmp/checkerboard.png'
media.write_image(output_image_path, checkerboard)
print(f"\nCheckerboard image written to {output_image_path}")

# --- Video Example (requires ffmpeg) ---
if shutil.which('ffmpeg'):
    video_url = 'https://github.com/hhoppe/data/raw/main/video.mp4'
    print(f"\nReading video from: {video_url}")
    video = media.read_video(video_url)
    print(f"Video shape: {video.shape}, dtype: {video.dtype}")
    if hasattr(video, 'metadata') and hasattr(video.metadata, 'fps'):
        print(f"Video framerate: {video.metadata.fps} fps")
        media.show_video(video, title='Remote Video', fps=video.metadata.fps)
    else:
        media.show_video(video, title='Remote Video - FPS unknown')
else:
    print("\nFFmpeg is not installed or not in PATH. Skipping video example.")
    print("Please install ffmpeg (e.g., 'brew install ffmpeg' on macOS, 'apt install ffmpeg' on Debian/Ubuntu) for video functionality.")

view raw JSON →