I want to know whether I can display an image from the path I have selected? like, I have a path for example: c:\user\desktop\33.jpg, and I want to take only that jpg file and I have to display that image using label or something. If it is possible, I want to know how?
Thanks in advance!
Here is a sample code for what you are asking:
from Tkinter import Label,Tk
from PIL import Image, ImageTk
import tkFileDialog
root = Tk()
path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
im = Image.open(path)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(root,image = tkimage)
myvar.image = tkimage
myvar.pack()
root.mainloop()
You will be wanting to add a button for calling the askopenfilename because right now its calling it the moment the program begins.
Also you might wanna add more file extensions to filetypes
Related
I have two running tkinter windows but I want only one specific window to display the image but I am not able to achieve this. I tried to specify the master in the Label statement but python shows an error which says "image pyimage1 doesn't exist"
Please help
import tkinter as tk
from PIL import Image, ImageTk
a=tk.Tk()
a.geometry('800x500+275+100')
a.title('HOME PAGE')
c=tk.Tk()
c.geometry('800x500+275+100')
c.title('PROFILE')
load=Image.open('untitled.png')
render=ImageTk.PhotoImage(load)
img=tk.Label(c,image=render)
img.pack()
a.mainloop()
c.mainloop()
If you want a second screen use tk.Toplevel and remove c.mainloop
a=tk.Tk()
a.geometry('800x500+275+100')
a.title('HOME PAGE')
c=tk.Toplevel()
c.geometry('800x500+275+100')
c.title('PROFILE')
load=Image.open('untitled.png')
render=ImageTk.PhotoImage(load)
img=tk.Label(c,image=render)
img.pack()
a.mainloop()
I've tried this but it doesn't help: Image resize under PhotoImage
I already have this:
import tkinter
root = tkinter.Tk()
root.attributes("-fullscreen", True)
photo = tkinter.PhotoImage(file="image.gif")
img1 = tkinter.Label(root, image = photo)
img1.pack()
root.mainloop()
This works but my image doesn't take up the full screen and I wanted to know how to resize it to full screen.
I realised it is not possible to do this and I will try to use PIL. Thanks to #BryanOakley for helping me with this question.
I have a folder which has some images. I want to display all the images using tkinter in a single window. Also whenever I click any image displayed in the window, I need to display the path of the image. I tried using for loop but it prints all the image file path. Here is the code I tried,
from Tkinter import *
import os
from PIL import ImageTk, Image
def getFileName(image):
print str(image)
gtk = Tk()
def showImages(folder):
for images in os.listdir(os.getcwd()):
if images.endswith("png"):
im = Image.open(images)
tkimage = ImageTk.PhotoImage(im)
imageButton = Button(gtk, image=tkimage, command=getFileName(images)
imageButton.image=tkimage
imageButton.pack()
gtk.mainloop()
Can anyone say what I'm doing wrong?
for images in os.listdir(os.getcwd()):
if images.endswith("png"):
im = Image.open(images)
tkimage = ImageTk.PhotoImage(im)
handler = lambda img = images: getFileName(img) #here modify
imageButton = Button(gtk, image=tkimage, command=handler)#here
imageButton.image=tkimage
imageButton.pack()
Because button-press callbacks are run with no arguments, if we
need to pass extra datato the handler, it must be wrapped in an object
that remembers that extra data and passes it along, by deferring the
call to the actual handler. Here, a button press runs the function
generated by the lambda, an indirect call layer that retains
information from the enclosing scope. The net effect is that the real
handler.
-- << Programming Python>> page 435
How can I add an image to a widget in tkinter?
Why when I use this code it does not work:
some_widget.config(image=PhotoImage(file="test.png"), compound=RIGHT)
but this does work?:
an_image=PhotoImage(file="test.png")
some_widget.config(image=anImage, compound=RIGHT)
Your image is getting garbage collected when you try to use it in the first version.
effbot is ancient, but here's good snippet:
You must keep a reference to the image object in your Python program, either by storing it in a global variable, or by attaching it to another object.
In the second version the image is declared at the global level.
Here's another example that would demonstrate this issue, that you would expect to also work, after all it's the same code just in a function
Doesn't work:
import tkinter as tk
from PIL import ImageTk
root = tk.Tk()
def make_button():
b = tk.Button(root)
image = ImageTk.PhotoImage(file="1.png")
b.config(image=image)
b.pack()
make_button()
root.mainloop()
Does work:
import tkiner as tk
from PIL import ImageTk
root = tk.Tk()
def make_button():
b = tk.Button(root)
image = ImageTk.PhotoImage(file="1.png")
b.config(image=image)
b.image = image
b.pack()
make_button()
root.mainloop()
Why? The variables in make_button are local to that function. Same idea if you run into this type of problem inside a class.
I need to add an image to the canvas. I have tried countless amounts of things, and finally decided to make a question on here.
This is what I have imported
from Tkinter import *
import tkFont
from PIL import ImageTk, Image
And this is the line of code I'm trying to add to import an image from the same folder the main file is in.
c.create_image(100,100, anchor=N, image = ghost.jpg)
I've also tried putting ""s around 'ghost.jpg' and it says the Image does not exist then. Without the quotes it says "global name 'ghost' does not exist."
Can anyone help?
Canvas.create_image's image argument
should be a PhotoImage or BitmapImage, or a
compatible object (such as the PIL's PhotoImage). The application must
keep a reference to the image object.
from Tkinter import *
"""python 2.7 =Tkinter"""
from PIL import Image, ImageTk
app = Tk()
temp=Image.open("photo.jpg")
temp = temp.save("photo.ppm","ppm")
photo = PhotoImage(file = "photo.ppm")
imagepanel=Label(app,image = photo)
imagepanel.grid()
app.mainloop()
This is a bit of code I wrote in python 2.7 with Tkinter and PIL to import a jpg file from a given directory, this doesn't pop up off the screen.
You should replace photo with the file name (and directory) and app with the relevant variable you set.