Read/write/show images and videos in an IPython notebook
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mediapy-1.2.6.tar.gz
(28.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
mediapy-1.2.6-py3-none-any.whl
(27.5 kB
view details)
File details
Details for the file mediapy-1.2.6.tar.gz.
File metadata
- Download URL: mediapy-1.2.6.tar.gz
- Upload date:
- Size: 28.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c866cfa0a170213f771b1dd5584a2e82d8d0dc0fa94982f83e29aae27e49c83
|
|
| MD5 |
ca35424a507022abbf072aacfa8abe49
|
|
| BLAKE2b-256 |
b3eb8a0499fb1a2f373f97e2b4df91797507c3971c42c59f1610bed090c57ddc
|
File details
Details for the file mediapy-1.2.6-py3-none-any.whl.
File metadata
- Download URL: mediapy-1.2.6-py3-none-any.whl
- Upload date:
- Size: 27.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a0ea00eb0da83c3c54d588b49c49a41ba456174aa33e530ffe13e17269c9072
|
|
| MD5 |
649e97fe6f9b84e6b214d9d3cd46c7a4
|
|
| BLAKE2b-256 |
378c52f0299f1675cdfa1ab39a6028a2e5adf9032ae1118c9895c84b08af162b
|