I created a simple image opening program which opens the image selected from filedialog by clicking a button, but wherever I select another image it just appears under the current image
I want the next image selected to be replaced by the old image.
Plz help what should I do
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
root=Tk()
root.title('Image')
def open():
global my_img
root.filename = filedialog.askopenfilename(initialdir='/GUI',title='Select A File',filetypes=(('jpg files','*.jpg'),('png files','*.png'),('all files','*.*')))
my_img = ImageTk.PhotoImage(Image.open(root.filename))
my_image_lbl = Label(image=my_img).pack()
my_btn = Button(root,text='Open File Manager',command=open).pack()
root.mainloop()
You should create the my_image_lbl outside open() and update its image inside the function:
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
root=Tk()
root.title('Image')
def open():
filename = filedialog.askopenfilename(initialdir='/GUI',title='Select A File',filetypes=(('jpg files','*.jpg'),('png files','*.png'),('all files','*.*')))
if filename:
my_image_lbl.image = ImageTk.PhotoImage(file=filename)
my_image_lbl.config(image=my_image_lbl.image)
Button(root,text='Open File Manager',command=open).pack()
my_image_lbl = Label(root)
my_image_lbl.pack()
root.mainloop()
Related
I have two tkinter files, one with a welcome screen and the other with the selection menu, I have created two separate tkinter files and combine them to make it a single program where the welcome screen dissolves after 10 seconds and the selection menu appears.
my code:
welcome screen:
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
#icon
root = Tk()
root.title("-----")
root.geometry("425x508")
root.iconbitmap('left this on purpose')
#welcome screen
welcome = ImageTk.PhotoImage(Image.open("left this on purpose"))
lbl = Label(image=welcome)
lbl.pack()
root.after(10000, lambda: root.destroy())
selection menu screen:
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
#icon
root = Tk()
root.title("selection")
root.geometry("325x396")
root.iconbitmap("left this on purpose")
select = ImageTk.PhotoImage(Image.open("left this on purpose"))
lbl2 = Label(image=select)
lbl2.pack()
root.mainloop()
You didn't add a way to display the welcome screen, however, if that's what you wanted:
main.py -
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
from selectionmenu import app
#icon
root = Tk()
root.title("-----")
root.geometry("425x508")
# stuff you want to do
root.after(10000, app)
selectionmenu.py -
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
# icon
def app():
root = Tk()
root.title("selection")
root.geometry("325x396")
root.mainloop()
I don't think this is the way to proceed by having two instances of Tk(), but using the Toplevel widget doesn't provide a way to delete the root window and using the toplevel.
from tkinter import *
from PIL import Image, ImageTk
import glob, os
root = Tk()
root.geometry("800x600")
# Function to display image
def displayImg(img):
image = Image.open(img)
photo = ImageTk.PhotoImage(image)
newPhoto_label = Label(image=photo)
newPhoto_label.pack()
# gta_images = []
os.chdir("gta")
for file in glob.glob("*.jpg"):
# gta_images.append(str(file))
displayImg(file)
print(file)
# print(gta_images)
root.mainloop()
I am trying to load images from a folder called "gta" and then display those game logos on my app. Program has no error but I think its a logical error. I am new to Python I don't know maybe there is some scoping logic problem in my displayImg funcion.
Note: When a PhotoImage object is garbage-collected by Python (e.g.
when you return from a function which stored an image in a local
variable), the image is cleared even if it’s being displayed by a
Tkinter widget.
For more.
from tkinter import *
from PIL import Image, ImageTk
import glob, os
root = Tk()
root.geometry("800x600")
photos = []
def displayImg(img):
image = Image.open(img)
photo = ImageTk.PhotoImage(image)
photos.append(photo)#keep references!
newPhoto_label = Label(image=photo)
newPhoto_label.pack()
for file in glob.glob("*.jpg"):
displayImg(file)
print(file)
root.mainloop()
Not sure if it will work but try using the path of the gta folder you are getting the images from, instead of its name simply - replace the name with the path.
I created a function in Tkinter that imports an image. But the function seems to execute but I am not able to access the image object. why did the problem occur?
import tkinter
from PIL import ImageTk, Image
from tkinter import *
from tkinter import ttk
root = tkinter.Tk()
root.title("Guess Geek")
root.geometry("1280x720")
root.resizable(0, 0)
def importimg(x,y):
x=ImageTk.PhotoImage(file=y)
importimg('bgimg','main1.jpg')
bg = Label(root, image=bgimg, )
bg.place()
root.mainloop()
Welcome to StackOverflow! I know that you have already accepted an answer but I want to show you my take on your code:
import tkinter as tk
import tkinter.ttk as ttk
import PIL.ImageTk
import PIL.Image
root = tk.Tk()
root.title("Guess Geek")
root.geometry("1280x720")
root.resizable(0, 0)
def importimg(file_name):
return PIL.ImageTk.PhotoImage(PIL.Image.open(file_name))
bg_img = importimg('main1.jpg')
bg = ttk.Label(root, image=bg_img)
bg.grid(column=0, row=0)
root.mainloop()
So here's what I have changed in your code:
Instead of bringing al of tkinter and tkinter.ttk in the global scope, I imported only the module
I renamed tkinter as tk and tkinter.ttk as ttk for the convenience of having shorter names to use
I imported the PIL.ImageTk module without bringing them in the global scope just to be sure of where it came from
I imported the PIL.Image module without bringing them in the global scope because there is a tkinter.Image that would have overrode it if with kept both in the global scope (the way your code was written)
PIL.ImageTk.PhotoImage() first argument should an PIL's image (provided easily by Image.open()) and not just the file name
importimg() returns the object created from the class PIL.ImageTk.PhotoImage(). This will get assigned to bg_img
Instead of using the place() geometry manager, I've used the grid()'s one. It's generally recommended by the TkDocs website
If you have any questions don't hesitate to ask.
The idea behind the code is perfect.
The problem here is that the code is unable to assign the image to the variable bgimg and that is happening because in line 15 you have defined bgimg as a string and not as a variable
Instead of
importimg('bgimg','main1.jpg')
Try
bgmain = 0
importimg(bgimg,'')
The Program might not be able to find the directory of image. I would recommend you to use .open() method to define image location. With this you can also specify the Width and Height of the image
.open() method
path = ".\imagename.jpg" or ".\images\imgname.jpg" or "F:\New folder\imgname.jpg"
k = Image.open(path)
k = k.resize((300,300), Image.ANTIALIAS)
bgimg = ImageTk.PhotoImage(k)
LABEL = Label(bg="black", image=bgimg)
LABEL.place(height=300, width=300)
If you still can't figure out what the problem is just refer to the following
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Guess Geek")
root.geometry("1280x720")
root.resizable(0, 0)
def importimg(x,y):
path = y
k = Image.open(path)
k = k.resize((400,400),Image.ANTIALIAS)
x=ImageTk.PhotoImage(k)
global bgimg
bgimg = x
bgimg = 0
importimg(bgimg,".\foldername\imagename.jpg")
bg = Label(root, image=bgimg )
bg.place(width=400, height=400, relx=0.05, rely=0.1)
root.mainloop()
I am trying to display a PGM image in Tkinter window, however I keep getting the error of "couldn't recognize data in image file". Is there something wrong with my code??
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from tkinter import PhotoImage
def browse_file(self):
path = filedialog.askopenfilename()
if len(path) > 0:
photo = tk.PhotoImage(file=path)
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
I want to have the user select images to have the logo image pasted onto. The file dialog pops up and lets you choose the image files fine. The logo image also loads up and gets resized. And the New Image file is made.
My problem comes with opening the images that were selected. When it gets to "im = PIL.Image.open(img)" it gives me an IOError that says "No such file or directory".
import PIL
import os.path
import PIL.Image
from Tkinter import *
from Tkinter import Tk
from tkFileDialog import askopenfilename
import Tkinter, tkFileDialog
def logo():
imgs = tkFileDialog.askopenfilename(parent=root, title='Choose files', multiple = True)
logo = PIL.Image.open('logo.jpeg')
logo_new = logo.resize((125, 100))
newpath = r'C:\Users\Austin\Desktop\New Images'
if not os.path.exists(newpath):
os.makedirs(newpath)
for img in imgs:
im = PIL.Image.open(img)
width, height = im.size()
im.paste(logo_new, ((width-125), (height-100)), mask=logo_new)
im.save(newpath)
#GUI Code
root = Tk()
root.title("Ad Maker") #GUI title
root.geometry("250x250") #GUI size
app = Frame(root)
app.grid()
button1 = Button(app, text="Logo", command=logo)
button1.grid() #sets button for the logo function
button2 = Button(app, text = "Frame")
button2.grid() #sets button for the frame function
root.mainloop()
I know this code probably isn't the best as I'm still fairly new to programming, but any help in getting the logo() function finally working would be really appreciated.