Read/write/show images and videos in an IPython notebook
Project Links
Meta
Author: Google LLC
Requires Python: >=3.8
Classifiers
Programming Language
- Python :: 3
License
- OSI Approved :: Apache Software License
Operating System
- OS Independent
Framework
- IPython
- Jupyter
Topic
- Multimedia :: Graphics :: Viewers
- Multimedia :: Video :: Display
Read/write/show images and videos in an IPython/Jupyter notebook.
[GitHub source] [API docs] [Colab example]
Examples:
See the notebook
mediapy_examples.ipynb
Images:
!pip install -q mediapy
import mediapy as media
import numpy as np
image = media.read_image('https://github.com/hhoppe/data/raw/main/image.png')
print(image.shape, image.dtype) # It is a numpy array.
media.show_image(image)
checkerboard = np.kron([[0, 1] * 16, [1, 0] * 16] * 16, np.ones((4, 4)))
media.show_image(checkerboard)
media.show_image(media.color_ramp((128, 128)), height=48, title='ramp')
images = {
'original': image,
'brightened': media.to_float01(image) * 1.5,
}
media.show_images(images)
media.write_image('/tmp/checkerboard.png', checkerboard)
Videos:
url = 'https://github.com/hhoppe/data/raw/main/video.mp4'
video = media.read_video(url)
print(video.shape, video.dtype) # It is a numpy array.
print(video.metadata.fps) # The 'metadata' attribute includes framerate.
media.show_video(video) # Play the video using the retrieved framerate.
media.show_images(video, height=80, columns=4) # Show frames side-by-side.
video = media.moving_circle((128, 128), num_images=10)
media.show_video(video, fps=10)
media.write_video('/tmp/video.mp4', video, fps=60)
# Darken a video frame-by-frame:
filename_in = '/tmp/video.mp4'
filename_out = '/tmp/out.mp4'
with media.VideoReader(filename_in) as r:
print(f'shape={r.shape} fps={r.fps} bps={r.bps}')
darken_image = lambda image: media.to_float01(image) * 0.5
with media.VideoWriter(
filename_out, shape=r.shape, fps=r.fps, bps=r.bps) as w:
for image in r:
w.add_image(darken_image(image))
media.show_video(media.read_video(filename_out), fps=60)
Setup:
Video I/O relies on the external program ffmpeg
, which must be present in
the system PATH. On Unix, it can be installed using:
apt install ffmpeg
or within a notebook using:
!command -v ffmpeg >/dev/null || (apt update && apt install -y ffmpeg)
May 01, 2025
1.2.4
Apr 16, 2025
1.2.3
Jun 06, 2024
1.2.2
May 30, 2024
1.2.1
Dec 06, 2023
1.2.0
Aug 07, 2023
1.1.9
Jun 28, 2023
1.1.8
Jun 26, 2023
1.1.7
Feb 24, 2023
1.1.6
Feb 20, 2023
1.1.5
Jan 06, 2023
1.1.4
Jan 03, 2023
1.1.3
Oct 20, 2022
1.1.2
Oct 17, 2022
1.1.1
Aug 03, 2022
1.1.0
Jul 29, 2021
1.0.3
Jun 15, 2021
1.0.2
May 24, 2021
1.0.1
May 17, 2021
1.0.0
Apr 02, 2021
0.2.2
Apr 01, 2021
0.2.1
Mar 29, 2021
0.2.0
Mar 21, 2021
0.1.3
Mar 19, 2021
0.1.2
Mar 18, 2021
0.1.1
Mar 16, 2021
0.1.0
Mar 13, 2021
0.0.2
Mar 12, 2021
0.0.1