QRCode displaying in tkinter GUI python - python

I am trying to display a QR Code in a tkinter GUI, however when I execute this code:
import tkinter as tk
from PIL import Image,ImageTk
import pyqrcode
from tkinter.font import Font
import random
root=tk.Tk()
root.title("QR Lottery")
root.config(bg="white")
# Defining Fonts
TitleFont = Font(family="HEX:gon Staggered 2", size="48")
def generateQR():
num=random.randint(1,2)
if num==1:
QRCode=pyqrcode.create("You Win!")
QRCode.png("QRCode.png",scale=8)
img = Image.open('QRCode.png')
QRCodeImg = ImageTk.PhotoImage(img)
QRCodeLabel=tk.Label(image=QRCodeImg)
QRCodeLabel.grid(row=2,column=1)
else:
QRCode=pyqrcode.create("You Lose!")
QRCode.png("QRCode.png",scale=8)
img = Image.open('QRCode.png')
QRCodeImg = ImageTk.PhotoImage(img)
QRCodeLabel=tk.Label(image=QRCodeImg)
QRCodeLabel.grid(row=2,column=1)
#Labels
TitleLabel=tk.Label(text="qr lottery",bg="white",font=TitleFont)
TitleLabel.grid(row=1,column=1,columnspan=5)
ButtonQR=tk.Button(text="Generate!",bg="white",command=generateQR)
ButtonQR.grid(row=3,column=1)
root.mainloop()
The Image Label produced is a blank square. I am unsure of why this is, as I left the background color blank.

Question: The Image Label produced is a blank square. I am unsure of why this is
A:You must keep a reference to the image object in your Python program, by attaching it to another object.
Use the following:
Define your own widget QRCodeLabel by inherit from tk.Label.
Init only with parameter parent
class QRCodeLabel(tk.Label):
def __init__(self, parent, qr_data):
super().__init__(parent)
print('QRCodeLabel("{}")'.format(qr_data))
Create your QRCode with the passed qr_data and
save as PNG file.
qrcode = pyqrcode.create(qr_data)
tmp_png_file = "QRCode.png"
qrcode.png(tmp_png_file, scale=8)
Create a image object from the PNG file.
Tkinter can handle PNG image files by its own, no PIL needed.
NOTE: You have to use self.image to prevent garbage collection!
self.image = tk.PhotoImage(file=tmp_png_file)
Configure this Label with the self.image
self.configure(image=self.image)
Usage:
class App(tk.Tk):
def __init__(self):
super().__init__()
buttonQR = tk.Button(text="Generate!", bg="white", command=self.generateQR)
buttonQR.grid(row=2, column=0)
self.qr_label = None
def generateQR(self):
if self.qr_label:
self.qr_label.destroy()
self.qr_label = QRCodeLabel(self, random.choice(["You Win!", "You Lose!"]))
self.qr_label.grid(row=1, column=0)
if __name__ == "__main__":
App().mainloop()
Tested with Python: 3.5

Related

I am getting an AttributeError when I try to resize an image I have inserted to a Tkinter window

I am trying to resize an image I inserted in my Tkinter window, but keep receiving this error message: "AttributeError: 'PhotoImage' object has no attribute 'resize'"
This is my code to resize the image:
self.path = 'S:/Öffentliche Ordner/Logos/Core Solution/Logo/CoreSolution_Logo_RGB.jpg'
self.img = ImageTk.PhotoImage(Image.open(self.path))
self.resized = self.img.resize(50,50)
self.new_img = ImageTk.PhotoImage(self.resized)
self.label = Label(master, image = self.new_img)
self.label.pack()
self.Main = Frame(self.master)
How can I resolve this error? All help is welcomed and appreciated.
As in this tutorial, it looks like it is easier to import the file as an image. Then resize it, then convert it to PhotoImage. Can you give it a try ?
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Load the image
image=Image.open('download.png')
# Resize the image in the given (width, height)
img=image.resize((450, 350))
# Conver the image in TkImage
my_img=ImageTk.PhotoImage(img)
# Display the image with label
label=Label(win, image=my_img)
label.pack()
win.mainloop()
https://www.tutorialspoint.com/resizing-images-with-imagetk-photoimage-with-tkinter
As far as I can see, the Pillow Image.PhotoImage class is meant for displaying in tkinter but does not have all the methods of the tkinter.PhotoImage class.
Easiest is to resize the Pillow.Image before converting to Pillow Image.PhotoImage.
from tkinter import *
from PIL import Image, ImageTk
master = Tk()
path = 'images/cleese.png'
img = Image.open(path)
img.thumbnail((50,50)) # Resize Pillow Image
new_img = ImageTk.PhotoImage(img) # Convert
label = Label(master, image=new_img)
label.pack()
master.mainloop()
Try this. I did not test.
path = Image.open('S:/Öffentliche Ordner/Logos/Core Solution/Logo/CoreSolution_Logo_RGB.jpg'
self.resized = path.resize{(50,50),Image.ANTIALIAS)
self.img = ImageTk.PhotoImage(self.resized)
self.label = Label(master, image = self.new_img)
self.label.pack()
self.Main = Frame(self.master)

python canvas image not rendered

I followed a few tutorials and I am trying to implement a clean object oriented code for python gui. The idea of my project is opening an image and drawing circles on it with mouse click.
There are 2 issues:
1)
In the code below, the canvas does not render the image.the image should be taken from the user
In java there is a repaint function that is called by VM so that the contents of the frame can be updated. I couldn't find something in python (No, I am not willing to try adding timer function to redraw every second.)
2)
Besides HELLO is not printed even I press f key. The keylistener is also not working
I have done GUI with Java before but Python code looks very dirty comparing Java.
I would be grateful if you can help.
from tkinter import *
from PIL import Image, ImageTk
from tkinter import Tk, Canvas, Frame, BOTH
from tkinter import filedialog
class Example(Frame):
def __init__(self, container):
super().__init__()
self.container = container
self.canvas = None
self.initUI()
def initUI(self):
fname=filedialog.askopenfilename(title='"pen')
self.canvas = Canvas(self.container)
self.canvas.pack(anchor='nw', fill='both', expand=1)
self.canvas.focus_set()
self.canvas.bind("<ButtonRelease-1>", self.mouseReleased)
self.canvas.bind("<Key>", self.pressedkey)
image = Image.open(fname)
image = image.resize((400, 400))
image = ImageTk.PhotoImage(image)
image_id=self.canvas.create_image(0, 0, image=image, anchor='nw')
def mouseReleased(self, event):
self.canvas.create_oval(event.x-10, event.y-10, event.x+10, event.y+10, width=2, fill='blue')
def pressedkey(self,event):
pressedkey=str(repr(event.char))
print(pressedkey,type(pressedkey))
if pressedkey.__eq__("f"):
print("HELLO")
def main():
root = Tk()
ex = Example(root)
root.geometry("400x250+300+300")
root.mainloop()
if __name__ == '__main__':
main()

Display image with buttons/text in tkinter

I'm a beginner trying to use tkinter to create a GUI. I've adapted code from other sources for my own use.
At the moment I'm trying to display an image along with a button.
However, when running the code, only the image displays no matter where I move the button, the button isn't displayed.
Would be grateful for any help.
Additional functionality:
I'm looking to implement a function so that I can move the image around using coordinates, also a function that allows me to use .jpg instead of .png.
Thanks in advance!
Code:
from tkinter import *
from PIL import Image, ImageTk
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("WindowName")
self.pack(fill = BOTH, expand = 1)
quitButton = Button(self, text = "quit", command=self.client_exit)
quitButton.place(x=400,y=400)
self.showImg()
def client_exit(self):
exit() # Or another function
def showImg(self):
self.grid(row=0)
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.original = Image.open('wire.png')
resized = self.original.resize((200, 200),Image.ANTIALIAS)
self.image = ImageTk.PhotoImage(resized)
self.display = Label(self, image = self.image)
self.display.grid(row=0)
root = Tk()
root.geometry("600x600")
app = Window(root)
root.mainloop()

Cannot construct tkinter.PhotoImage from PIL Image

I try to show a image in a label when I push a button, but the image are too large and I have tried to resize the image. I have created this function:
def image_resize(imageFile):
width = 500
height = 300
image = Image.open(imageFile)
im2 = image.resize((width, height), Image.ANTIALIAS)
return im2
To show the image I have created this function:
def show_image():
label_originalimage ['image'] = image_tk
And the button with the command=show_image:
filename = 'bild_1.jpg'
image_resize = image_resize(filename)
image_tk = PhotoImage(image_resize)
button_open = Button(frame_open, text='Open Image', command=show_image)
I get only this:
TypeError : __str__ returned non-string (type instance)
The PhotoImage class from tkinter takes a filename as an argument, and as it cannot convert the image into a string, it complains. Instead, use the PhotoImage class from the PIL.ImageTk module. This works for me:
from tkinter import *
from PIL import ImageTk, Image
def image_resize(imageFile):
width = 500
height = 300
image = Image.open(imageFile)
im2 = image.resize((width,height), Image.ANTIALIAS)
return im2
def show_image():
label_originalimage ['image'] = image_tk
root = Tk()
filename = './Pictures/Space/AP923487321702.jpg'
image_resize = image_resize(filename)
image_tk = ImageTk.PhotoImage(image_resize)
label_originalimage = Label(root)
label_originalimage.pack()
button_open = Button(root, text='Open Image', command=show_image)
button_open.pack()
root.mainloop()
Notice the change from image_tk = PhotoImage(image_resize) to image_tk = ImageTk.PhotoImage(image_resize).
I had the same problem when I try to construct a canvas image item for
tkinter from a tkinter PhotoImage. The latter was constructed from some
image data in memory (in my case an opencv image). The same exception
occurs if I simply try to convert the PhotoImage to a string.
I guess there is a bug in the conversion method __str__ of the PhotoImage,
making it simply returns the image source. If constructed from a file name
(see below) this works fine. If constructed from some image data, this is not
of type string and yields an exception.
Unfortunately, using the compatible PhotoImage from PIL's
ImageTk module like matsjoyce suggested didn't help me either because I experienced an even worse problem, probably a platform or library version dependent bug (I used OS X 10.11.6, python 3.5, tkinter 8.6, PIL 1.1.7): Now the python script crashed at the construction of the canvas image item with a "Bus Error".
The only workaround I am aware of was to store the image data into a temporary file and use a tkinter PhotoImage constructed from that file name. (Trying the same with the PIL PhotoImage still crashes.)
#!/usr/bin/env python3
import tkinter
import tempfile
import cv2
def opencv2photoimg(opencv_img):
"""Convert OpenCV (numpy) image to tkinter photo image."""
# ugly workaround: store as file & load file, because direct
# construction leads to a crash on my platform
tmpfile = tempfile.NamedTemporaryFile(suffix='.png', delete=True)
# ^^^ I am using PNGs only, you might want to use another suffix
cv2.imwrite(tmpfile.name, opencv_img)
return tkinter.PhotoImage(file=tmpfile.name)
# load image
img = cv2.imread('test.png')
# do something w/ the image ...
# setup tk window w/ canvas containing an image
root = tkinter.Tk()
canvas = tkinter.Canvas(root, width=img.shape[1], height=img.shape[0])
canvas.pack()
# keep reference to PhotoImage to avoid it being garbage collected
# (well known tkinter bug for canvas image items)
photo_img = opencv2photoimg(img)
# create a canvas item
img_item = canvas.create_image(0, 0, anchor=tkinter.NW, image=photo_img)
# display the window
tkinter.mainloop()
I do not think it's elegant, but it works.
Yes it works, but yeeeucchh - what I way to have to do it.
Surely there is a better way.
Here is my test code I got to starting from here....
import tkinter
from PIL import Image
import numpy
import time
import io
#python2 version (original) -> 120fps
#full physical file io and new image each cycle -> 130fps
#reuse PIL Image instead of create new each time -> 160fps
class mainWindow():
times=1
timestart=time.clock()
data=numpy.array(numpy.random.random((400,500))*100,dtype=int)
theimage = Image.frombytes('L', (data.shape[1],data.shape[0]),data.astype('b').tostring())
def __init__(self):
self.root = tkinter.Tk()
self.frame = tkinter.Frame(self.root, width=500, height=400)
self.frame.pack()
self.canvas = tkinter.Canvas(self.frame, width=500,height=400)
self.canvas.place(x=-2,y=-2)
self.root.after(0,self.start) # INCREASE THE 0 TO SLOW IT DOWN
self.root.mainloop()
def start(self):
global data
global theimage
self.theimage.frombytes(self.data.astype('b').tobytes())
self.theimage.save('work.pgm')
self.photo = tkinter.PhotoImage(file='work.pgm')
self.canvas.create_image(0,0,image=self.photo,anchor=tkinter.NW)
self.root.update()
self.times+=1
if self.times%33==0:
print("%.02f FPS"%(self.times/(time.clock()-self.timestart)))
self.root.after(10,self.start)
self.data=numpy.roll(self.data,-1,1)
if __name__ == '__main__':
x=mainWindow()
Here it is: I found that the input data to photoimage can be a byte array that looks like a ppm file, although it only appears to work on a subset of legal ppm (e.g. 16 bit values don't work)
So for future reference.......
import tkinter
import numpy
import time
#python2 version (original) -> 120fps
#full physical file io and new image each cycle -> 130fps
#reuse PIL Image instead of create new each time -> 160fps
#and... direct image into tkinter using ppm byte array -> 240 fps
class mainWindow():
times=1
timestart=time.clock()
data=numpy.array(numpy.random.random((400,500))*900,dtype=numpy.uint16)
def __init__(self):
self.root = tkinter.Tk()
self.frame = tkinter.Frame(self.root, width=500, height=400)
self.frame.pack()
self.canvas = tkinter.Canvas(self.frame, width=500,height=400)
self.canvas.place(x=-2,y=-2)
xdata = b'P5 500 400 255 ' + self.data.tobytes()
self.photo = tkinter.PhotoImage(width=500, height=400, data=xdata, format='PPM')
self.imid = self.canvas.create_image(0,0,image=self.photo,anchor=tkinter.NW)
self.root.after(1,self.start) # INCREASE THE 0 TO SLOW IT DOWN
self.root.mainloop()
def start(self):
global data
xdata = b'P5 500 400 255 ' + numpy.clip(self.data,0,255).tobytes()
self.photo = tkinter.PhotoImage(width=500, height=400, data=xdata, format='PPM')
if True:
self.canvas.itemconfig(self.imid, image = self.photo)
else:
self.canvas.delete(self.imid)
self.imid = self.canvas.create_image(0,0,image=self.photo,anchor=tkinter.NW)
self.times+=1
if self.times%33==0:
print("%.02f FPS"%(self.times/(time.clock()-self.timestart)))
self.root.update()
self.root.after(0,self.start)
self.data=numpy.roll(self.data,-1,1)
if __name__ == '__main__':
x=mainWindow()

Why it shows blank instead of picture in my Tkinter program?

I want to use Tkinter to display all the pictures in a specific directory, so I firstly code to show all the pictures from a given list
The code is:
import os
from Tkinter import *
from PIL import Image, ImageTk
class Application(Frame):
def add_pic_panel(self, pic):
img = ImageTk.PhotoImage(Image.open(pic))
label = Label(root, image = img)
print label
return label
def create_gui(self):
pics = ['1.jpg', '2.jpg']
for pic in pics:
self.add_pic_panel(pic)
pass
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.create_gui()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
Environment: Mac OS 10.9, Python 2.7.5
How could I display all the pictures in the list?
The code does not pack the label.
Should keep the reference for the image.
def add_pic_panel(self, pic):
img = ImageTk.PhotoImage(Image.open(pic))
label = Label(self.master, image=img)
label.img = img # to keep the reference for the image.
label.pack() # <--- pack
return label
BTW, add_pic_panel use root directly. It would be better to use self.master.
root.destroy() at the last line cause TclError when closing the window. Remove the line.

Categories