Why can't I convert my RGB values to Munsell colours? #1331
Replies: 2 comments 7 replies
-
|
Hello @GeologyAMG, It is hard to understand what is going on given there is no traceback but a cursory look at your CSV file shows that # /// script
# dependencies = [
# "colour-science",
# "matplotlib",
# "pandas",
# ]
# ///
import colour
import pandas as pd
df = pd.read_csv("/Users/kelsolaar/Downloads/SO-05_Full_Res_Edit.csv")
RGB = colour.utilities.tstack([df["R"].values, df["G"].values, df["B"].values]) / 255
XYZ = colour.sRGB_to_XYZ(RGB)
non_representable = []
for i, xyY in enumerate(colour.XYZ_to_xyY(XYZ)):
try:
colour.xyY_to_munsell_colour(xyY)
except AssertionError as exception:
print(f"Error at row {i}: {RGB[i]}, {exception}")
non_representable.append(i)
swatches = [
colour.plotting.ColourSwatch(swatch, "x" if i in non_representable else "")
for i, swatch in enumerate(RGB)
]
colour.plotting.plot_multi_colour_swatches(swatches, columns=15) |
Beta Was this translation helpful? Give feedback.
-
|
Looking back at the above discussion about the munsell colors not being less than 2C. That is an interesting point... Although I think this is more of a nomenclature issue... Munsell certainly knew that colors could be devoid of chroma, as that is actually the entire basis of his color system. He would mix relative proportions and strengths of color on a Maxwell disk until they appeared neutral. In a proper munsell book, there are many patches that are noted to be an "N0" or "N1" patch, so these could be considered munsell colors. But the next question would be is when to switch from the hue nomenclature to the N nomenclature. ... and... in our case probably never. Or with a very small epsilon around 0C, such as <= 0.005 which should be less than the accuracy of most measurement equipment today (such small chroma that no real world device could measure the difference). Of course, I'm making that threshold up, not based on any citation. |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
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)
Beta Was this translation helpful? Give feedback.
All reactions