AttributeError: Image has no attribute 'open' - python

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)

Related

How do I do this?

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:

image slide show using tkinter and ImageTk

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

'PyCapture2.Image' object has no attribute 'read'

i'm trying to show a picture in TKInter. I'm using the Pycapture library in a FLEA3 Point Grey Camera. I can take the picture, but when i try to show in my GUI, show the error saying Pycapture does not have the attribute "read"
import PyCapture2
from sys import exit
from PIL import ImageTk
import PIL.Image
from time import sleep
from tkinter import *
enableEmbeddedTimestamp(camera, True)
camera.startCapture()
img = ImageTk.PhotoImage(PIL.Image.open(grabImages(camera, 1)))
showImage = Label(imageContainer, image=img)
showImage.pack(side=BOTTOM, expand="yes")
imageLabel.create_image(20, 20, anchor="n", image=img)
camera.stopCapture()

Setting an image as a tkinter window background

I am Trying to set an image as the background of my Tkinter window and everything I try doesn't seem to work. Here's the code I have:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
window.configure(background = '#007501')
If you want the background to be an image (without using canvas), use labels:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
photo=PhotoImage(file="path/to/your/file")
l=Label(window,image=photo)
l.image=photo #just keeping a reference
l.grid()

Not being able to load an image in python

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.

Categories