Icon

DB SQL - SQLite - store and read JPEG Files in databases - also using Python

SQLite - store and read JPEG Files in databases - also using Python

In KNIME there does not seem to be a direct way to store JPG/JPEG files in SQLite. But you can use the bundled Python to load files into a SQLite database.

URL: python graphics examples from Kaggle used https://www.kaggle.com/xuhewen/iris-dataset-visualization-and-machine-learning
URL: Meta Collection about KNIME and Python https://hub.knime.com/mlauber71/spaces/Public/latest/_knime_and_python_meta_collection~AvjrddXKOIoZYLV3
URL: Medium: Exploring the Power of Python Graphics with KNIME: A Collection of Examples https://medium.com/p/841df87b5563
URL: more SQLite examples https://hub.knime.com/search?type=Workflow&tag=SQLite,sql&sort=best
URL: Hub: KNIME, Python and SQL with local SQLite database https://hub.knime.com/-/spaces/-/~JvzzN5leAp1EgLLU/current-state/
URL: Medium: KNIME, Databases and SQL https://medium.com/p/273e27c9702a
URL: KNIME 4: SQLite - store and read JPEG Files in databases - also using Python https://hub.knime.com/-/spaces/-/~Agi73PoewIwmFDwA/current-state/
URL: KNIME Forum (80717) https://forum.knime.com/t/export-photos-from-sql-table-to-local-disk/80717/4?u=mlauber71

SQLite - store and read JPEG Files in databases - also using Python

https://forum.knime.com/t/export-photos-from-sql-table-to-local-disk/80717/4?u=mlauber71

Exploring the Power of Python Graphics with KNIME: A Collection of Examples

https://medium.com/p/841df87b5563

KNIME, Databases and SQL

https://medium.com/p/273e27c9702a

import knime.scripting.io as knio

# This example script creates an output table containing randomly drawn integers using numpy and pandas.

import numpy as np
import pandas as pd

import sqlite3
import os
from PIL import Image
import io

# Connect to SQLite database (it will create the database file if it does not exist)
conn = sqlite3.connect(knio.flow_variables['Path_SQLite_location'])
cursor = conn.cursor()

# Variable for the table name
table_name = 'images_01'

# SQL command with variable table name
sql_command = f'''
CREATE TABLE IF NOT EXISTS {table_name} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image BLOB
)
'''
try:
cursor.execute(sql_command)
except sqlite3.OperationalError as e:
print(f"Error creating table: {e}")

# Function to convert image to binary data
def convert_image_to_blob(image_path):
with Image.open(image_path) as image:
blob = io.BytesIO()
image.save(blob, 'JPEG')
return blob.getvalue()

# Directory containing images
image_directory = knio.flow_variables['context.workflow.data-path']

# List to hold filenames
filenames = []

# Loop through each file in the directory
for filename in os.listdir(image_directory):
if filename.endswith('.jpg'):
filenames.append(filename)
image_path = os.path.join(image_directory, filename)
image_blob = convert_image_to_blob(image_path)
# Insert image into the database using a dynamic table name
insert_sql = f'INSERT INTO {table_name} (image) VALUES (?)'
cursor.execute(insert_sql, (image_blob,))

# Commit changes and close connection
conn.commit()
conn.close()

# Create a DataFrame from the list of filenames
df_filenames = pd.DataFrame(filenames, columns=['Filename'])


knio.output_tables[0] = knio.Table.from_pandas(df_filenames)
locate and create /data/ folder with absolute paths
Collect Local Metadata
export JPG files to disc
Binary Objects to Files
Path_Export_JPG_01
Create File/Folder Variables
Path_SQLite/data/
Create File/Folder Variables
Concatenate
knime://knime.workflow/data/my_database.sqlite
SQLite Connector
Path_SQLite
Path to String (Variable)
images_01see advanced settingsto convert BLOB to Image (Binary Object)
DB Table Selector
write jpg files to disk /data/
Image Writer
insert jpg images from path into SQLite database
Python Script
Create JPG images
images_01 bring binary images (jpg) back from SQLite
DB Reader
Column Renamer
Iris Dataset
Example Data Reader

Nodes

Extensions

Links