I can't get this image to be displayed in a tkinter gui because it doesn't recognize the image file or directory, here is my code:
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
path = "krebs.jpg"
img = ImageTk.PhotoImage(Image.open(path))
img.pack()
root.mainloop()
and here is the picture in question: Krebs security pic
I bet your current working directory isn't what you think it is. Try:
import os
os.getcwd()
Make sure that directory (printed by os.getcwd()) has your image in it (i.e. go to that folder and check to see if your image is there). If not, specify the absolute path to your image:
path = C:/Users/<user_name>/... .../krebs.jpg
or whatever the full path to the image is on your OS.
Alternatively, you could move the image to the folder indicated by os.getcwd().
Sometimes I make a typo when defining paths in python, because very simple IDEs (like IDLE) don't have in-line auto-complete. Here's a trick I use all the time to make sure my absolute path doesn't have a typo:
import tkinter.filedialog as fd
path = fd.askopenfilename()
It will open a familiar file opening dialog. Use the file dialog window to find your file, then click "Open", and you'll have the full path saved to variable "path".
Well I believe there is a simpler solution and one that is better overall. Python can use . to tell the code that the file you are looking for is in your same directory as your python code. This way you do not need to select a file manually or use a complete file path name.
2nd I noticed you are trying to pack() an image into the window however you cannot do this. Instead add it to a label or canvas. Just make sure your image file is in the same folder as your .py file.
Try this.
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
path = "./krebs.jpg"
img = ImageTk.PhotoImage(Image.open(path))
lbl = tk.Label(root, image=img)
lbl.pack()
root.mainloop()
I used a simple small red square image I use for testing but here is the results:
Related
I have a chart function that saves the end figure as a file. After I run the function, I also want it to display the figure at the end. So, I use this:
from PIL import Image
filepath = 'image.png'
img = Image.open(filepath)
img.show()
It works just fine, but when the file opens, it opens with a random file name, not the actual file name.
This can get troublesome as I have a lot of different chart functions that work in a similar fashion, so having logical names is a plus.
Is there a way I can open an image file with Python and have it display it's original file name?
EDIT
I'm using Windows, btw.
EDIT2
Updated the example with code that shows the same behaviour.
Instead of PIL you could use this:-
import os
filepath = "path"
os.startfile(filepath)
Using this method will open the file using system editor.
Or with PIL,
import Tkinter as tk
from PIL import Image, ImageTk # Place this at the end (to avoid any conflicts/errors)
window = tk.Tk()
#window.geometry("500x500") # (optional)
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()
The function img.show() opens a Windows utility to display the image. The image is first written to a temporary file before it is displayed. Here is the section from the PIL docs.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show
Image.show(title=None, command=None)[source] Displays this image. This
method is mainly intended for debugging purposes.
This method calls PIL.ImageShow.show() internally. You can use
PIL.ImageShow.register() to override its default behaviour.
The image is first saved to a temporary file. By default, it will be
in PNG format.
On Unix, the image is then opened using the display, eog or xv
utility, depending on which one can be found.
On macOS, the image is opened with the native Preview application.
On Windows, the image is opened with the standard PNG display utility.
Parameters title – Optional title to use for the image window, where
possible.
"
The issue is that PIL uses a quick-and-dirty method for showing your image, and it's not intended for serious application use.
In my program, there any many images that are placed in a tkinter window, and i'm using the following code to place one image:
imgCapa = tk.PhotoImage(file='capa.png')
self.canvasTitulo.create_image(800,300,
image=imgCapa,
anchor='se')
The problem is that there are many images and i would like to place all of them inside a folder instead of placing inside the same folder of the python code, and read them from there.
just make a folder and add you path to file
imgCapa1 = tk.PhotoImage(file='my_folder/capa1.png')
imgCapa2 = tk.PhotoImage(file='my_folder/capa2.png')
self.canvasTitulo.create_image(800,300,
image=imgCapa1,
anchor='se')
I am making an animated presentation and I want to add an image to it, but I can't use PIL because it will be presented at school, where PIL isn't installed, so I used the method given by the website http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm, but it doesn't work - my slide is still blank. What should I do?
Here is my code (copied almost completely from the website - did I miss something out?):
photo = PhotoImage('Alveoli.png')
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
You can not open .png files without using an external image library. Since PIL isn't available I'd think other similar libraries aren't either.
EDIT: Note: PNG files are supported for tkinter v8.6+.
The only possibility that comes to mind is converting the image to another, compatible format like GIF or PGM
If you have the image in one of those formats you can simply add them. It worked just fine for me with this code:
from tkinter import *
root = Tk()
photo = PhotoImage(file="img.ppm")
img = Label(root, image=photo)
img.image = photo
img.place(x=0, y=0)
root.mainloop()
Message File Name
Syntax Error
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: >truncated \UXXXXXXXX escape C:\visualexample.py
I've Imported PIL and tkinter, and tried a few different things, but the one I see mostly suggested is:
img = ImageTk.PhotoImage(Image.open("C:\testpic.gif"))
or
img = tk.PhotoImage(Image.open("C:\testpic.gif"))
Have also tried image file types: .bmp .jpeg .png .gif....put them into a label and pack or place.
I have also got another error msg that I don't have at hand, but it was something like:
Invaild '____str____' value type: 'JpegImageFormat' is not a recognised format.
When this one comes up, it also does it for all picture file types.
I think i'm just going to have to pretty up my program with plain old solid colour backgrounds and font colours. Maybe the PIL Lib is the wrong one (I had to get it separate from python 3.3), I thought I got the right one.
Maybe theres a file size(Mb's) limit?
If anyone has any suggestions as to how to put a picture file in the background that would be great!
edit:
python 3x, import tkinter and from PIL import ImageTk, Image
suggestion worked for .gif .bmp .png .jpg :)
img = PIL.ImageTk.PhotoImage(PIL.Image.open(picfilepath))
I had also problems with PIL and Tkinter Images... I had been searching for hours when I found a solution. Try this, I hope it will help you, this simple program runs a label with an image on it:
from tkinter import *
import PIL.Image
import PIL.ImageTk
root = Tk()
image = PIL.ImageTk.PhotoImage(PIL.Image.open ("C:/Users/Vladi/example.jpg"))
label = Label (root, image = image).pack()
root.mainloop()
I've just specified all the libraries where I took the modules from. And take also care to the backslashes... Using those which you have used, you will get an error. Use this one: / or: \\
If you want to resize it:
img = PIL.Image.open ("C:/Users/Vladi/example.jpg")
img = img.resize ((80, 75))
img = PIL.ImageTk.PhotoImage(self.im)
You have to resize it before calling PhotoImage, you can also write it all in a line:
img = PIL.ImageTk.PhotoImage(PIL.Image.open ("C:/Users/Vladi/example.jpg").resize((80, 75)))
I hope I've helped you
It seems like PIL has serious problems. I think it doesn't support Python 3 yet at all (https://mail.python.org/pipermail/image-sig/2009-March/005498.html). But I also tried it on Python 2.7.11, and couldn't show any image. If you're trying to show an image on Tkinter, I would suggest you try this:
from tkinter import *
root = Tk()
imgobj = PhotoImage(file="filename.png")
label = Label(root, image=imgobj)
label.pack()
It also supports gif image format, but doesn't support jpg type.
I am new to TKinter and cant seem to find any examples on how to view a document in a window. What I am trying to accomplish is that when selecting a PDF or TIF it will open the file and show the first page in a window using TKinter. Is this possible?
A long time has passed since this question was posted but, for those still looking for a solution, here's one I've found :
https://github.com/rk700/PyMuPDF/wiki/Demo:-GUI-script-to-display-a-PDF-using-wxPython-or-Tkinter
Basically, it uses PyMuPDF, a python binding for MuPDF. MuPDF is a lightweight document viewer capable of displaying a few file formats, such as pdf and epub.
I quote the code used for TKinter:
This demo can easily be adopted to Tkinter. You need the imports
from Tkinter import Tk, Canvas, Frame, BOTH, NW
from PIL import Image, ImageTk
and do the following to display each PDF page image:
#-----------------------------------------------------------------
# MuPDF code
#-----------------------------------------------------------------
pix = doc.getPagePixmap(pno - 1) # create pixmap for a page
#-----------------------------------------------------------------
# Tkinter code
#-----------------------------------------------------------------
self.img = Image.frombytes("RGBA",
[pix.width, pix.height],
str(pix.samples))
self.photo = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20,
height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.photo)
canvas.pack(fill=BOTH, expand=1)
No, it is not possible to show a TIF or PDF in a Tkinter window. Your main options are to show plain text, and to show images (.gif with plain Tkinter, other formats if you include PIL - the python imaging library).
Nothing is impossible my friend!
Try using snippets from this:
http://nedbatchelder.com/blog/200712/extracting_jpgs_from_pdfs.html
to convert the pdf to an image.
I would then display the image on a label using PIL and Tkinter.
And I think Tif files should display on a label without problems IIRC.
The following code uses the old library pypdfocr (I had to modify it to work with Python 3) and Ghostscript (Ghostscript needs to be installed outside of Python and so isn't ideal for software to be distributed) to convert each of the PDF's pages into an image that can then be opened by PIL. In the sense that it uses very old libraries and packages, DrD's answer is probably more appropriate.
import glob
import PIL.Image
import PIL.ImageTk
import pypdfocr.pypdfocr_gs as pdfImg
files = glob.glob(pdfImg.PyGs({}).make_img_from_pdf(file_name)[1])
pages = [PIL.ImageTk.PhotoImage(PIL.Image.open(file)) for file in files]