Skip to content

Commit

Permalink
draw image with blob data -2
Browse files Browse the repository at this point in the history
  • Loading branch information
vanguardmaster01 committed Sep 7, 2023
1 parent 24edbc1 commit 7a21f75
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 34 deletions.
Binary file modified DbFuncs/__pycache__/db.cpython-311.pyc
Binary file not shown.
Binary file modified DbFuncs/__pycache__/db_create.cpython-311.pyc
Binary file not shown.
33 changes: 13 additions & 20 deletions DbFuncs/db.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import sqlite3
import datetime
from config import utils

import os
import sys
# import os
# import sys

script_dir = os.path.dirname( __file__ )
constants_dir = os.path.join( script_dir, '..', 'config' )
sys.path.append(constants_dir)
import constants
# script_dir = os.path.dirname( __file__ )
# urils_dir = os.path.join( script_dir, '..', 'config' )
# sys.path.append(urils_dir)
# import utils

from model.Product import Product


# convert image to blob data
def convert_to_blod_data(filename):
with open(filename, 'rb') as file:
blobData = file.read()
return blobData


# insert into product table
def insert_product(product):
try:
conn = sqlite3.connect(constants.dbPath)
conn = sqlite3.connect(utils.dbPath)
cursor = conn.cursor()

insert_query = """insert into products (name, image, count, price, modifiedAt)
values (?, ?, ?, ?, ?)"""

imageBlob = convert_to_blod_data(product.image)
data = (product.name, imageBlob, product.count, product.price, datetime.datetime.now())
imageBlob = utils.convert_to_blod_data(product.image)
data = (product.name, (imageBlob), product.count, product.price, datetime.datetime.now())

cursor.execute(insert_query, data)

Expand All @@ -51,7 +44,7 @@ def insert_product(product):
# update product item
def update_product(id, count):
try:
conn = sqlite3.connect(constants.dbPath)
conn = sqlite3.connect(utils.dbPath)
cursor = conn.cursor()

update_query = '''update products set count = ? where id = ?'''
Expand All @@ -71,7 +64,7 @@ def update_product(id, count):
# get all products
def get_products():
try:
conn = sqlite3.connect(constants.dbPath)
conn = sqlite3.connect(utils.dbPath)
cursor = conn.cursor()

select_query = '''select id, name, image, count, price from products'''
Expand All @@ -94,7 +87,7 @@ def get_products():
# get product
def get_product(id):
try:
conn = sqlite3.connect(constants.dbPath)
conn = sqlite3.connect(utils.dbPath)
cursor = conn.cursor()

print(f'id: {id}')
Expand Down
10 changes: 5 additions & 5 deletions DbFuncs/db_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import sys

script_dir = os.path.dirname( __file__ )
constants_dir = os.path.join( script_dir, '..', 'config' )
sys.path.append(constants_dir)
import constants
utils_dir = os.path.join( script_dir, '..', 'config' )
sys.path.append(utils_dir)
import utils


# create product table
def create_table_if_not_exists():
try:
conn = sqlite3.connect(constants.dbPath)
conn = sqlite3.connect(utils.dbPath)
cursor = conn.cursor()

create_product_table_query = '''CREATE TABLE IF NOT EXISTS products (
Expand Down Expand Up @@ -41,7 +41,7 @@ def create_table_if_not_exists():
def insertData(name, avatar, modifiedAt):
try:
# Connect to the database (create a new file if it doesn't exist)
conn = sqlite3.connect(constants.dbPath)
conn = sqlite3.connect(utils.dbPath)

# Create a cursor object to execute SQL commands
cursor = conn.cursor()
Expand Down
Binary file added config/__pycache__/utils.cpython-311.pyc
Binary file not shown.
1 change: 0 additions & 1 deletion config/constants.py

This file was deleted.

19 changes: 19 additions & 0 deletions config/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dbPath = './DbFuncs/sql.db'

# convert image to blob data
def convert_to_blod_data(filename):
with open(filename, 'rb') as file:
blobData = file.read()
return blobData

# write
def write_to_file(data, filename):
# Convert binary data to proper format and write it on Hard Disk
with open(filename, 'wb') as file:
file.write(data)
print("Stored blob data into: ", filename, "\n")



# path = 'D:\\tmp\\' + product[1] + '.png'
# utils.write_to_file(product[2], path)
35 changes: 27 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.core.image import Image as CoreImage
from kivy.clock import Clock
from kivy.graphics.texture import Texture
# from kivy.graphics.texture import Texture
from kivy.properties import StringProperty
from PIL import Image as PILImage
import io

from DbFuncs import db
from DbFuncs import db_create
from model.Product import Product

from config import utils

dbFlag = False

class ListScreen(Screen):
Expand Down Expand Up @@ -52,15 +57,29 @@ def on_image_click(self, instance, touch):

# draw one image
def on_draw_item(self, product):
image = Image()
image.size = (100, 100)
# image = Image()
# image.size = (100, 100)

# image.allow_stretch = True
# image.keep_ratio = False
# image.source = 'D:\Workspace\Python\Kivy\Hello-kivy\img/1-1.png'
texture = Texture.create(size=(image.width, image.height))
texture.blit_buffer(product[2], colorfmt='rgba', bufferfmt='ubyte')
image.texture = texture
image.color = [1,0,0,1]

# texture = Texture.create(size=(image.width, image.height))
# texture.blit_buffer(product[2], colorfmt='rgba', bufferfmt='ubyte')
# image.texture = texture

# pil_image = PILImage.open(io.BytesIO(product[2]))
# texture = pil_image.tobytes()

# image_stream = BytesIO(product[2])
# loader = ImageLoader()
# texture = loader.load(image_stream.getvalue(), ext="png")

image = Image()
image_stream = io.BytesIO(product[2])
img = CoreImage(image_stream, ext='png')
image.texture = img.texture

image.bind(on_touch_down=self.on_image_click)

return image
Expand Down Expand Up @@ -111,7 +130,7 @@ def insertProduct(self):
for name in names:
image = path + "/" + name
data = Product('111', image, 10, 100)
db.insertProduct(data)
db.insert_product(data)



Expand Down

0 comments on commit 7a21f75

Please sign in to comment.