Skip to content

Commit e53faa2

Browse files
authored
Add files via upload
1 parent 6f93527 commit e53faa2

3 files changed

Lines changed: 91 additions & 8 deletions

File tree

SQLiteImageHandler/SQLiteImageHandler.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sqlite3
22
import os
3+
import sys
4+
from typing import Tuple
35

46
class ImageHandler:
57

@@ -8,7 +10,7 @@ def __init__(self, databasePath : str = "database.db", tableName : str = "images
810
self.tableName = tableName
911
self.startConnection()
1012

11-
def startConnection(self):
13+
def startConnection(self) -> None:
1214
"""
1315
### Explanation
1416
Starts the connection with SQLite Database.
@@ -19,7 +21,7 @@ def startConnection(self):
1921
self.cursor.execute(f"CREATE TABLE IF NOT EXISTS {self.tableName} (Name TEXT, Data BLOB, Type TEXT)")
2022
self.connection.commit()
2123

22-
def imageSelector(self, path : str = None):
24+
def imageSelector(self, path : str = None) -> Tuple[bytes, str]:
2325
"""
2426
### Explanation
2527
Selects an image and returns the image's bytes content and extension type.
@@ -44,7 +46,7 @@ def imageSelector(self, path : str = None):
4446
extensionType = os.path.splitext(path)[1][1:]
4547
return bytesContent, extensionType
4648

47-
def addImage(self, imageName : str, imageBytes : bytes, extensionType : str = "png"):
49+
def addImage(self, imageName : str, imageBytes : bytes, extensionType : str = "png") -> None:
4850
"""
4951
### Explanation
5052
Adds an image to database.
@@ -66,7 +68,7 @@ def addImage(self, imageName : str, imageBytes : bytes, extensionType : str = "p
6668
except Exception as error:
6769
print(error)
6870

69-
def getSaveImage(self, imageName : str = None, savePath : str = "savedImage"):
71+
def getSaveImage(self, imageName : str = None, savePath : str = "savedImage") -> None:
7072
"""
7173
### Explanation
7274
Saves the previously saved image in the database as an image to the given path.
@@ -96,7 +98,9 @@ def getSaveImage(self, imageName : str = None, savePath : str = "savedImage"):
9698
except Exception as error:
9799
print(error)
98100

99-
def isImageExists(self, imageName : str = None):
101+
print(f"{imageName} saved successfully. Path:\n{savePath}")
102+
103+
def isImageExists(self, imageName : str = None) -> bool:
100104
"""
101105
### Explanation
102106
Checks if image exists in database by image name.
@@ -122,7 +126,7 @@ def isImageExists(self, imageName : str = None):
122126
except Exception as error:
123127
print("Error occurred in isImageExists func. Error: " + str(error))
124128

125-
def deleteImage(self, imageName : str = None):
129+
def deleteImage(self, imageName : str = None) -> None:
126130
"""
127131
### Explanation
128132
Deletes image by name in database.
@@ -146,7 +150,7 @@ def deleteImage(self, imageName : str = None):
146150
except Exception as error:
147151
print(error)
148152

149-
def updateImage(self, imageName : str = None, newImageBytes : bytes = None, newExtensionType : str = None):
153+
def updateImage(self, imageName : str = None, newImageBytes : bytes = None, newExtensionType : str = None) -> None:
150154
"""
151155
### Explanation
152156
Updates image by name in database.

SQLiteImageHandler/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
from SQLiteImageHandler.SQLiteImageHandler import ImageHandler
1+
from SQLiteImageHandler import ImageHandler
2+
3+
__version__ = "0.1.0"
4+
__version_info__ = (0, 1, 0)

SQLiteImageHandler/__main__.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from SQLiteImageHandler import ImageHandler
2+
import argparse
3+
from __init__ import __version__
4+
5+
parser = argparse.ArgumentParser(description="SQLite Image Handler.")
6+
parser.add_argument("-v", "--version", action="version", version=f'SQLiteImageHandler {__version__}')
7+
parser.add_argument("--database-path", help="database path.", dest="dbpath", type=str)
8+
parser.add_argument("--table-name", help="table name of the database", dest="tablename", type=str)
9+
10+
addGroup = parser.add_argument_group(title="Add an Image", description="Adding an image to the database.")
11+
addGroup.add_argument("-a", "--add-image", help="Adds an image to database. (Requires -sn and -ip)", action="store_true")
12+
addGroup.add_argument("-sn", "--save-name", help="Save name of image.", type=str)
13+
addGroup.add_argument("-ip", "--image-path", help="Path of the selected image", type=str)
14+
15+
saveGroup = parser.add_argument_group(title="Save an Image", description="Saving image from database to computer.")
16+
saveGroup.add_argument("-s", "--save-image", help="Saves the previously saved image in the database as an image to the given path. (Requires -sdn and -sp)", action="store_true")
17+
saveGroup.add_argument("-sdn", "--saved-name", help="Saved name of image in the database.", type=str)
18+
saveGroup.add_argument("-sp", "--save-path", help="Save path. (default: savedImage.png)", default="savedImage.png")
19+
20+
updateGroup = parser.add_argument_group(title="Update an Image", description="Updating the image in the database.")
21+
updateGroup.add_argument("-u", "--update-image", help="Updates image by name in database. (Requires -in and -uip)", action="store_true")
22+
updateGroup.add_argument("-in", "--image-name", help="Saved name of image in the database.", type=str)
23+
updateGroup.add_argument("-uip", "--update-image-path", help="Path of the selected image", type=str)
24+
25+
deleteGroup = parser.add_argument_group(title="Delete an Image", description="Deleting an image from database")
26+
deleteGroup.add_argument("-d", "--delete", help="Deletes image by name in database. (Requires -in)", action="store_true")
27+
28+
otherGroup = parser.add_argument_group(title="Other")
29+
otherGroup.add_argument("-c", "--check", help="Checks if image exists in database by image name. (Requires -in)", action="store_true")
30+
otherGroup.add_argument("-is", "--image-selector", help="Selects an image and returns the image's bytes length and extension type. (Requires -ip)", action="store_true")
31+
args = parser.parse_args()
32+
33+
if args.dbpath == None or args.tablename == None:
34+
parser.print_help()
35+
else:
36+
handler = ImageHandler(databasePath=args.dbpath, tableName=args.tablename)
37+
if args.add_image:
38+
if args.save_name != None and args.image_path != None:
39+
handler.addImage(args.save_name, *handler.imageSelector(path=args.image_path))
40+
else:
41+
print("You need to use -sn and -ip")
42+
43+
if args.save_image:
44+
if args.saved_name != None and args.save_path != None:
45+
saved_image = args.saved_name
46+
handler.getSaveImage(imageName = saved_image, savePath = args.save_path)
47+
else:
48+
print("You need to use -sdn and -sp")
49+
50+
if args.update_image:
51+
if args.image_name != None and args.update_image_path != None:
52+
name = args.image_name
53+
path = args.update_image_path
54+
handler.updateImage(name, *handler.imageSelector(path=path))
55+
else:
56+
print("You need you use -in and -uip")
57+
58+
if args.delete:
59+
if args.image_name != None:
60+
image_name = args.image_name
61+
handler.deleteImage(imageName=image_name)
62+
else:
63+
print("You need to use -in")
64+
65+
if args.check:
66+
if args.image_name != None:
67+
image_name = args.image_name
68+
print(handler.isImageExists(imageName=image_name))
69+
else:
70+
print("You need to use -in")
71+
72+
if args.image_selector:
73+
if args.image_path != None:
74+
path = args.image_path
75+
bytesOfImage, extensionType = handler.imageSelector(path=path)
76+
print("Bytes Length: " + str(len(bytesOfImage)) + "\nExtension Type: " + extensionType)

0 commit comments

Comments
 (0)