I would like to open the filedialog after pressing a button. Then I could choose an image and display it on a canvas. (My goal is to do a very simple image editor) Unfortunately, the filedialog open up automatically when I start the program. Is there a way for example to do something like this:
press a button to open the filedialog
choose an image
display the image on the canvas
Here is my code that I've done so far
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
root = Tk()
#function to select my image by using the filedialog
def select_image():
file_path = filedialog.askopenfilename()
return Image.open(file_path)
#button to press to open filedialog
select = Button(root, text="select an image", command=select_image)
select.pack()
#the canvas where the image will be display
canvas = Canvas(root, width= 400, height=400, bg="grey")
canvas.pack()
image_tk = ImageTk.PhotoImage(select_image())
canvas.create_image(200,200, image= image_tk)
root.mainloop()
The filedialog opens because of this line:
image_tk = ImageTk.PhotoImage(select_image())
My Solution is:
from tkinter import Tk, filedialog, Frame, Button, Canvas
from PIL import Image, ImageTk
class Gui:
def __init__(self, master):
self.master = master
self.create_widgets()
def create_widgets(self):
self.select = Button(self.master, text="select an image", command=self.select_image)
self.select.pack()
self.canvas = Canvas(self.master, width= 400, height=400, bg="grey")
self.canvas.pack()
def select_image(self):
file_path = filedialog.askopenfilename()
des = Image.open(file_path)
bg_image = ImageTk.PhotoImage(des)
self.canvas.bg_image = bg_image
self.canvas.create_image(200,200, image=self.canvas.bg_image)
if __name__ == "__main__":
root = Tk()
my_gui = Gui(root)
root.mainloop()
Related
I am creating a gui for my ML project but I am unable to successfully create a vertical scrollbar that actually works. Here's my code
import tkinter as tk
import tkinter.filedialog
from PIL import ImageTk, Image
class App:
def __init__(self, master):
frame = tk.Frame(master,width=500,height=500)
frame.pack()
self.canvas=tk.Canvas(frame,width=500, height=500,borderwidth=0,highlightthickness=0,background='white',scrollregion=(0,0,500,800))
self.canvas.pack()
vbar=tk.Scrollbar(self.canvas,orient="vertical")
vbar.pack(side="right",fill="y")
vbar.config(command=self.canvas.yview)
self.canvas.config(yscrollcommand=vbar.set)
self.canvas.bind("<Configure>",lambda e:self.canvas.configure(scrollregion=self.canvas.bbox('all')))
root.minsize(500,500)
self.button = tk.Button(self.canvas,
text="QUIT", fg="red",
command=frame.quit)
self.button.pack()
self.button2 = tk.Button(self.canvas,
text="Display image",
command=self.Display_image)
self.button2.pack()
def Display_image(self):
f = tkinter.filedialog.askopenfilename(
parent=root, initialdir='D:/Test',
title='Choose file',
filetypes=[('bmp images', '.bmp')]
)
image = ImageTk.PhotoImage(Image.open(f))
l1 = tkinter.Label(self.canvas, image=image)
l1.image = image
l1.pack()
root = tk.Tk()
app = App(root)
root.mainloop()
The code does create a scrollbar but is not functional. However when I include this line of code self.canvas.create_rectangle((0,0,500,800)) in the code the rectangle scrolls.
I am able to flip an image on the canvas. I am doing it by saving it in a file by using ImageGrab function and rotating that by using ImageOps and placing that on canvas but when the image is edited I mean when objects are rotated or dragged am not able to flip. The drag and rotate part is not there in code.It would be too long if I include it.
Here is my code
from tkinter import *
from PIL import ImageTk,Image,ImageGrab
import PIL
from tkinter import filedialog
from PIL import ImageOps
root = Tk()
def selected1(event):
if clicked1.get()=="flip horizontal":
x2=root.winfo_rootx()+canvas.winfo_x()
y2=root.winfo_rooty()+canvas.winfo_y()
x1=x2+root.winfo_screenwidth()
y1=y2+root.winfo_screenheight()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("saver.jpg")
im = Image.open("saver.jpg")
im = ImageOps.mirror(im)
im.save("saver2.jpg")
canvas.delete('all')
global img1
img1=Image.open("saver2.jpg")
img1=img1.resize((1225,685),Image.ANTIALIAS)
canvas.img=ImageTk.PhotoImage(img1)
canvas_img=canvas.create_image(0,0,image=canvas.img,anchor="nw")
if clicked1.get()=="flip vertical":
x2=root.winfo_rootx()+canvas.winfo_x()
y2=root.winfo_rooty()+canvas.winfo_y()
x1=x2+root.winfo_screenwidth()
y1=y2+root.winfo_screenheight()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("saver.jpg")
im = Image.open("saver.jpg")
im = ImageOps.flip(im)
im.save("saver1.jpg")
canvas.delete('all')
global img2
img2=Image.open("saver1.jpg")
img2=img2.resize((1225,685),Image.ANTIALIAS)
canvas.img=ImageTk.PhotoImage(img2)
canvas_img=canvas.create_image(0,0,image=canvas.img,anchor="nw")
def selected2(event):
if clicked2.get()=="open file":
global img
root.filename=filedialog.askopenfilename(initialdir="/ROBIN",title="select a file",filetypes=(("image files","*.jpg"),("all files","*.*")))
img=Image.open(root.filename)
img=img.resize((1225,685),Image.ANTIALIAS)
canvas.img=ImageTk.PhotoImage(img)
canvas_img=canvas.create_image(0,0,image=canvas.img,anchor="nw")
if clicked2.get()=="save file":
x2=root.winfo_rootx()+canvas.winfo_x()
y2=root.winfo_rooty()+canvas.winfo_y()
x1=x2+root.winfo_screenwidth()
y1=y2+root.winfo_screenheight()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("checker.jpg")
if clicked2.get()=="exit":
root.quit()
canvas = Canvas(root)
canvas.pack(fill=BOTH, expand=1)
rectangles=[]
canvas.grid(column=0, row=0, sticky=(N,W,E,S))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
options1=[
"select flip",
"flip horizontal",
"flip vertical",
]
clicked1=StringVar()
clicked1.set("select flip")
drop1=OptionMenu(root,clicked1,*options1,command=selected1)
drop1.place(relx=1,x=-1,y=1,anchor=NE)
clicked2=StringVar()
clicked2.set("select")
options2=[
"select",
"open file",
"save file",
"exit",
]
drop2=OptionMenu(root,clicked2,*options2,command=selected2)
drop2.place(relx=1,x=-1,y=40,anchor=NE)
root.mainloop()
When the button is clicked, browse window should pop up asking for the image file to select and that image needs to be displayed on a label. Unable to define the function clicked() to do so.
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
root = Tk()
#def clicked():
# path=filedialog.askopenfilename(filetypes=[("Image File",'.jpg')])
label = Label(root, image = logo)
label.pack()
button = Button(root, text = "Load Image", command = clicked)
button.pack()
root.mainloop()
This May Help You
from tkinter import *
from tkinter import filedialog
from PIL import Image,ImageTk
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()
#Replace with your image
self.image = PhotoImage(file='test.png')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = filedialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = Image.open(ifile)
self.image2 = ImageTk.PhotoImage(path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
I am trying to create a button that will clear a canvas and then put an image in the canvas selected from a Tkinter file dialog. I am using the canvas.delete("all") method shared here to try clearing the canvas before selecting a new image, but the new image is appearing above the existing image rather than replacing it.
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
from PIL import Image, ImageTk
root = Tk()
canvas1 = Canvas(root, width=500, height=500)
canvas1.pack()
class widgets:
def __init__(self, parent):
self.myParent = parent
self.myContainer1 = Frame(parent)
self.myContainer1.pack()
self.button1 = Button(self.myContainer1, command=self.button1click)
self.button1.config(text="Select Image")
self.button1.pack(side=TOP)
def button1click(self):
canvas1.delete("all")
path = tkFileDialog.askopenfilename(filetypes=[("Image File", '.png')])
im = Image.open(path)
tkimage = ImageTk.PhotoImage(im)
imglabel = Label(canvas1, image=tkimage)
imglabel.image = tkimage
imglabel.pack()
widgets = widgets(root)
root.mainloop()
This seems like a pretty straightforward question, but i am having trouble displaying a jpg image when a button is clicked. Here is my code (without the button code for the sake of time):
from tkinter import *
#screen stuff here
canvas = Canvas(app)
canvas.grid(row = 0,column = 0)
photo = PhotoImage(file = "test.jpg")
canvas.create_image(0,0, image = photo)
def show_image():
global canvas
global photo
canvas.create_image(0,0, image = photo)
#button that calls the function down here
Thanks!
This works with Python2:
import Tkinter as tk
import ImageTk
def show_image():
x = canvas.create_image(125, 125, image=tk_img)
while True:
print('show')
canvas.itemconfigure(x, state=tk.NORMAL)
button.configure(text = 'Hide')
yield
print('hide')
canvas.itemconfigure(x, state=tk.HIDDEN)
button.configure(text = 'Show')
yield
root = tk.Tk()
canvas = tk.Canvas(root, width=250, height=250)
canvas.grid(row=0, column=0)
tk_img = ImageTk.PhotoImage(file='image.png')
button = tk.Button(
root, text="Show", command=show_image().next, anchor='w',
width=10, activebackground="#33B5E5")
button.grid(row=1, column=0)
root.mainloop()
In Python3, PhotoImage can open GIF, PPM/PGM images. To open other formats, you may need to install Pillow (a fork of the PIL project for Python3).