Unable to display PIL Image inside Canvas - python

I'm trying to display a PIL.Image inside a tkinter.Canvas:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry('500x400')
canvas = Canvas(root, width=500, height=400)
canvas.pack()
pilImage = Image.open("pillow.png")
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(400,400,image=image)
root.mainloop()
But when I run this code only a void window appears, without the image.
How can I solve this problem?
I'm using Python 3.4.1 on OS X 10.9

Related

How do I use an image for a project using PIL in Python code?

I am trying to make a blackjack game in Python using Tkinter and PIL packages. I saved all the necessary cards in a folder on my desktop but cannot seem to get the card images to appear.
Below is the code I have tried :
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Blackjack")
canvas = Canvas(root, bg="green", height=1200, width=800)
canvas.pack(fill=BOTH, expand=1)
img = ImageTk.PhotoImage(Image.open(
"C:\Desktop\GUI\Images\Cards\2_of_clubs.png"))
root.mainloop()
How to make images appear when using tkinter ?

How do I remove the white frame around the photo in Tkinter?

When I upload an Image in tkinter there is a white frame around the picture. I searched a lot but I didn't foud any answer.
Picture of the white frame:
Here is my code:
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.geometry("1200x583")
root.resizable(0,0)
place_one=Image.open("p1.jpg")
place_one=place_one.resize((1200,583))
place_one_2=ImageTk.PhotoImage(place_one)
place=Label(root,image=place_one_2)
place.place(x=-2,y=0)
character=Image.open("m2.gif")
character=ImageTk.PhotoImage(character)
L1=Label(root,image=character)
L1.place(x=0,y=355)
root.mainloop()
You need to set image border width (borderwidth) ,
L1=Label(root,image=character, borderwidth=0)
Edit:
Do not use import *(why it is bad?) try to,
import tkinter as tk
from PIL import ImageTk,Image
root = tk.Tk()
root.geometry("1200x583")
root.resizable(0,0)
place_one=Image.open("p1.jpg")
place_one=place_one.resize((1200,583))
place_one_2=ImageTk.PhotoImage(place_one)
place=tk.Label(root,image=place_one_2)
0place.place(x=-2,y=0)
character=Image.open("m2.gif")
character=ImageTk.PhotoImage(character)
L1=tk.Label(root,image=character, borderwidth=0)
L1.place(x=0,y=355)
root.mainloop()

PGM file won't open in tkinter window, "couldn't recognize data in image file"

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')

My code doen't work inside a function, python3/tkinter

I'm using python3 and tkinter, the first code works but the second after I put it into a function it no longer shows the image and I want to know why
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("test")
canv = Canvas(root,width=500, height=500)
canv.pack()
image = ImageTk.PhotoImage(Image.open("C:/Users/tomas/Desktop/carros/carro2.jpg"))
canv.create_image(0,0,anchor="nw",image=image)
root.mainloop()
second code:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("test")
def main():
canv = Canvas(root,width=500, height=500)
canv.pack()
image = ImageTk.PhotoImage(Image.open("C:/Users/tomas/Desktop/carros/carro2.jpg"))
canv.create_image(0,0,anchor="nw",image=image)
main()
root.mainloop()
In order to properly display ImageTk.PhotoImage images, you need to keep a persistent reference to each of them (see this answer and the documentation).
When your code leaves the main() function, your image object is garbage-collected. One easy way to fix this is to store the image reference in the canvas object, so that it will not be garbage-collected as long as your canvas exists:
...
image = ImageTk.PhotoImage(Image.open("C:/Users/tomas/Desktop/carros/carro2.jpg"))
canv.img = image
...

How can I open an image in Python?

I have looked at a lot of tutorials tried them all, but nothing seemed to work through Pygame, PIL, Tkinter. it could be because of me of course, cause Im a greenie...
from Tkinter import *
root = Tk()
photo = PhotoImage(file="too.jpg")
label = Label(root, image=photo)
label.pack()
root.mainloop()
Your code is correct but it won't work because of the jpgfile.
If you want to use the PhotoImage class you can only read read GIF and PGM/PPM images from files (see docs).
For other file formats you can use the Python Imaging Library (PIL).
Here's your example using PIL:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
image = Image.open("too.jpg")
photo = ImageTk.PhotoImage(image)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
root.mainloop()
The line label.image = photo is necessary if you want to avoid your image getting garbage-collected.

Categories

Resources