Tkinter PhotoImage won't Work - python

I have been creating a file that displays a image on a canvas. I created the 'PhotoImage' file that I use to store my picture.
d = PhotoImage(file="pic.gif")
canvas = Canvas(root, 500, 500)
pic = canvas.create_image(20, 20, image=d)
But I just produce an error each time I run the program...for some reason, PhotoImage will never work for me. How do I get this to work?

This is a an example that works for me.
#----------------------------------------------------------------------
import Tkinter
#----------------------------------------------------------------------
root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame = Tkinter.LabelFrame(root, text="LabelFrame text", padx=5, pady=5)
frame.pack(side=Tkinter.LEFT)
Label1 = Tkinter.Label(frame, text="Label1 text")
Label1.pack()
#----------------------------------------------------------------------
photo1 = Tkinter.PhotoImage(file = 'Chart_Example.gif')
#
width_1 = photo1.width()
height_1 = photo1.height()
#
x_center_1 = width_1 / 2.0
y_center_1 = height_1 / 2.0
#---------------------------------
iframe1 = Tkinter.Frame(frame, bd=2, relief=Tkinter.RAISED)
iframe1.pack(expand=1, fill=Tkinter.X, pady=5, padx=5, side=Tkinter.LEFT)
c1 = Tkinter.Canvas(iframe1, width=width_1, height=height_1)
c1.create_image(x_center_1, y_center_1, image=photo1, anchor = Tkinter.CENTER)
c1.pack(side=Tkinter.LEFT)
#----------------------------------------------------------------------
root.mainloop()
#----------------------------------------------------------------------

Related

Too early to create image at NewWindow

I want to display the image in a new window, but I get an error.
This is my error code
photo = PhotoImage(file='img/dog')
File "C:\Users\Hyojae\Anaconda3\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Hyojae\Anaconda3\lib\tkinter\__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
And this is my code sample.
I would appreciate your help.
from tkinter import *
def messageWindow():
win = Tk()
win.geometry('300x200')
root.destroy()
photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)
Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)
win.mainloop()
root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()
Button(root, text='Bring up Message', command=messageWindow).pack()
root.mainloop()
You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.
Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.
I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it
from tkinter import *
def messageWindow():
win = Toplevel()
win.geometry('300x200')
root.withdraw() # THIS HIDE THE WINDOW
photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)
Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)
root = Tk()
photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()
B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)
root.mainloop()

Importing Tkinter and rowspan/sticky question

I am writing a program to generate draft emails, and I am trying to set it up with a tkinter GUI. I'm on Python 3.5 with tkinter 8.6.
My issue is that I cannot get rowspan to work. I want to have the first row span a few rows, but when I add 'rowspan' the row doesn't change size. If I add 'sticky=tK.N+tK.S', I get errors that those mean nothing. So then I started messing with how I was importing tkinter, trying it as importing from, importing as tk, importing *, but everything I change breaks soemthing else, and still doesn't get the north-south stickiness that I am looking for so that the row will stretch out and fill the space.
I am pretty sure that this is an issue with how I am importing tkinter, and any advice would be tremendously appreciated. I also can't get tkinter to work if I use 'import tkinter as tk" because then I get errors like name 'StringVar' is not defined". I tried fixing that by moving where my root was declared, but that created issues with GUI.
Help! Thanks :)
import os
# pull in GUI stuff
import tkinter as tk #import Tk, Label, Button, W, E, N, S, StringVar, OptionMenu, Entry, Text, END, WORD
from PIL import Image, ImageTk
path = os.getcwd()
# init GUI
class GUI:
def __init__(self, master):
self.master = master
master.title("Email Draft Builder")
# set logo image
f = os.getcwd() + "\\cgs_logo.gif"
# lock in image for use outside this section
im = Image.open(f)
ph = ImageTk.PhotoImage(im)
# vars to show dynamic text for displays 1 and 2
self.box1_titleText = tk.StringVar()
self.box2_titleText = tk.StringVar()
self.box1_content = tk.StringVar()
self.box2_content = tk.StringVar()
# main/container pane info
self.label = tk.Label(master, text="Email Draft Composer", image = ph, bg = "#ffffff")
self.label.image = ph
self.label.grid(columnspan = 3, row = 0, rowspan = 2, sticky=tk.N+
tk.S, column=1)
# ROW 1
self.readLast_button = tk.Button(master, text="Read Training File", command=self.dataOps)
self.readLast_button.grid(row=3, column=0,sticky=tk.W)
# ROW 2
self.file_button = tk.Button(master, text="Unused", command=self.chooseSite)
self.file_button.grid(row=4, column=0,sticky=tk.W)
# ROW 3
self.pullSite_button = tk.Button(master, text="Show Site Info:", command=self.pullSite)
self.pullSite_button.grid(row=5, column=0,sticky=tk.W)
self.getSite = tk.Entry(master)
self.getSite.grid(row=5, column=1,sticky=tk.W)
# ROW 4
self.sendEmail_button = tk.Button(master, text="Build Email", command=self.sendEmail)
self.sendEmail_button.grid(row=6, column=0,sticky=tk.W)
recipients = ["mgancsos#cogstate.com","mgancsos#gmail.com","kkiernan#cogstate.com;mchabon#cogstate.com","ashortland#cogstate.com"]
self.receiver = tk.StringVar()
self.receiver.set(recipients[0])
self.menu1 = tk.OptionMenu(master, self.receiver, *recipients)
self.menu1.grid(row=6, column=1,sticky=tk.W)
# ROW 5
self.close_button = tk.Button(master, text="Close", command=root.destroy)
self.close_button.grid(row=7, column=0,sticky=tk.W)
# ROW 6
self.box1_title = tk.Label(master, textvariable=self.box1_titleText, bg = "#fffff0", borderwidth=1, relief = "groove", width = 15)
self.box1_title.grid(columnspan=1, row = 8, column=0, sticky=tk.W)
self.box1_pane = tk.Label(master, textvariable=self.box1_content, bg = "#fffff0", borderwidth=1, relief = "groove", width = 55)
self.box1_pane.grid(columnspan=1, row = 8, column=1, sticky=tk.W)
# ROW 7
self.box2_title = tk.Label(master, textvariable=self.box2_titleText, bg = "#ffff00", borderwidth=1, relief = "groove", width = 15)
self.box2_title.grid(columnspan=1, row = 9, column=0, sticky='nw')
self.box2_pane = tk.Label(master, textvariable=self.box2_content, bg = "#ffffff", borderwidth=1, relief = "groove", width = 55)
self.box2_pane.grid(columnspan=1, row = 9, column=1, sticky='NW')
# ROW 8
self.display1 = tk.Text(master, wrap=tk.WORD)
self.display1.grid(columnspan=1, row = 10, column=1, sticky=tk.W)
def dataOps(self):
return(1)
def chooseSite(self):
return(1)
def pullSite(self):
return(1)
def sendEmail(self):
return(1)
root = tk.Tk()
root.geometry('800x800')
root["bg"] = "#ffffff"
my_gui = GUI(root)
root.mainloop()

how to display images one after the other in python using tkinter GUI

I have mainly 2 problems in the code:
1) it is showing error "no such directory or file exists" though I have used the same path for an image to another program (though I specified a constant path to only one image) and the images file, this program and the other program, all are in the same working directory
2) it doesn't wait for the wait function, it just executes till 16 and then opens the GUI. I want it to show all the images one by one and change only when I press the 'Next' button
Please suggest any changes in the code which may be required to satisfy the above.
I have Python 3.5.2 in a Windows 10 system.
Thanks in advance
import sys
import tkinter as tk
from PIL import Image,ImageTk,ImageFilter,ImageOps
from msvcrt import getch
def wait():
getch()
return
def classify_obj():
print("In Development")
return
src = "ímages/"
root = tk.Tk()
root.wm_title("Classify Image")
for i in range(1,17):
frame1 = tk.Frame(root, width=500, height=400, bd=2)
frame1.grid(row=1, column=0)
cv1 = tk.Canvas(frame1, height=390, width=490, background="white", bd=1, relief=tk.RAISED)
cv1.grid(row=1,column=0)
im = Image.open(src+str(i)+".jpg")
if (490-im.size[0])<(390-im.size[1]):
width = 490
height = width*im.size[1]/im.size[0]
else:
height = 390
width = height*im.size[0]/im.size[1]
im.thumbnail((width, height), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(im)
cv1.create_image(0, 0, image=photo, anchor='nw')
claButton = tk.Button(master=root, text='Classify', height=2, width=10, command=classify_obj)
claButton.grid(row=0, column=1, padx=2, pady=2)
frame2 = tk.Frame(root, width=500, height=400, bd=1)
frame2.grid(row=1, column=1)
cv2 = tk.Canvas(frame2, height=390, width=490, bd=2, relief=tk.SUNKEN)
cv2.grid(row=1,column=1)
broButton = tk.Button(master=root, text='Next', height=2, width=8, command=wait)
broButton.grid(row=0, column=0, padx=2, pady=2)
print(i)
tk.mainloop()
So on of the big problems with getting the next image the way you are trying to do it is the for loop. What is happening is you have told python the check everything in that loop and perform those functions.
I think one better way would be to first create this program as a class. This way we can avoid calling global variables anywhere in the program. Then inside said class on __init__ we create a list of every photo image inside of our image folder. (Note: I have only tested this with .jpg images). Now that we have a list of all the file names we can then use that list to get a count of how many image files we are working with. With this count we can create a counter that lets us select the next index in the list thus selecting the next image in said list.
By creating all our variables as an attribute of the class we can interact with those attributes without having to declare them as global.
Take the following example. Just change the src variable to your file path.
remember to use . in your file path if your folder is inside your main python workspace. Example: I have a folder called TestImages inside the same directory as my main.py so I can make this variable: src = "./TestImages/".
Take a look at the code below:
import tkinter as tk
from PIL import Image,ImageTk
import os
class ImageClassifyer(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.root.wm_title("Classify Image")
src = "./TestImages/"
self.list_images = []
for d in os.listdir(src):
self.list_images.append(d)
self.frame1 = tk.Frame(self.root, width=500, height=400, bd=2)
self.frame1.grid(row=1, column=0)
self.frame2 = tk.Frame(self.root, width=500, height=400, bd=1)
self.frame2.grid(row=1, column=1)
self.cv1 = tk.Canvas(self.frame1, height=390, width=490, background="white", bd=1, relief=tk.RAISED)
self.cv1.grid(row=1,column=0)
self.cv2 = tk.Canvas(self.frame2, height=390, width=490, bd=2, relief=tk.SUNKEN)
self.cv2.grid(row=1,column=0)
claButton = tk.Button(self.root, text='Classify', height=2, width=10, command=self.classify_obj)
claButton.grid(row=0, column=1, padx=2, pady=2)
broButton = tk.Button(self.root, text='Next', height=2, width=8, command = self.next_image)
broButton.grid(row=0, column=0, padx=2, pady=2)
self.counter = 0
self.max_count = len(self.list_images)-1
self.next_image()
def classify_obj(self):
print("In Development")
def next_image(self):
if self.counter > self.max_count:
print("No more images")
else:
im = Image.open("{}{}".format("./TestImages/", self.list_images[self.counter]))
if (490-im.size[0])<(390-im.size[1]):
width = 490
height = width*im.size[1]/im.size[0]
self.next_step(height, width)
else:
height = 390
width = height*im.size[0]/im.size[1]
self.next_step(height, width)
def next_step(self, height, width):
self.im = Image.open("{}{}".format("./TestImages/", self.list_images[self.counter]))
self.im.thumbnail((width, height), Image.ANTIALIAS)
self.root.photo = ImageTk.PhotoImage(self.im)
self.photo = ImageTk.PhotoImage(self.im)
if self.counter == 0:
self.cv1.create_image(0, 0, anchor = 'nw', image = self.photo)
else:
self.im.thumbnail((width, height), Image.ANTIALIAS)
self.cv1.delete("all")
self.cv1.create_image(0, 0, anchor = 'nw', image = self.photo)
self.counter += 1
if __name__ == "__main__":
root = tk.Tk()
MyApp = ImageClassifyer(root)
tk.mainloop()
There are lots of issues with the current code. I tried to make it work with as little change as possible. The fundamental problem was that the button binding to wait() goes against the principle of having a GUI. So in short, here is a framework from which to get started...
import tkinter as tk
from PIL import Image,ImageTk,ImageFilter,ImageOps
from msvcrt import getch
src = 'images/'
i = 1
def showImage():
global i
# You need to do file exists error checking here.
try:
im = Image.open(src+str(i)+".jpg")
except:
print("No image file named", src+str(i)+".jpg")
return
if (490-im.size[0])<(390-im.size[1]):
width = 490
height = width*im.size[1]/im.size[0]
else:
height = 390
width = height*im.size[0]/im.size[1]
im.thumbnail((width, height), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(im)
label.configure(image=photo)
label.image = photo
i += 1
return
def classify_obj():
print("In Development")
return
root = tk.Tk()
root.wm_title("Classify Image")
frame1 = tk.Frame(root, width=500, height=400, bd=2)
frame1.grid(row=1, column=0)
cv1 = tk.Canvas(frame1, height=390, width=490, background="white", bd=1, relief=tk.RAISED)
label = tk.Label(cv1)
cv1.create_window(0, 0, anchor='nw', window=label)
cv1.grid(row=1,column=0)
claButton = tk.Button(master=root, text='Classify', height=2, width=10, command=classify_obj)
claButton.grid(row=0, column=1, padx=2, pady=2)
frame2 = tk.Frame(root, width=500, height=400, bd=1)
frame2.grid(row=1, column=1)
cv2 = tk.Canvas(frame2, height=390, width=490, bd=2, relief=tk.SUNKEN)
cv2.grid(row=1,column=1)
broButton = tk.Button(master=root, text='Next', height=2, width=8, command=showImage)
broButton.grid(row=0, column=0, padx=2, pady=2)
showImage()
tk.mainloop()

AttributeError: class Frame has no attribute 'StringVar'

So i have this code below. Ive tried a various form of how to get to work the StringVar, but nothing happened. And thats why a turned to you oh, god of stackoverflow. Pls show me how to make it throught. I have an input in Entry1 and I need to get this input into an sql ( ive cut it out because of its uninportant) and return the value of it and write it into Entry1 insted of the original input. Please Lord of SO halp me!
#!usr/bin/python
#-*- coding: utf-8 -*-
import os
import time
import mysql.connector
import getpass
import smtplib
from email.mime.text import MIMEText
global atado_kartya_input
global atvevo_kartya_input
from PIL import Image, ImageTk
#from Tkinter import Tk, Text, TOP, BOTH, X, N, LEFT
from Tkinter import *
from Tkinter import Tk as tk
from ttk import Frame, Style, Entry, Label
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.addbutton()
def addbutton(self):
b = Button( self, text= "Get!", width = 10, command= self.callback)
b.pack()
def callback(self):
#07561847
#tk()
atvevoText = Frame.StringVar()
atvevoText = atvevo(self.entry1.get()) #from the "atvevo" function it gets a name of a worker form an SQL statement
self.entry1.delete(0, 'end')
self.entry1.insert(0, atvevoText)
#self.entry1 = Entry(self, textvariable = atvevoText )
print(atvevoText)
def initUI(self):
self.parent.title("Pozi")
self.pack(fill = BOTH, expand=True)
frame1 = Frame(self)
frame1.pack(fill=X)
lbl1 = Label(frame1, text = "ĂtadĂł kártyája", width = 30)
lbl1.pack(side = LEFT, padx=5, expand=True)
self.entry1 = Entry(frame1)
self.entry1.pack(side = LEFT, padx=5, expand=True)
frame2 = Frame(self)
frame2.pack(fill=X)
lbl2 = Label(frame2, text = "ĂrvevĹ‘ kártyája", width = 30)
lbl2.pack(side = LEFT, padx=5, expand=True)
entry2 = Entry(frame2)
entry2.pack(side = LEFT, padx=5, expand=True)
frame3 = Frame(self)
frame3.pack(fill=X)
lbl3 = Label(frame3, text = "ĂrvevĹ‘ kártyája", width = 30)
lbl3.pack(side = LEFT, padx=5, expand=True)
entry3 = Entry(frame3)
entry3.pack(side = LEFT, padx=5, expand=True)
frame4 = Frame(self)
frame4.pack(fill=BOTH, expand = True)
lbl4 = Label(frame4, text = "Title", width = 30)
lbl4.pack(side = LEFT, anchor=N, padx=5, pady=5)
txt = Text(frame4)
txt.pack(fill = BOTH, padx=5, pady=5, expand=True)
#Style().configure("TFrame", backgroung = "#333") # tframe háttérszinét beállítjuk90%
def main():
root = Tk()
root.geometry("550x450+300+300") # width x heigth + x + y (on screen)
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Update
I have to change in a way like that:
def callback(self):
#07561847
#tk()
atvevoText = StringVar()
number = self.entry1.get()
self.entry1.delete(0, 'end')
#self.entry1.insert(0, atvevoText)
self.entry1 = Entry(self, textvariable = atvevoText )
atvevoText = atvevo(number)
print(atvevoText)
*And with it i got nothing to back nor error nor the value :( *
Change
Frame.StringVar()
to
StringVar()
Since StringVar is a class inside Tkinter(not tested just googled)

python PIL and tkinter; brightening image and displaying it

What I want to be able to do is open a window with two images (one image is an exact copy of the other). Then when I click a button it changes the image on the right. I hope this is making sense. The code I have no is:
from __future__ import division
from Tkinter import *
from PIL import Image, ImageTk, ImageFilter
import random
class MyApp(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("Image examples")
img = Image.open("lineage.jpg").convert("RGB")
(w, h) = (img.size[0], img.size[1])
print (w, h)
tkpi = ImageTk.PhotoImage(img)
label = Label(self.root, image=tkpi)
label.grid(row=0, column=0, padx=5, pady=5, rowspan=10)
img2 = img.copy()
pixels = img2.load()
tkpi2 = ImageTk.PhotoImage(img2)
label = Label(self.root, image=tkpi2)
label.grid(row=0, column=1, padx=5, pady=5, rowspan=10)
Button(self.root, text="Brighten", command=self.brighten).grid(row=0, column= 2)
self.root.mainloop()
def brighten(self):
self.pixels = self.pixels.point(lambda x: x*1.9)
MyApp()
What I am trying to is have img2 update when I click on the brighten button. When I try now I get this error:
File "C:\Users\Admin\Desktop\imageeditor.py", line 36, in brighten
self.pixels = self.pixels.point(lambda x: x*1.9)
AttributeError: 'MyApp' object has no attribute 'pixels'
As you can tell I'm new to programming so any help to send me on the right track would be awesome.
Below is a complete solution that works. These are a few comments on the changes that were made:
Previously the __init__ method never returned because it calls self.root.mainloop() at the end. Which can cause some issues. I restructured it to be more like the hello world example in the python docs.
There is a great Darken/Lighten Example that is what the brighten() method is modeled around.
there was a from Tkinter import *, this replaced by from Tkinter import Frame, Tk, Label, Button. it turns out that both PIL and Tkinter have an attribute called Image which was really confusing to work with. Try and avoid the use of from module import * and instead be explicit in what you are importing this will prevent name space collisions.
code
from Tkinter import Frame, Tk, Label, Button
from PIL import Image, ImageTk, ImageFilter
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
master.wm_title("Image examples")
self.pack()
self.createWidgets()
def createWidgets(self):
self.img = Image.open("lineage.jpg")
self.photo1 = ImageTk.PhotoImage(self.img.convert("RGB"))
self.label1 = Label(self, image=self.photo1)
self.label1.grid(row=0, column=0)
self.photo2 = ImageTk.PhotoImage(self.img.convert("RGB"))
self.label2 = Label(self, image=self.photo2)
self.label2.grid(row=0, column=1)
button = Button(self, text="Brighten", command=self.brighten)
button.grid(row=0, column=2)
def brighten(self):
img2 = self.img.point(lambda p: p * 1.9)
self.photo2 = ImageTk.PhotoImage(img2)
self.label2 = Label(self, image=self.photo2)
self.label2.grid(row=0, column=1)
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
I got a working one.
from __future__ import division
from Tkinter import *
from PIL import Image, ImageTk, ImageFilter
import random
class MyApp(object):
def __init__(self):
self.root = Tk()
self.root.wm_title("Image examples")
img = Image.open("lineage.jpg").convert("RGB")
(self.w, self.h) = (img.size[0], img.size[1])
self.tkpi = ImageTk.PhotoImage(img)
self.label = Label(self.root, image=self.tkpi)
self.label.grid(row=0, column=0, padx=5, pady=5, rowspan=10)
self.img2 = img.copy()
self.pixels = self.img2.load()
self.width, self.height = self.img2.size
self.tkpi2 = ImageTk.PhotoImage(self.img2)
self.label2 = Label(self.root, image=self.tkpi2)
self.label2.grid(row=0, column=1, padx=5, pady=5, rowspan=10)
self.btn = Button(self.root, text="Brighten")
self.btn.grid(row=0, column= 2)
self.btn.bind('<Button-1>', self.brighten)
self.root.mainloop()
def brighten(self,*args):
# self.pixels = self.pixels.point(lambda x: x*1.9)
for i in range(self.w): # for every pixel:
for j in range(self.h):
# print self.pixels[i,j]
self.pixels[i,j] = (int(self.pixels[i,j][0] * 1.9),
int(self.pixels[i,j][1] * 1.9),
int(self.pixels[i,j][2] * 1.9))
self.tkpi2 = ImageTk.PhotoImage(self.img2)
self.label2.configure(image = self.tkpi2)
self.root.update_idletasks()
MyApp()

Categories

Resources