import urllib, io
from Tkinter import *
from PIL import Image, ImageTk
from bs4 import BeautifulSoup
import Tkinter as tk
root = Tk()
root.geometry("1000x800+20+20")
im=[]
t1=[]
z1=[]
x=var=0
def Search():
global t1
global var
var = IntVar()
show = "http://blog.acorn-is.com/wp-content/uploads/apple-full2.jpg" #random google image
fd = urllib.urlopen(show)
imgFile = io.BytesIO(fd.read())
im.append (ImageTk.PhotoImage(Image.open(imgFile)))
image = Label(root, image = im[0])
t1.append(image)
z1.append(image)
#both t1 and z1 should contain the image..
r1 = Checkbutton(root, variable=var, command=Queue)
r1.place(bordermode=INSIDE, x=0, y=90)
def Queue():
if (var.get() == 1):
z1[0].place(bordermode=INSIDE, x=0, y=300) #x=0
t1[0].place(bordermode=INSIDE, x=550, y=300) #x=550
tx1 = StringVar()
b1 = Button(root, text="Search", command=Search, width="20")
b1.grid(row=0, column=0)
root.mainloop()
example code above. you'll notice in the Search function, that both t1 and z1 are appending the image to their own list but when i "place" z1 and t1, the image shows only once - only the second image shows. how can i get it to display the same image twice?
See the changes below. This will make it work !
import urllib, io
from Tkinter import *
from PIL import Image, ImageTk
from bs4 import BeautifulSoup
import Tkinter as tk
root = Tk()
root.geometry("1000x800+20+20")
im=[]
t1=[]
z1=[]
x=var=0
def Search():
global t1
global var
var = IntVar()
show = "http://blog.acorn-is.com/wp-content/uploads/apple-full2.jpg" #random google image
fd = urllib.urlopen(show)
imgFile = io.BytesIO(fd.read())
im.append (ImageTk.PhotoImage(Image.open(imgFile)))
image = Label(root, image = im[0])
t1.append(image)
# New added line
image = Label(root, image = im[0])
z1.append(image)
#both t1 and z1 should contain the image..
r1 = Checkbutton(root, variable=var, command=Queue)
r1.place(bordermode=INSIDE, x=0, y=90)
def Queue():
if (var.get() == 1):
z1[0].place(bordermode=INSIDE, x=0, y=300) #x=0
t1[0].place(bordermode=INSIDE, x=550, y=300) #x=550
tx1 = StringVar()
b1 = Button(root, text="Search", command=Search, width="20")
b1.grid(row=0, column=0)
root.mainloop()
Related
I am currently working on building an UI to show the images in my JupyterLab to my Tkinter UI, that is running on a same script. In my script, I hope that after entering the values in Tkinter, it will take in the input and run again, and show the image on Tkinter UI again, so that I can do try and error. Can anyone guide me or give me a little tips to know where to find the answer and how to work on it?
from tk import *
from tkinter import ttk
from PIL import ImageTk, Image
import tkinter as tk
import os
window = tk.Tk()
def show_result(a,b,c,d):
#display the image result
#run the again to test the result
x1 = tk.IntVar()
x2 = tk.IntVar()
y1 = tk.IntVar()
y2 = tk.IntVar()
# set textbox to capture variables
x1_value = ttk.Entry(textvariable=x1).place(x=50, y=50)
x2_value = ttk.Entry(textvariable=x2).place(x=50, y=100)
y1_value = ttk.Entry(textvariable=y1).place(x=50, y=150)
y2_value = ttk.Entry(textvariable=y2).place(x=50, y=200)
display_button = ttk.Button(text="Run", command= lambda: show_result(x1.get(),x2.get(),y1.get(),y2.get())).place(x=50, y=300)
window.geometry("900x750")
window.mainloop( )
plt.figure(figsize = (50,8))
plt.imshow(crop_img, cmap='gray')
fig1 = plt.gcf()
fig1.savefig('crop_img.png', dpi=100)
open_img()
# Open img to show on tkinter
def open_img():
x = 'crop_img.png'
img = Image.open(x)
left=2000
top=0
right=3000
bottom=800
img = img.crop((left, top, right, bottom))
img = img.resize((800, 600), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
panel = Label(window, image=img)
panel.image = img
panel.place(x=200, y=0)
I have just started utilizing Python's tkinter module to create some basic GUIs. In the GUI shown below, the user is prompted to upload the cafe image and subsequent cafe name will appear. However, I have not found a convenient way to clear the cafe label text for when the user upload another cafe image and classify again. I have the full code attached below.
import numpy as np
import cv2
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image
from h5py import File
def upload():
global img
global filename
f_types = [('Png files', '*.png')]
filename = filedialog.askopenfilename(filetype=f_types)
img = ImageTk.PhotoImage(file = filename)
e1 = tk.Label(window)
e1.place(x=50, y=150)
e1['image'] = img
window.configure(background = '#CDCDCD')
show_classify_button()
def show_classify_button():
classify_btn = tk.Button(window, text='Classify Image', command=classification)
classify_btn.place(x=250, y=400)
classify_btn.configure(background = '#364156', foreground = 'white', font=('cambria', 15, 'bold') )
def classification():
matrix = []
image = cv2.imread(filename, 0)
array = np.array(image)
flatten_array = array.flatten()
matrix.append(flatten_array)
X = pca.transform(matrix)
pred = clf.predict(X)
if pred == 0:
Label(window, text='Casa De Cafe', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=170, y=300)
elif pred == 1:
Label(window, text='Coffeology', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=170, y=300)
else:
Label(window, text='Mori Cafe', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=170, y=300)
def main_account_screen():
global window
window = Tk()
window.title('Cafe Recognition System')
window.geometry('500x500')
window.configure(background = '#CDCDCD')
Label(window, text='Cafe Recognition System', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=110, y=50)
button = tk.Button(window, text='Upload', command=upload)
button.place(x=130, y=400)
button.configure(background = '#364156', foreground = 'white', font=('cambria', 15, 'bold') )
window.mainloop()
main_account_screen()
I'm trying to do an image search engine with Tkinter. Basically a "Pokedex" that searches for images inside a specific folder on my computer associated with a specific name.Example of the image output
import tkinter as tk
from io import BytesIO
import matplotlib.pyplot as plt
import os
import glob
import natsort
from PIL import Image
from PIL import ImageTk
window = tk.Tk()
window.geometry("600x500")
window.title("CoreaninhaDex")
window.config(padx=10, pady=10)
title_label = tk.Label(window, text = 'CoreaninhaDex')
title_label.config(font=('Arial', 32))
title_label.pack(padx=10, pady=10)
target_image = tk.Label(window)
target_image.pack(padx=10, pady=10)
target_information = tk.Label(window)
target_information.config(font=("Arial", 20))
target_information.pack(padx=10, pady=10)
photos_names_list = ["target1","target2","target3","target4","target5"]
#FUNCTION
def showimage():
dir1 = r"C:\Users\path"
path1 = os.path.join(dir1, '*g')
files = glob.glob(path1)
files1 = natsort.natsorted(files, reverse=False)
for x in files1:
img = plt.imread(x)
image = Image.open(img)
imag = ImageTk.PhotoImage(image)
target_image.config(image=imag)
target_image.image = imag
label_id_name = tk.Label(window, text="ID or Name")
label_id_name.config(font=("Arial", 20))
label_id_name.pack(padx=10, pady=10)
text_id_name = tk.Text(window, height = 1)
text_id_name.config(font=("Arial", 20))
text_id_name.pack(padx=10, pady=10)
btn_load = tk.Button(window, text="Load target", command = showimage)
btn_load.config(font=("Arial", 20))
btn_load.pack(padx=10, pady=10)
window.mainloop()
The output generates a display. But I can't associate the image with any name or even upload the images.
I would like help to write a showimage () function that would do this.
Guys I wanted to know how to save a png file to a specific path through tkinter I have a button and when I click in it I want it to save a specific png file in a path that I write in the entry u can look at my code here
from tkinter import *
from tkinter import filedialog
from tkinter import*
from tkinter.ttk import *
import pyautogui
from PIL import ImageTk,Image
import time
import os.path
def callback(event):
w1 = e1.get()
h1 = e2.get()
im = pyautogui.screenshot('my_screenshot.png',region=(0,0, w1, h1))
tru()
def save():
save_path =e3.get()
#if name_of_file is True:
#name_of_file=name_of_file+1
completeName = os.path.join(save_path, "my_screenshot.png")
window = tkinter.Tk()
window.wm_iconbitmap('Scissors-black.ico')
window.title("Screenshot Taker")
#window.geometry("500x500")
frame = LabelFrame(window,text="The Screenshot")
frame.grid(row=4,column=None)
e1 = Entry(window)
e2 = Entry(window)
l1=Label(window,text="Width")
l2=Label(window,text="Height")
l1.grid(row=0,column=0, pady = 2)
l2.grid(row=1,column=0, pady = 2)
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
b1 = Button(text="Save",command=save)
b1.grid(row=3, column=0, pady = 2)
e3 = Entry(window)
e3.grid(row =3, column=1, pady = 2)
L=window.bind("<Return>",callback)
def tru():
my_img = ImageTk.PhotoImage(Image.open("my_screenshot.png"))
l3=Label(frame,image=my_img)
l3.grid(row=4,column=None)
window.mainloop()
#label = tkinter.Label(window,text = "Press Enter to take a screenshot").pack()
You can save image by Image module from PIL library
#import image module
from PIL import Image
#Create image object
image = Image.open(r"path_to_image")#type the path
image.save("Example.png") #type the location to be saved
How can I make a text entry widget over picture in tkinter using python?
The code I am using:
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
root.title('Title')
E1 = Entry(root, bd = 5,show='*') # tried this line of code but it didn't worked
img = ImageTk.PhotoImage(Image.open("path/to/image.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
c = Button(text=" OK ")
c.place(relx=0.95, rely=0.98, anchor=SE)
root.mainloop()
It can make a button (OK) , why it can't make entry text widget?
You forgot to pack the Entry field, just try something like:
E1 = Entry(root, bd = 5,show='*')
E1.pack(side=BOTTOM, fill=BOTH, expand=YES)
And customize the position and behaviour as you prefer.