I have saved an image in mysql and I want to show it but an error occurred and I would like you to help me know where the error originates from.
def convertToBinaryData(self,filename):
#convert digital data to bynary format
with open(filename,'rb') as file:
binaryData = file.read()
return binaryData
def upload image (self):
self.btn_Frame.filename = filedialog.askopenfilename(initialdir="C:/Users/dimitri/Pictures",title="seleccione el dibujo",filetypes=[("PNG files","*.png"),("JPG files","*.jpg")])
self.my_image = Image.open(self.btn_Frame.filename)
self.blobImage = self.convertToBinaryData(self.btn_Frame.filename)
self.resize_image = self.my_image.resize((200,200),Image.ANTIALIAS)
self.new_image = ImageTk.PhotoImage(self.resize_image)
foto= Label(self.Predecir_Frame,image=self.new_image)
foto.photo = self.new_image
foto.grid(row=1,column=0,pady=10,padx=30,sticky=W)
def save_DB(self):
if self.name_var.get()=="" or self.lastName_var.get()==""
messagebox.showerror("Error", "All fields are required !!!")
else:
con = pymysql.connect(host="localhost", user="root",password="", database="postulantebd")
cur = con.cursor()
cur.execute("insert into postulantes values(%s, %s, %s, %s)",(
self.name_var.get(),
self.lastName_var.get(),
self.txt_address.get('1.0', 'end-1c'),
self.blobImage
))
con.commit()
self.v2()
self.fetch_all()
con.close()
def fetch_all(self):
con = pymysql.connect(host="localhost", user="root",password="", database="postulantebd")
cur = con.cursor()
cur.execute("select * from postulantes")
rows=cur.fetchall()
if len(rows)!=0:
self.tabla_postulante.delete(*self.tabla_postulante.get_children())
for row in rows:
self.tabla_postulante.insert('',END,values=row)
con.commit()
con.close()
def get_cursor(self,ev):
cursor_row=self.tabla_postulante.focus()
contents=self.tabla_postulante.item(cursor_row)
row=contents['values']
self.name_var.set(row[0]),
self.lastName_var.set(row[1]),
self.txt_address.delete('1.0', 'end-1c')
self.txt_address.insert('end-1c',row[2]),
self.blobImagen = row[3]
readBlob=Image.open(io.BytesIO(self.blobImage))
pic = ImageTk.PhotoImage(readBlob)
foto = Label(self.Predecir_Frame,image=pic)
foto.grid(row=1,column=0,pady=10,padx=30,sticky=W)
the error that appears is the following: in get_cursor readBlob=Image.open(io.BytesIO(self.blobImagen)) TypeError: a bytes-like object is required, not 'str'
I know it's long but I didn't know how to make a shorter example thanks for taking the time to read
CREATE TABLEstatement, I am working with mysql and phpmyadmin and it is a table of 10 columns in which 9 of those columns are varchar type and the last column is longblob type. I checked in phpmyadmin and the file is stored in binary.