Home > OS >  How to encode/decode an image to save it for later?
How to encode/decode an image to save it for later?

Time:12-23

Right now I can upload a picture and then put it in the same Tkinter site, but can not put it on a newly created site even though I use the same variable and only change pic = Label(root, image=img) to pic = Label(table, image=img). It shows the Image in the root site but not on the newly opened table site. I always get an error. Here is my code:

global df_main

root = Tk()
root.title("Fabric Database")
root.geometry("400x400")
df_main = pd.read_csv("C:/Users/noahk/Documents/Trial.csv")

print(df_main)

def upload_file():
    global converted_string
    file = filedialog.askopenfilename()
    #with open(file, "rb") as image2string:
    converted_string = base64.b64encode(open(file, "rb").read())

def submit():
    global df_main
    df_main = df_main.append({'Name': str(name.get()), 'Country': str(country.get()), 'Price': str(price.get()), 'Thickness':str(thickness.get()), 'Fabric Example': str(converted_string)}, ignore_index = True)    
    #query_label = Label(root, text=df_main)
    #query_label.grid(row=12,column=1)
    
def show_pic():
    global img
    b = base64.b64decode(converted_string)
    img = Image.open(io.BytesIO(b))
     
    # resize the image and apply a high-quality down sampling filter
    img = img.resize((20, 20), Image.ANTIALIAS)
 
    # PhotoImage class is used to add image to widgets, icons etc
    img = ImageTk.PhotoImage(img)
    pic = Label(root, image=img)
    pic.grid(row=1,column=5)

def show_table():
    global b
    global img
    global pic
    table = Tk()
    table.title("Fabric Database")
    table.geometry("400x400")
    b = base64.b64decode(converted_string)
    img = Image.open(io.BytesIO(b))
     
    # resize the image and apply a high-quality down sampling filter
    img = img.resize((20, 20), Image.ANTIALIAS)
 
    # PhotoImage class is used to add image to widgets, icons etc
    img = ImageTk.PhotoImage(img)
    pic = Label(table, image=img)
    pic.grid(row=1,column=5)
    
    table_name = Label(table, text="Name")
    table_name.grid(row=0,column=0)
    table_Country = Label(table, text="Country")
    table_Country.grid(row=0,column=1)
    table_Price = Label(table, text="Price")
    table_Price.grid(row=0,column=2)
    table_Thickness = Label(table, text="Thickness")
    table_Thickness.grid(row=0,column=3)
    table_Fabric = Label(table, text="Fabric Example")
    table_Fabric.grid(row=0,column=4)
    
    df_name_label = Label(table, text=df_main["Name"])
    df_name_label.grid(row=1,column=0)
    df_Countrylabel = Label(table, text=df_main["Country"])
    df_Countrylabel.grid(row=1,column=1)
    df_Pricelabel = Label(table, text=df_main["Price"])
    df_Pricelabel.grid(row=1,column=2)
    df_Thicknesslabel = Label(table, text=df_main["Thickness"])
    df_Thicknesslabel.grid(row=1,column=3)

def search():
    search = Tk()
    search.title("Fabric Database")
    search.geometry("400x400")
    
    global name_table
    global country_table
    global price_table
    global thickness_table
    global fabric_example_table
    
    name_table = Entry(search, width=30)
    name_table.grid(row=2, column=0)
    country_table = Entry(search, width=30)
    country_table.grid(row=2, column=1)
    price_table = Entry(search, width=30)
    price_table.grid(row=2, column=2)
    thickness_table = Entry(search, width=30)
    thickness_table.grid(row=2, column=3)
    fabric_example_table = Entry(search, width=30)
    fabric_example_table.grid(row=2, column=4)
    name_label_table = Label(search, text="Name")
    name_label_table.grid(row=1, column=0)
    country_label_table = Label(search, text="Country")
    country_label_table.grid(row=1, column=1)
    price_label_table = Label(search, text="Price")
    price_label_table.grid(row=1, column=2)
    thickness_label_table = Label(search, text="Thickness")
    thickness_label_table.grid(row=1, column=3)
    fabric_example_label_table = Label(search, text="Fabric example")
    fabric_example_label_table.grid(row=1, column=4)
    
    global search_btn
    search_btn = Button(search, text="Search", command=search_table)
    search_btn.grid(row=3,column=0,columnspan=2,pady=10,padx=10,ipadx=120)

def search_table():
    stdf = df[(df["Name"]==name_table) & (df["Country"]==country_table) & (df["Price"]==price_table)&(df["Thickness"]==thickness_table)&(df["Fabric Example"]==fabric_example_table)]
    stdf_label = Label(search, text=stdf)
    stdf_label.grid(row=12,column=1)

global name
global country
global price
global thickness
global fabric_example

name = Entry(root, width=30)
name.grid(row=0, column=1, padx=20, pady=(10,0))
country = Entry(root, width=30)
country.grid(row=1, column=1)
price = Entry(root, width=30)
price.grid(row=2, column=1)
thickness = Entry(root, width=30)
thickness.grid(row=3, column=1)
fabric_example = tk.Button(root, text='Upload Files', 
   width=20,command = lambda:upload_file())
fabric_example.grid(row=4, column=1)

show_btn = Button(root, text="Press", command=show_pic)
show_btn.grid(row=7,column=2)

#create text box labels
name_label = Label(root, text="Name")
name_label.grid(row=0, column=0, pady=(10,0))
country_label = Label(root, text="Country")
country_label.grid(row=1, column=0)
price_label = Label(root, text="Price")
price_label.grid(row=2, column=0)
thickness_label = Label(root, text="Thickness")
thickness_label.grid(row=3, column=0)
fabric_example_label = Label(root, text="Fabric example")
fabric_example_label.grid(row=4, column=0)

delete_box = Entry(root, width=30)
delete_box.grid(row=9,column=1,pady=5)
delete_box_label=Label(root, text="Select Row ID")
delete_box_label.grid(row=9,column=0, pady=5)

#Create Submit Button

submit_btn = Button(root, text="Add to Database", command=submit)
submit_btn.grid(row=5,column=0,columnspan=2,pady=10,padx=10,ipadx=105)

query_btn = Button(root, text="Show Table", command=show_table)
query_btn.grid(row=7,column=0,columnspan=2,pady=10,padx=10,ipadx=120)
'''
delete_btn = Button(root, text="Delete Column", command=delete)
delete_btn.grid(row=10,column=0,columnspan=2,pady=10,padx=10,ipadx=120)

update_btn = Button(root, text="Edit Table", command=edit)
update_btn.grid(row=11,column=0,columnspan=2,pady=10,padx=10,ipadx=120)
'''

search_btn = Button(root, text="Search Databank", command=search)
search_btn.grid(row=12,column=0,columnspan=2,pady=10,padx=10,ipadx=120)

root.mainloop()

The error I'm getting is pyimagexy does not exist and I don't know why. My goal is to encode an image so that I can store the raw string version of that image somewhere, and when I need it again I can decode it back into an image and display it. But, right now I can only upload it and instantly display it.

I tried to take it apart in every way possible but it just does not work. I can't store it anywhere and display it somewhere else.

CodePudding user response:

As it turns out, the fix for your problem seems to be fairly simple. The problem is caused by using table = Tk() in your table function. Two main Tk windows are not allowed to run in the same thread, according to the docs. Therefore the fix should just be to change this line to table = Toplevel(). This creates a new window without causing the conflicts that a new Tk() will.

Also, please include your imports in your code next time, as it really helps to reproduce your environment, especially when you are using external modules like Pillow which has similar class names to tkinter.

  • Related