i am trying to make an image slide show but i cant get the ImageTk module to work
import tkinter as tk
from itertools import cycle
from ImageTk import PhotoImage
images = ["ffa1c83.jpg", "ffa4600.jpg", "faa4149.jpg", "f099e64.png"]
photos = cycle(PhotoImage(file=image) for image in images)
def slideShow():
img = next(photos)
displayCanvas.config(image=img)
root.after(50, slideShow) # 0.05 seconds
root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenwidth()
root.geometry('%dx%d' % (640, 480))
displayCanvas = tk.Label(root)
displayCanvas.pack()
root.after(10, lambda: slideShow())
root.mainloop()
i am using python 3.7
i get this error:
Traceback (most recent call last):
File "C:\Users\cj\Desktop\code_stuff\slideshow\slide show V2\SlideShowV2.py",
line 3, in <module>
from ImageTk import PhotoImage
ModuleNotFoundError: No module named 'ImageTk'
ImageTk is part of module PIL/pillow
from PIL import ImageTk
ImageTk.PhotoImage is used instead of standard tkinter.PhotoImage which can't read .png.
In older versions tkinter.PhotoImage couldn't read .jpg and it was working only with .gif.
Minimal code
import tkinter as tk
from itertools import cycle
from PIL import ImageTk
images = ["ffa1c83.jpg", "ffa4600.jpg", "faa4149.jpg", "f099e64.png"]
photos = cycle(ImageTk.PhotoImage(file=image) for image in images)
BTW: Read about bug in PhotoImage which removes image from memory when it is assigned to local variable in function - see Note in doc PhotoImage
Related
Attribute Error: type object 'Image' has no attribute 'open'
this code is giving this error
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
\#from PIL import Image
import PIL.Image
root=Tk()
root.geometry('300x400')
Button(root,text='open second window',command=open).pack()
def open():
global myimage , img
img=PIL.Image.open("C:\\Users\\HP\\Desktop\\test\\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
mainloop()
I want the image to come on top level window but it is showing attribute error
I do not used PIL. Re-arranged your code in order to make it working. Don't use double slash.
Code:
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import PIL.Image
root=Tk()
root.geometry('300x400')
def open():
global myimage , img
img=PIL.Image.open(r"C:\Users\HPDesktop\test\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
Button(root,text='open second window',command=open).pack()
root.mainloop()
Output:
I'm new to learning the Tkinter Module that is built in Python. I was trying to build a simple Image Viewer GUI using pillow. I'm getting an Attribute Error here.
AttributeError: type object 'Image' has no attribute 'open'
Here is my code:
from PIL import ImageTk,Image
from tkinter import *
base = Tk()
base.title("Image Viewer")
base.iconbitmap("download.ico")
img1 = ImageTk.PhotoImage(Image.open("download.png"))
label1 = Label(image = img1)
label1.grid(row = 0, column = 0, columnspan = 3)
base.mainloop()
I can't seem to find a fix for this and none of the solutions for similar questions on StackOverflow, work.
from tkinter import *
this imports everything from tkinter, including
Image:
Init signature: Image(imgtype, name=None, cnf={}, master=None, **kw)
Docstring: Base class for images.
File: [...]
Type: type
Subclasses: PhotoImage, BitmapImage
So, the Image module that you import earlier from PIL is overwritten.
you can
a) reverse the order:
from tkinter import *
from PIL import Image, ImageTk
b) import only that what you need from tkinter
from PIL import ImageTk, Image
from tkinter import Tk
c) import Image as something else:
from PIL import ImageTk
from PIL import Image as PILImage
from tkinter import *
You can do onething try to use this snippet
import PIL.Image
import PIL.ImageTk
im = PIL.Image.open("download.png")
photo = PIL.ImageTk.PhotoImage(im)
I'm trying to zoom in on an image and display it with the following code
import tkinter as tk
from PIL import Image as PIL_image, ImageTk as PIL_imagetk
window = tk.Tk()
img1 = PIL_imagetk.PhotoImage(file="C:\\Two.jpg")
img1 = img1.zoom(2)
window.mainloop()
But python says AttributeError: 'PhotoImage' object has no attribute 'zoom'. There is a comment on a related post here:
Image resize under PhotoImage
which says "PIL's PhotoImage does not implement zoom from Tkinter's PhotoImage (as well as some other methods)".
I think this means that I need to import something else into my code, but I'm not sure what. Any help would be great!
img1 has no method zoom, however img1._PhotoImage__photo does. So just change your code to:
import tkinter as tk
from PIL import Image as PIL_image, ImageTk as PIL_imagetk
window = tk.Tk()
img1 = PIL_imagetk.PhotoImage(file="C:\\Two.jpg")
img1 = img1._PhotoImage__photo.zoom(2)
label = tk.Label(window, image=img1)
label.pack()
window.mainloop()
By the way if you want to reduce the image you can use the method subsample img1 = img1._PhotoImage__photo.subsample(2) reduces the picture by half.
If you have a PIL Image, then you can use resize like following example:
import tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('C:\\Two.jpg')
image = image.resize((200, 200), Image.ANTIALIAS)
img1 = ImageTk.PhotoImage(image=image)
label = tk.Label(window, image=img1)
label.pack()
window.mainloop()
Note I just import Image and ImageTk, see no need to rename to PIL_image and PIL_imagetk, which to me is only confusing
I am trying to load an image into a canvas in python. However I am getting the error: TclError: couldn't recognize data in image file "C:\testimage\spongesea.jpg"
import Tkinter
from Tkinter import *
import time
import inspect
import os
from PIL import Image, ImageTk
class game_one:
def __init__(self):
global root
global canvas_one
root = Tk()
root.title(" Thanks Josh Dark
canvas_one = Tkinter.Canvas(root, bg="BLACK")
canvas_one.pack(expand= YES, fill= BOTH)
canvas_one.focus_set() #allows keyboard events
p = PhotoImage(file="C:\testimage\spongesea.jpg")
canvas_one.create_image(0, 0, image = p, anchor=NW)
root.grab_set()#I forget what this does. Don't change it.
root.lift()#This makes root appear in front of the other applications
ObjectExample = game_one()# starts the animation
I can open the image manually from the file, so it is not corrupted, and it is calling the correct place. Any ideas? Thanks
PhotoImage works only with GIF and PGM/PPM.
You have to use Image, ImageTk to work with other formats
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
BTW: read PhotoImage and see "Garbage Collection problem" in note.
I'm unable to find any way to load .bmp file into Tkinter() so that I can use it in a canvas widget!Plz help me!
from Tkinter import *
from PIL import Image
import ImageTk
import tkFileDialog
import tkMessageBox
root=Tk()
class lapp:
def __init__(self,master):
w=Canvas(root,width=300,height=300)
w.pack()
p=Image.open("001.bmp")
tkimage=ImageTk.PhotoImage(p)
w.creat_image(0,0,image=tkimage)
App=lapp(root)
root.mainloop()
Its not showing any image on the canvas, its just blank!
Btw I'm using win7 with python 2.7
This works for me.
The image doesn't show when I use the Tk PhotoImage class. But it works ok when using PIL.
My image size is 50*250, so I've put coordinates that center it (25, 125)
from Tkinter import *
from PIL import Image, ImageTk
root=Tk()
root.title("My Image")
w = Canvas(root, width=50, height=250)
image = Image.open("blog0.bmp")
w.create_image((25, 125), image=ImageTk.PhotoImage(image))
w.pack()
root.mainloop()
I hope it helps