Why can't I convert my RGB values to Munsell colours? #1331
Unanswered
GeologyAMG
asked this question in
Q&A
Replies: 1 comment
-
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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