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:
Related
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 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()
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()
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