Icon

kn_​example_​python_​image_​sqlite

store images in a SQLite database from within Python and read the images back into KNIME

store images in a SQLite database from within Python and read the images back into KNIME


store images in a SQLite database from within Python and read the images back into KNIME import knime.scripting.io as knioimport numpy as npimport pandas as pdvar_data_path = knio.flow_variables['context.workflow.data-path']import sqlite3from PIL import Image# Open the PNG file as binary datawith open(knio.flow_variables['context.workflow.data-path'] + 'sample_bar_chart.png', "rb") as f: img_data = f.read()# Connect to the SQLite databaseconn = sqlite3.connect(knio.flow_variables['context.workflow.data-path'] + "mydatabase.sqlite")# Check if the table already existstable_exists = conn.execute("SELECT name FROM sqlite_master WHERE type='table' ANDname='images'").fetchone()if table_exists: # Append the image data to the existing table conn.execute("INSERT INTO images (data) VALUES (?)", [sqlite3.Binary(img_data)])else: # Create a table to store the image conn.execute('''CREATE TABLE images (id INTEGER PRIMARY KEY, data BLOB)''') # Insert the image into the table conn.execute("INSERT INTO images (data) VALUES (?)", [sqlite3.Binary(img_data)])# Commit the changes and close the connectionconn.commit()conn.close() locate and create/data/ folderwith absolute pathsexport image to localSQLite database fromwithin Pythonfig.savefig(knio.flow_variables['context.workflow.data-path'] + 'sample_bar_chart.png')knime://knime.workflow/data/mydatabase.sqliteimagesread the image data fromSQLite databaseexport imagesto /data/ folderread first image to KNIME Collect LocalMetadata Python Script Python Script SQLite Connector DB Table Selector DB Reader Image Writer(Table) Table To Image store images in a SQLite database from within Python and read the images back into KNIME import knime.scripting.io as knioimport numpy as npimport pandas as pdvar_data_path = knio.flow_variables['context.workflow.data-path']import sqlite3from PIL import Image# Open the PNG file as binary datawith open(knio.flow_variables['context.workflow.data-path'] + 'sample_bar_chart.png', "rb") as f: img_data = f.read()# Connect to the SQLite databaseconn = sqlite3.connect(knio.flow_variables['context.workflow.data-path'] + "mydatabase.sqlite")# Check if the table already existstable_exists = conn.execute("SELECT name FROM sqlite_master WHERE type='table' ANDname='images'").fetchone()if table_exists: # Append the image data to the existing table conn.execute("INSERT INTO images (data) VALUES (?)", [sqlite3.Binary(img_data)])else: # Create a table to store the image conn.execute('''CREATE TABLE images (id INTEGER PRIMARY KEY, data BLOB)''') # Insert the image into the table conn.execute("INSERT INTO images (data) VALUES (?)", [sqlite3.Binary(img_data)])# Commit the changes and close the connectionconn.commit()conn.close() locate and create/data/ folderwith absolute pathsexport image to localSQLite database fromwithin Pythonfig.savefig(knio.flow_variables['context.workflow.data-path'] + 'sample_bar_chart.png')knime://knime.workflow/data/mydatabase.sqliteimagesread the image data fromSQLite databaseexport imagesto /data/ folderread first image to KNIMECollect LocalMetadata Python Script Python Script SQLite Connector DB Table Selector DB Reader Image Writer(Table) Table To Image

Nodes

Extensions

Links