Skip to main content

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

Project description

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)

Project details


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.4.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

mediapy-1.2.4-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file mediapy-1.2.4.tar.gz.

File metadata

  • Download URL: mediapy-1.2.4.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for mediapy-1.2.4.tar.gz
Algorithm Hash digest
SHA256 052947a676ebd3491359a6943b144559fdc414f741b03a05c78fa7b6291e6b12
MD5 e01863366e944df8617f226607e8e3f6
BLAKE2b-256 04a3e92406a5552130085bc741861de2d02e1cc16f0615373e0a4cd0016de16d

See more details on using hashes here.

File details

Details for the file mediapy-1.2.4-py3-none-any.whl.

File metadata

  • Download URL: mediapy-1.2.4-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for mediapy-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dd2b2364543077a1d292cab9eca5d08c8550521523aa1cbb721fd7617ccde872
MD5 a5bafe064d5f558539c27db1ad85adc3
BLAKE2b-256 07cb9419149225b44d2cec12208430044c7310dd778d5348000ebb274bc3e2ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page