Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Image Storing App in Python By Ayan
- # ~ First step is going to be creating database and table
- # ~ Inserting images in our database
- # ~ To insert an image in database we need to convert the image in binary
- # ~ Step is to show files that has been added
- # ~ Final Step Retrive image file
- # ~ Contact : ayanU881@gmail.com
- import sqlite3
- from tabulate import tabulate
- conn=sqlite3.connect('imagedb.db')
- cursor=conn.cursor()
- def createtable():
- try:
- command="CREATE TABLE objectfiles(photoname TEXT,image BLOB);"
- cursor.execute(command)
- print("database and table created successfully")
- except:
- print("You already have a safe")
- def convertToBinary(filename):
- with open(filename,'rb') as f:
- data= f.read()
- return data
- def insertImage():
- filename=input("Enter your photoname:")
- params=(filename,convertToBinary(filename))
- cursor.execute("insert into objectfiles values(?,?)",params)
- conn.commit()
- print("Image inserted")
- def showImage():
- command="select rowid,photoname from objectfiles"
- cursor.execute(command)
- result=cursor.fetchall()
- print(tabulate(result))
- def retriveImage():
- id=input('Enter Image Row id:')
- photoname=input('Enter photoname:')
- command=f"select image from objectfiles where rowid={id};"
- rows=cursor.execute(command)
- for row in rows:
- writeImage(photoname,row[0])
- def writeImage(filename,blobdata):
- with open(filename,'wb') as file:
- file.write(blobdata)
- print('Success!')
- createtable()
- run=True
- # option='' ~ you actually don't need this
- while run:
- print("What would you like to do today?")
- print("Press i to insert image ")
- print("Press s to show files")
- print("Press r to retrive image")
- print("Press q to quit application")
- option=input('>')
- if option=='i':
- insertImage()
- if option=='s':
- showImage()
- if option=='r':
- retriveImage()
- if option=='q':
- run=False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement