Skip to content

[DISCUSSION]: Why can't I convert my RGB values to Munsell colours? #1329

@GeologyAMG

Description

@GeologyAMG

Question

I have a .csv of RGB values that I am attempting to convert to Munsell color. The goal is to have a new column added to the .csv that has the munsell values.

My Data
SO-05_Full_Res_Edit.csv

This script works fine for the df within the code. However when I try to expand it to a dataframe based on the attached .csv it keeps giving an error that the Y of xyY needs to be in the domain of [0 to 10]. However even when I check this it still gives the same error.

AssertionError Traceback (most recent call last)
Cell In[47], line 3
1 # Ensure that the Y value is normalized correctly before converting to Munsell
2 df['nY'] = df['nY'].clip(0, 10)
----> 3 df['Munsell'] = df.apply(lambda row: xyY_to_munsell((row['x'], row['y'], row['nY'])), axis=1)
5 # Print the DataFrame with XYZ, xyY, and Munsell columns
6 df.he#1300

#Code
import colour
import numpy as np
import pandas as pd

Example DataFrame of RGB values

data = {
'R': [255, 128, 64, 32, 16],
'G': [0, 255, 128, 64, 32],
'B': [0, 0, 255, 128, 64]
}
df = pd.DataFrame(data)

def rgb_to_xyz(rgb):
# Normalize RGB values to [0, 1]
rgb_normalized = np.array(rgb) / 255.0
# Convert RGB to XYZ
xyz = colour.sRGB_to_XYZ(rgb_normalized)
return xyz

def xyz_to_xyY(xyz):
# Convert XYZ to xyY
xyY = colour.XYZ_to_xyY(xyz)
return xyY

def xyY_to_munsell(xyY):
# Convert xyY to Munsell
munsell_color = colour.xyY_to_munsell_colour(xyY)
return munsell_color

Apply the conversion to each row in the DataFrame

df[['X', 'Y', 'Z']] = df.apply(lambda row: pd.Series(rgb_to_xyz((row['R'], row['G'], row['B']))), axis=1)
df[['x', 'y', 'Y']] = df.apply(lambda row: pd.Series(xyz_to_xyY((row['X'], row['Y'], row['Z']))), axis=1)
df['Munsell'] = df.apply(lambda row: xyY_to_munsell((row['x'], row['y'], row['Y'])), axis=1)

Print the DataFrame with XYZ, xyY, and Munsell columns

df.head()

#print(df)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions