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

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

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:

AttributeError: Image has no attribute 'open'

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)

PhotoImage zoom

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

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