mediapy

Read/write/show images and videos in an IPython notebook

30 个版本 Python >=3.8
安装
pip install mediapy
poetry add mediapy
pipenv install mediapy
conda install mediapy
描述

Read/write/show images and videos in an IPython/Jupyter notebook.

Unittests PyPI version

[GitHub source]**   [API docs]**   **[Colab example]**

Examples:

See the notebook   mediapy_examples.ipynb     Open In Colab   Open in Binder

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)
版本列表
1.2.6 2026-02-03
1.2.5 2025-12-17
1.2.4 2025-05-01
1.2.3 2025-04-16
1.2.2 2024-06-06
1.2.1 2024-05-30
1.2.0 2023-12-06
1.1.9 2023-08-07
1.1.8 2023-06-28
1.1.7 2023-06-26
1.1.6 2023-02-24
1.1.5 2023-02-20
1.1.4 2023-01-06
1.1.3 2023-01-03
1.1.2 2022-10-20
1.1.1 2022-10-17
1.1.0 2022-08-03
1.0.3 2021-07-29
1.0.2 2021-06-15
1.0.1 2021-05-24
1.0.0 2021-05-17
0.2.2 2021-04-02
0.2.1 2021-04-01
0.2.0 2021-03-29
0.1.3 2021-03-21
0.1.2 2021-03-19
0.1.1 2021-03-18
0.1.0 2021-03-16
0.0.2 2021-03-13
0.0.1 2021-03-12