Coder Social home page Coder Social logo

yuvio's Introduction

License GitHub top language GitHub stars PyPI - Python Version PyPI

“YUVIO”

Welcome to yuvio, a python package for reading and writing uncompressed yuv image and video data. yuvio supports many pixel formats specified by ffmpeg. And if it doesn't, it's fast and easy to add support for your own pixel formats.

NEW: Added support for colorspace conversion (YCbCr to/from RGB). For usage, see below. Supported specifications are

Install

yuvio is easily installed using python's pip package manager.

pip install yuvio

Usage

To read and write yuv files, yuvio provides the functions imread, imwrite, mimread and mimwrite for single and multiple frames, respectively. Both require the specification of a pixel format to use for the yuv data.

import yuvio

yuv_frame = yuvio.imread("example_yuv420p.yuv", 1920, 1080, "yuv420p")
yuvio.imwrite("example_yuv420p_copy.yuv", yuv_frame)

yuv_frames = yuvio.mimread("example_yuv420p.yuv", 1920, 1080, "yuv420p")
yuvio.mimwrite("example_yuv420p_copy.yuv", yuv_frames)

Thereby, yuvio is not restricted to file objects and can read and write from/to other io streams as well. For example, this allows to conveniently unpack the individual yuv planes from interleaved yuv data available in memory.

import io
import yuvio

data = ...  # e.g. np.ndarray
buffer = io.BytesIO(data)
yuv_frame = yuvio.imread(buffer, 1920, 1080, "v210")
y = yuv_frame.y
u = yuv_frame.u
v = yuv_frame.v

For advanced use cases, yuvio also provides direct access to the Reader and Writer objects. This allows sequential reading and writing from/to yuv files and may be beneficial for iterating over large files without keeping all frames in memory.

import yuvio

reader = yuvio.get_reader("example_yuv420p.yuv", 1920, 1080, "yuv420p")
writer = yuvio.get_writer("example_yuv420p_copy.yuv", 1920, 1080, "yuv420p")

for yuv_frame in reader:
    writer.write(yuv_frame)

To create custom yuv data, yuvio provides access to convenient yuv frame initializers empty, zeros and ones similar to numpys convenience array initializers.

import yuvio

empty = yuvio.empty(1920, 1080, "yuv420p")
zeros = yuvio.zeros(1920, 1080, "yuv420p")
ones = yuvio.ones(1920, 1080, "yuv420p")

For advanced use cases, yuvio also allows direct yuv frame initialization from custom data using the frame initializer.

import yuvio
import numpy as np

y = 255 * np.ones((1920, 1080), dtype=np.uint8)
u = np.zeros((960, 540), dtype=np.uint8)
v = np.zeros((960, 540), dtype=np.uint8)
frame_420 = yuvio.frame((y, u, v), "yuv420p")

frame_400 = yuvio.frame((y, None, None), "gray")

Colorspace conversion

Colorspace conversion from RGB to YCbCr colorspace and from YCbCr to RGB colorspace is possible using the from_rgb(rgb, pixel_format, specification, value_range) and to_rgb(yuv, specification, value_range) functions.

import yuvio

yuv_frame = ...
rgb = yuvio.to_rgb(yuv_frame, specification='bt709', value_range='limited')
yuv_frame = yuvio.from_rgb(rgb, 'yuv444p', specification='bt709', value_range='limited')

Important

Color conversion is only supported for '444' chroma subsampling, i.e., no chroma subsampling. Support for chroma subsampling is planned for a future release.

Formats

Print a complete list of available pixel formats using print(yuvio.pixel_formats). To get detailed information on the IO format of a specific pix_fmt use print(yuvio.pixel_formats[pix_fmt].io_info()).

Currently, the following pixel formats (pix_fmt) are available:

  • 'gray'
  • 'gray10le'
  • 'gray10be'
  • 'gray16le'
  • 'gray16be'
  • 'gray9le'
  • 'gray9be'
  • 'gray12le'
  • 'gray12be'
  • 'gray14le'
  • 'gray14be'
  • 'nv12'
  • 'v210'
  • 'yuv420p'
  • 'yuv420p10le'
  • 'yuv420p10be'
  • 'yuv420p16le'
  • 'yuv420p16be'
  • 'yuv420p9le'
  • 'yuv420p9be'
  • 'yuv420p12le'
  • 'yuv420p12be'
  • 'yuv420p14le'
  • 'yuv420p14be'
  • 'yuv422p'
  • 'yuv422p10le'
  • 'yuv422p10be'
  • 'yuv422p16le'
  • 'yuv422p16be'
  • 'yuv422p9le'
  • 'yuv422p9be'
  • 'yuv422p12le'
  • 'yuv422p12be'
  • 'yuv422p14le'
  • 'yuv422p14be'
  • 'yuv444p'
  • 'yuv444p10le'
  • 'yuv444p10be'
  • 'yuv444p16le'
  • 'yuv444p16be'
  • 'yuv444p9le'
  • 'yuv444p9be'
  • 'yuv444p12le'
  • 'yuv444p12be'
  • 'yuv444p14le'
  • 'yuv444p14be'
  • 'yuyv422'
  • 'uyvy422'
  • 'yvyu422'

yuvio's People

Contributors

d-prieur avatar labradon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yuvio's Issues

Document usage with OpenCV

Please provide an example on how to use with PIL or OpenCV data, or more specific, existing NumPy arrays (which are used with OpenCV).

Maybe having a helper function would be nice as well...

Usage with raw data

Thanks for this useful library! Can I use this if only have the raw data?

For instance, PyAV returns frame.planes as a list, with three entries for YUV 4:2:0, or one entry for UYVY 4:2:2. I want to convert the UYVY representation to YUV 4:2:0 using yuvio. How can I construct a frame when I don't have the Y/U/V components separated already?

For reference, I'm thinking about using this here:

https://github.com/VQEG/siti-tools/blob/2eb188f1f1a812bb5f96d5a330ee2fe7ad1b486c/siti_tools/file.py#L55-L67

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.