为什么在插入 sqlite3 后在 tkinter window 上显示图片时出现该错误?
why I get that error when displaying picture on tkinter window after inserting into sqlite3?
我创建了一个 tkinter 来显示来自 PC 的图片,然后插入到 sqlite3 文件中,我的问题是在尝试显示图片时出现错误“无法识别图像文件 <_io.BytesIO object at 0x05B60F28>”从 sqlite 插入后,这是我的代码示例:
def createWidgets(self):
notebook = ttk.Notebook(root)
self.Tab_Control1 = Frame(notebook)
self.Tab_Control2 = Frame(notebook)
notebook.add(self.Tab_Control1, text="Enter")
notebook.add(self.Tab_Control2, text="Show")
notebook.pack(fill='both', expand=1)
notebook.select(self.Tab_Control1)
def Insert():
global img
global folder_path
folder_path = StringVar()
filename = filedialog.askopenfilename(initialdir="/", title="Select An Image", filetypes=(("jpeg files", "*.jpg"), ("gif files", "*.gif*"), ("png files", "*.png")))
folder_path.set(filename)
try:
print(filename)
if not filename == '':
self.Tab_Control2.destroy()
self.Tab_Control2 = Frame(notebook)
notebook.add(self.Tab_Control2, text="Show")
notebook.pack(fill='both', expand=1)
notebook.select(self.Tab_Control2)
img = np.array(ImageTk.PhotoImage(Image.open(filename)))
panel = Label(self.Tab_Control2)
panel.config (image=img)
panel.pack(side="bottom", fill="both", expand="yes")
ASK = messagebox.askquestion("HR", "Are you wanting to insert picture into your database ?", icon='warning')
if ASK == 'yes':
conn = sqlite3.connect('datastorage.db')
c = conn.cursor()
img_str = cv2.imread(filename)
c.executescript('drop table if exists Structure1;')
c.execute("create table if not exists Structure1 (img blob)")
c.execute("insert into Structure1 values (?)", (img_str,))
conn.commit()
conn.close()
messagebox.showinfo("HR", "Done")
except:
messagebox.showinfo("HR", "Error, try again")
def Display():
global img
notebook.select(self.Tab_Control2)
conn = sqlite3.connect('datastorage.db')
c = conn.cursor()
filename = "SELECT * from Structure1"
c.execute(filename)
record = c.fetchall()
for row in record:
print(row)
photo=row[0]
fp = io.BytesIO(photo)
image = Image.open(fp)
img = ImageTk.PhotoImage(image)
panel = Label(self.Tab_Control2, image=img)
panel.image = img
panel.pack(side="bottom", fill="both", expand="yes")
conn.commit()
conn.close()
self.insert = Button(self.Tab_Control1, font='Helvetica 14 bold', width=15, text="Insert picture", fg='red', bg='light goldenrod yellow', command=Insert)
self.insert.pack()
self.display = Button(self.Tab_Control1, font='Helvetica 14 bold', width=15, text="Display picture", fg='red', bg='light goldenrod yellow', command=Display)
self.display.pack()
您应该使用二进制文件读取而不是 cv2.imread()
:
...
#img_str = cv2.imread(filename)
with open(filename, "rb") as f:
img_str = f.read()
c.execute("INSERT INTO Structure1 VALUES (?)", (img_str,))
...
我创建了一个 tkinter 来显示来自 PC 的图片,然后插入到 sqlite3 文件中,我的问题是在尝试显示图片时出现错误“无法识别图像文件 <_io.BytesIO object at 0x05B60F28>”从 sqlite 插入后,这是我的代码示例:
def createWidgets(self):
notebook = ttk.Notebook(root)
self.Tab_Control1 = Frame(notebook)
self.Tab_Control2 = Frame(notebook)
notebook.add(self.Tab_Control1, text="Enter")
notebook.add(self.Tab_Control2, text="Show")
notebook.pack(fill='both', expand=1)
notebook.select(self.Tab_Control1)
def Insert():
global img
global folder_path
folder_path = StringVar()
filename = filedialog.askopenfilename(initialdir="/", title="Select An Image", filetypes=(("jpeg files", "*.jpg"), ("gif files", "*.gif*"), ("png files", "*.png")))
folder_path.set(filename)
try:
print(filename)
if not filename == '':
self.Tab_Control2.destroy()
self.Tab_Control2 = Frame(notebook)
notebook.add(self.Tab_Control2, text="Show")
notebook.pack(fill='both', expand=1)
notebook.select(self.Tab_Control2)
img = np.array(ImageTk.PhotoImage(Image.open(filename)))
panel = Label(self.Tab_Control2)
panel.config (image=img)
panel.pack(side="bottom", fill="both", expand="yes")
ASK = messagebox.askquestion("HR", "Are you wanting to insert picture into your database ?", icon='warning')
if ASK == 'yes':
conn = sqlite3.connect('datastorage.db')
c = conn.cursor()
img_str = cv2.imread(filename)
c.executescript('drop table if exists Structure1;')
c.execute("create table if not exists Structure1 (img blob)")
c.execute("insert into Structure1 values (?)", (img_str,))
conn.commit()
conn.close()
messagebox.showinfo("HR", "Done")
except:
messagebox.showinfo("HR", "Error, try again")
def Display():
global img
notebook.select(self.Tab_Control2)
conn = sqlite3.connect('datastorage.db')
c = conn.cursor()
filename = "SELECT * from Structure1"
c.execute(filename)
record = c.fetchall()
for row in record:
print(row)
photo=row[0]
fp = io.BytesIO(photo)
image = Image.open(fp)
img = ImageTk.PhotoImage(image)
panel = Label(self.Tab_Control2, image=img)
panel.image = img
panel.pack(side="bottom", fill="both", expand="yes")
conn.commit()
conn.close()
self.insert = Button(self.Tab_Control1, font='Helvetica 14 bold', width=15, text="Insert picture", fg='red', bg='light goldenrod yellow', command=Insert)
self.insert.pack()
self.display = Button(self.Tab_Control1, font='Helvetica 14 bold', width=15, text="Display picture", fg='red', bg='light goldenrod yellow', command=Display)
self.display.pack()
您应该使用二进制文件读取而不是 cv2.imread()
:
...
#img_str = cv2.imread(filename)
with open(filename, "rb") as f:
img_str = f.read()
c.execute("INSERT INTO Structure1 VALUES (?)", (img_str,))
...