Skip to content

Instantly share code, notes, and snippets.

@mertsalik
Created March 9, 2016 14:45
Show Gist options
  • Save mertsalik/0ca64f8f5e848b7fc1fd to your computer and use it in GitHub Desktop.
Save mertsalik/0ca64f8f5e848b7fc1fd to your computer and use it in GitHub Desktop.
import numpy as np
def rgb2ycbcr(rgb_color):
"""
requires numpy
http://www.equasys.de/colorconversion.html
:param rgb_color: tuple(R,G,B)
:return: tuple(Y,Cb,Cr)
"""
if not isinstance(rgb_color, tuple):
raise TypeError("This method only accepts color parameter as tuple!")
base = np.matrix("16;128;128")
factor = np.matrix("0.257 0.504 0.098; -0.148 -0.291 0.439; 0.439 -0.368 -0.071")
rgb_input = np.matrix(rgb_color)
result = (factor * rgb_input.T) + base
if result.shape != (3, 1):
raise ArithmeticError("Something went wrong while computing the matrix calculation!")
arr = np.asarray(result)
return [int(arr[0][0]), int(arr[1][0]), int(arr[2][0])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment