from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
import cv2
def main():
root = Tk()
root.title("face")
w=root.winfo_screenwidth()
h=root.winfo_screenheight()
root.geometry("%dx%d" % (w, h))
def openimage(): #in this part I just want to keep just one picture in Frame
fname = filedialog.askopenfilename()
image = Image.open(fname)
(width, height)=image.size
if width>=height:
new_width=t1.winfo_width()
new_height=int(height*t1.winfo_width()/width)
image = image.resize((new_width, new_height), Image.ANTIALIAS)
else:
new_height=t1.winfo_height()
new_width=int(width*t1.winfo_height()/height)
image = image.resize((new_width, new_height), Image.ANTIALIAS)
render = ImageTk.PhotoImage(image)
img = Label(t1, image=render)
img.image = render
img.place(x=0, y=0)
def finish():
exit()
menubar = Menu(root)
root.config(menu=menubar)
menu1 = Menu(root)
menu1.add_command(label='openphoto', command=openimage)
menu1.add_command(label='get')
menu1.add_command(label='save')
menu2 = Menu(root)
menu2.add_command(label='read')
menu2.add_command(label='training')
menu3 = Menu(root)
menu3.add_command(label='openphotos')
menu3.add_command(label='dis')
menu4 = Menu(root)
menu4.add_command(label='quit', command=finish)
menubar.add_cascade(label="catch", menu=menu1)
menubar.add_cascade(label="train", menu=menu2)
menubar.add_cascade(label="distinguish", menu=menu3)
menubar.add_cascade(label="exit", menu=menu4)
t1=Frame(width=500,height=500, bg='gray')
t1.grid(padx=100, pady=100)
root.mainloop()
if __name__ =='__main__':
main()
I want to clear the content in Frame, or let the former picture not show in the Frame while I choose another picture. Each time a picture is selected, it has nothing to do with the parameters used in the last selection. So I want to empty a Frame every time I choose a picture. I have tried in many ways. I don't know how to clear it.
To clear the widgets from a tkinter Frame() instance frame, you can use the following code:
for widget in frame.winfo_children():
widget.destroy()
frame.winfo_children() returns a list of all the widgets in frame, then we loop through them and destroy() them all.
Related
This makes an image:
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.geometry("960x540+480+200")
load = Image.open("example.png")
render = ImageTk.PhotoImage(load)
img = tk.Label(image=render)
img.image = render
img.place(x=450, y=280)
tk.mainloop()
How do I put image inside a button?
You can put the image on a button.
button1=Button(window , text = 'Click Me !', image = render)
You can use the following code...
Button(master, text = "Button", image = "image.png", compound=LEFT)
You can use the following code ....
from tkinter import *
from tkinter.ttk import *
# creating tkinter window
root = Tk()
# Adding widgets to the root window
Label(root, text = 'btn').pack(side = TOP, pady = 10)
# Creating a photoimage object to use image
photo = PhotoImage(file = r"pic.png")
# here, image option is used to set image on button
Button(root, text = 'button', image = photo).pack(side = TOP)
mainloop()
So currently, I have to design some Menu bars with many options when clicked. But I have Menu bars part settled, what I'm lost about is I want to have 2 images. For instance if I click 'View', it'll drop down 'Original Image' & 'Scaled image' and if I clicked either one, it will show me respective images with the correct images.
import cv2
import numpy as np
img = cv2.imread('image.jpg')
scaled_img = cv2.resize(img, (400, 500))
cv2.imshow('Original image', img)
From my original code; and I'm not sure where to insert above codes(if correct), to below.
def showImg(self):
load = Image.open('image.jpg')
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0,y=0)
You can use Pillow module to resize image instead of OpenCV. Below is an example:
from tkinter import *
from PIL import Image, ImageTk
class Window(Tk):
def __init__(self):
Tk.__init__(self)
self.geometry('500x600')
# initialize the images
img = Image.open('image.jpg')
self.original_img = ImageTk.PhotoImage(image=img)
img = img.resize((400, 500))
self.scaled_img = ImageTk.PhotoImage(image=img)
# create the menu bar
menubar = Menu(self)
self.config(menu=menubar)
file = Menu(menubar, tearoff=0)
file.add_command(label='New')
file.add_command(label='Open')
file.add_command(label='Save')
file.add_command(label='Save As')
file.add_separator()
file.add_command(label='Exit', command=self.client_exit)
menubar.add_cascade(label='File', menu=file)
view = Menu(menubar, tearoff=0)
view.add_command(label='Original Image', command=lambda:self.showImg(self.original_img))
view.add_command(label='Scaled Image', command=lambda:self.showImg(self.scaled_img))
menubar.add_cascade(label='View', menu=view)
# create a label to show the image
self.imgbox = Label(self)
self.imgbox.place(x=0, y=0)
def showImg(self, img):
self.imgbox.config(image=img)
def client_exit(self):
self.destroy()
Window().mainloop()
from tkinter import *
import tkinter as tk
from PIL import Image, ImageTk
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Creation of init_window
def init_window(self):
img = Image.open("001.png")
img = img.resize((250, 250)) ## The (250, 250) is (height, width)
photo = ImageTk.PhotoImage(img)
quitBtn2 = Label(root, image=photo)
quitBtn2.image = photo
quitBtn2.pack()
Image Module
The Image module provides a class with the same name which is used to
represent a PIL image. The module also provides a number of factory
functions, including functions to load images from files, and to
create new images.
https://pillow.readthedocs.io/en/stable/reference/Image.html
I am making a program where I need to at some point display an image onto a frame at the press of a button. I am using an object oriented approach but it won't display the image. If I do something like:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
pic = Image.open("image.jpg")
tkpic = ImageTk.PhotoImage(pic)
label = Label(root, image=tkpic)
label.pack()
root.mainloop()
that works fine. But if I create a frame and try to display the picture like this:
from tkinter import *
from PIL import Image, ImageTk
class picframe(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
button = Button(self, text="show", command=self.showpic)
button.pack()
def showpic(self):
pic = Image.open("image.jpg")
tkpic = ImageTk.PhotoImage(pic)
label = Label(self, image=tkpic)
label.pack()
root = Tk()
frame = picframe(root)
frame.pack()
root.mainloop()
When I press the button it expands the window as if it was trying to display the image but nothing shows up it just becomes a wider window. So what am I doing wrong?
Thank you in advance!
As the picture is created in a function the reference tkpic will be garbage collected when the function exits. You need to save a reference to the image:
def showpic(self):
pic = Image.open("image.jpg")
tkpic = ImageTk.PhotoImage(pic)
label = Label(self, image=tkpic)
label.image = tkpic # Save reference to image
label.pack()
Alternatively you can ensure the persistance of the image reference by making it an instance variable:
def showpic(self):
pic = Image.open("images/beer.png")
self.tkpic = ImageTk.PhotoImage(pic)
label = Label(self, image=self.tkpic)
label.pack()
Here what I'd like
I'm new in PYTHON. I'm trying to display IMAGE and TEXTBOX simultaneously in a "Form" with PYTHON.
My problem: Image is not visible on the screen. How to solve that problem?
Thanks,
My code:
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("SEE image and ENTER its information")
window.geometry("600x400")
window.configure(background='grey')
# Create textbox in window
text_widget = tk.Text(window)
text_widget.insert('insert',"Enter image information here")
text_widget.pack(anchor = "w", padx = 50, pady = 50)
#Creates a tkinter-compatible photo image.
path = "Picture.jpg"
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard tkinter widget used to display a text or
image on the screen.
panel = tk.Label(window, image = img)
#The Pack geometry manager packs widgets in rows or columns.
#panel.pack(side = "bottom", fill = "both", expand = "no")
panel.pack()
#Start the GUI
window.mainloop()
import tkinter as tk
from PIL import ImageTk, Image
#This creates the main window of an application
window = tk.Tk()
window.title("SEE image and ENTER its information")
window.geometry("600x400")
window.configure(background='grey')
#Creates a tkinter-compatible photo image.
path = "Picture.jpg"
img = ImageTk.PhotoImage(Image.open(path))
#The Label widget is a standard tkinter widget used to display a text or image on the screen.
panel = tk.Label(window, image = img)
# Create textbox in window
text_widget = tk.Text(panel)
text_widget.insert('insert',"Enter image information here")
text_widget.pack(anchor = "w", padx = 50, pady = 50)
#The Pack geometry manager packs widgets in rows or columns.
#panel.pack(side = "bottom", fill = "both", expand = "no")
panel.pack()
#Start the GUI
window.mainloop()
If that's the idea, do you wish to display information to the user or for user to enter information?
Assuming the latter, here is something.
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.title("SEE image and ENTER its information")
window.geometry("600x400") # You can drop this line if you want.
window.configure(background='grey')
path = "Picture.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
txtVar = tk.StringVar(None)
usrIn = tk.Entry(window, textvariable = txtVar, width = 90)
usrIn.grid(row = 50, column = 60)
usrIn.pack()
panel.pack()
window.mainloop()
ThetxtVar can be used for accepting info from user. You may also have to use the Button feature if needed.
Here is nice link.
I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself.
Currently, I can display an image like so:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
However, when the user hits, say the ENTER key, I'd like to change the image.
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
# change image
root.bind("<Return>", callback)
root.mainloop()
Is this possible?
The method label.configure does work in panel.configure(image=img).
What I forgot to do was include the panel.image=img, to prevent garbage collection from deleting the image.
The following is the new version:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
img2 = ImageTk.PhotoImage(Image.open(path2))
panel.configure(image=img2)
panel.image = img2
root.bind("<Return>", callback)
root.mainloop()
The original code works because the image is stored in the global variable img.
Another option to do it.
Using object-oriented programming and with an interactive interface to update the image.
from Tkinter import *
import tkFileDialog
from tkFileDialog import askdirectory
from PIL import Image
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
w,h = 650, 650
master.minsize(width=w, height=h)
master.maxsize(width=w, height=h)
self.pack()
self.file = Button(self, text='Browse', command=self.choose)
self.choose = Label(self, text="Choose file").pack()
self.image = PhotoImage(file='cualitativa.gif')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = ifile.name
self.image2 = PhotoImage(file=path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
Replace 'cualitativa.jpg' for the default image you want to use.
Another solution that might be of help.
In my case, I had two tk.Tk() windows. When using ImageTk.PhotoImage(), the object defaults to setting its tk window to being the first one created. A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)
import tkinter as tk
from PIL import ImageTk, Image
if __name__ == '__main__':
main_window = tk.Tk()
second_window = tk.Tk()
main_canvas = Canvas(second_window)
main_canvas.pack()
filename = 'test.png'
img = Image.open(filename)
img = img.resize((300, 100), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(img, master=second_window)
logo_label = Label(master=main_canvas, image=logo)
logo_label.image = logo
logo_label.pack()
main_window.mainloop()