Icon

kn_​forum_​63461_​cielab_​colorspace_​hex

Python script to convert CIELAB color parameters to hexadecimal color codes in KNIME

Python script to convert CIELAB color parameters to hexadecimal color codes in KNIME

import knime.scripting.io as kniodf = knio.input_tables[0].to_pandas()import pandas as pdfrom colormath.color_objects import LabColor, sRGBColorfrom colormath.color_conversions import convert_color# Define a function to convert CIELAB parameters to hexadecimal codedef convert_to_hex(row): lab = LabColor(row['l'], row['a'], row['b']) rgb = convert_color(lab, sRGBColor) return rgb.get_rgb_hex()# Apply the conversion function to each row of the data framedf['hex_code'] = df.apply(convert_to_hex, axis=1)# Print the resulting data frameprint(df)knio.output_tables[0] = knio.Table.from_pandas(df) This Python code reads in a data table from the first input port of a KNIME node using the knime.scripting.io library. The data table is then converted to a Pandas data frame using the to_pandas()method.The code then defines a function named convert_to_hex() that converts CIELAB color parameters to hexadecimal color codes using the colormath library.The apply() method is then used to apply the convert_to_hex() function to each row of the data frame, and the resulting hexadecimal codes are added as a new column named hex_code to the dataframe. Finally, the resulting data frame is saved to the first output port of the KNIME node using the from_pandas() method of the knime.scripting.io.Table class.Overall, this code converts CIELAB color parameters in a data table to corresponding hexadecimal color codes and outputs the result to a new table. Python script to convert CIELAB color parameters to hexadecimal color codes in KNIMEhttps://forum.knime.com/t/colorspace-doesnt-work-in-knime-interactive-r/63461/4?u=mlauber71 KNIME and Python — Setting up and managing Conda environmentshttps://medium.com/p/2ac217792539 # conda env create -f="/Users/m_lauber/Dropbox/knime-workspace/forum/2023/kn_forum_63461_cielab_colorspace_hex/data/py3_knime_colorspace.yml"# conda env create -f="C:\\Users\\x123456\\knime-workspace\\forum\\2023\\kn_forum_63461_cielab_colorspace_hex\\data\\py3_knime_colorspace.yml"# conda activate py3_knime_colorspace# conda update -n py3_knime_colorspace --all# conda env update --name py3_knime_colorspace --file "/Users/m_lauber/Dropbox/knime-workspace/forum/2023/kn_forum_63461_cielab_colorspace_hex/data/py3_knime_colorspace.yml" --prune# conda env update --name py3_knime_colorspace --file "C:\\Users\\x123456\\knime-workspace\\forum\\2023\\kn_forum_63461_cielab_colorspace_hex\\data\\py3_knime_colorspace.yml" --prune# conda env update --name py3_knime_colorspace --file "/Users/m_lauber/Dropbox/knime-workspace/forum/2023/kn_forum_63461_cielab_colorspace_hex/data/py3_knime_colorspace.yml"# conda env update --name py3_knime_colorspace --file "C:\\Users\\x123456\\knime-workspace\\forum\\2023\\kn_forum_63461_cielab_colorspace_hex\\data\\py3_knime_colorspace.yml"# conda update -n base conda# KNIME official Python integration guide# https://docs.knime.com/latest/python_installation_guide/index.html#_introduction# KNIME and Python — Setting up and managing Conda environments# https://medium.com/p/2ac217792539# Hyperparameter optimization for LightGBM — wrapped in KNIME nodes# https://medium.com/p/ddb7ae1d7e2# conda activate py3_knime_colorspace# file: py3_knime_colorspace.yml with some modifications# THX Carsten Haubold (https://hub.knime.com/carstenhaubold) for hintsname: py3_knime_colorspace # Name of the created environmentchannels: # Repositories to search for packages# - defaults # edit: removed to just use conda-forge# - anaconda # edit: removed to just use conda-forge - conda-forge# https://anaconda.org/knime - knime # conda search knime-python-base -c knime --info # to see what is in the packagedependencies: # List of packages that should be installed- python=3.9 # Python- knime-python-base # dependencies of KNIME - Python integration# - knime-python-scripting # everything you need to also build Python packages for KNIME- cairo # SVG support- pillow # Image inputs/outputs- matplotlib # Plotting- IPython # Notebook support- nbformat # Notebook support- scipy # Notebook support- jpype1 # A Python to Java bridge# Jupyter Notebook support- jupyter # Jupyter Notebook- pandas-profiling # create overview of your data- sweetviz # In-depth EDA (target analysis, comparison, feature analysis, correlation) in two lines of code!- colormath- pip # Python installer- pip:# - JPype1 # Databases data.csvpy3_knime_colorspacecolor_data.table CSV Reader Python Script Conda EnvironmentPropagation Table Writer import knime.scripting.io as kniodf = knio.input_tables[0].to_pandas()import pandas as pdfrom colormath.color_objects import LabColor, sRGBColorfrom colormath.color_conversions import convert_color# Define a function to convert CIELAB parameters to hexadecimal codedef convert_to_hex(row): lab = LabColor(row['l'], row['a'], row['b']) rgb = convert_color(lab, sRGBColor) return rgb.get_rgb_hex()# Apply the conversion function to each row of the data framedf['hex_code'] = df.apply(convert_to_hex, axis=1)# Print the resulting data frameprint(df)knio.output_tables[0] = knio.Table.from_pandas(df) This Python code reads in a data table from the first input port of a KNIME node using the knime.scripting.io library. The data table is then converted to a Pandas data frame using the to_pandas()method.The code then defines a function named convert_to_hex() that converts CIELAB color parameters to hexadecimal color codes using the colormath library.The apply() method is then used to apply the convert_to_hex() function to each row of the data frame, and the resulting hexadecimal codes are added as a new column named hex_code to the dataframe. Finally, the resulting data frame is saved to the first output port of the KNIME node using the from_pandas() method of the knime.scripting.io.Table class.Overall, this code converts CIELAB color parameters in a data table to corresponding hexadecimal color codes and outputs the result to a new table. Python script to convert CIELAB color parameters to hexadecimal color codes in KNIMEhttps://forum.knime.com/t/colorspace-doesnt-work-in-knime-interactive-r/63461/4?u=mlauber71 KNIME and Python — Setting up and managing Conda environmentshttps://medium.com/p/2ac217792539 # conda env create -f="/Users/m_lauber/Dropbox/knime-workspace/forum/2023/kn_forum_63461_cielab_colorspace_hex/data/py3_knime_colorspace.yml"# conda env create -f="C:\\Users\\x123456\\knime-workspace\\forum\\2023\\kn_forum_63461_cielab_colorspace_hex\\data\\py3_knime_colorspace.yml"# conda activate py3_knime_colorspace# conda update -n py3_knime_colorspace --all# conda env update --name py3_knime_colorspace --file "/Users/m_lauber/Dropbox/knime-workspace/forum/2023/kn_forum_63461_cielab_colorspace_hex/data/py3_knime_colorspace.yml" --prune# conda env update --name py3_knime_colorspace --file "C:\\Users\\x123456\\knime-workspace\\forum\\2023\\kn_forum_63461_cielab_colorspace_hex\\data\\py3_knime_colorspace.yml" --prune# conda env update --name py3_knime_colorspace --file "/Users/m_lauber/Dropbox/knime-workspace/forum/2023/kn_forum_63461_cielab_colorspace_hex/data/py3_knime_colorspace.yml"# conda env update --name py3_knime_colorspace --file "C:\\Users\\x123456\\knime-workspace\\forum\\2023\\kn_forum_63461_cielab_colorspace_hex\\data\\py3_knime_colorspace.yml"# conda update -n base conda# KNIME official Python integration guide# https://docs.knime.com/latest/python_installation_guide/index.html#_introduction# KNIME and Python — Setting up and managing Conda environments# https://medium.com/p/2ac217792539# Hyperparameter optimization for LightGBM — wrapped in KNIME nodes# https://medium.com/p/ddb7ae1d7e2# conda activate py3_knime_colorspace# file: py3_knime_colorspace.yml with some modifications# THX Carsten Haubold (https://hub.knime.com/carstenhaubold) for hintsname: py3_knime_colorspace # Name of the created environmentchannels: # Repositories to search for packages# - defaults # edit: removed to just use conda-forge# - anaconda # edit: removed to just use conda-forge - conda-forge# https://anaconda.org/knime - knime # conda search knime-python-base -c knime --info # to see what is in the packagedependencies: # List of packages that should be installed- python=3.9 # Python- knime-python-base # dependencies of KNIME - Python integration# - knime-python-scripting # everything you need to also build Python packages for KNIME- cairo # SVG support- pillow # Image inputs/outputs- matplotlib # Plotting- IPython # Notebook support- nbformat # Notebook support- scipy # Notebook support- jpype1 # A Python to Java bridge# Jupyter Notebook support- jupyter # Jupyter Notebook- pandas-profiling # create overview of your data- sweetviz # In-depth EDA (target analysis, comparison, feature analysis, correlation) in two lines of code!- colormath- pip # Python installer- pip:# - JPype1 # Databases data.csvpy3_knime_colorspacecolor_data.tableCSV Reader Python Script Conda EnvironmentPropagation Table Writer

Nodes

Extensions

Links