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.

[GitHub source]   [API docs]   [PyPI package]   [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; no framerate.)
    media.show_video(video, fps=60)  # Display at 60 frames/s.

    with VideoReader(url) as reader:
      show_video(reader, fps=reader.fps)

    media.show_images(video, height=80, columns=4)  # 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-get 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-0.2.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

mediapy-0.2.0-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mediapy-0.2.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.7

File hashes

Hashes for mediapy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1ccf392b638604f09298d7abfb433263101891ac8babee4de27cf507b1dfe18f
MD5 e2ea389a84fb5095ba50da7bb9d276ad
BLAKE2b-256 03bd84a4820eeffd7c8d37a33a23bf96dc367543d687bdfa73a9cf7ea1b96e48

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: mediapy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.7

File hashes

Hashes for mediapy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a56130d55afa999e39ab4e9674408695def229b5db665ae0d7c19a0ce9a845d
MD5 acb756a66e08b46fb69c27fe54b52370
BLAKE2b-256 25b8904787e0dc248c4dd3ae4fb0f774b302fe37c910d677d543429139d0dd6d

See more details on using hashes here.

Provenance

Supported by

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