i folks,
I'm trying to use the subsample method in the PhotoImage class to resize an image I have in a list that links to a Label widget in Tkinter but python says there is no such method. I know I could use Image.resize before calling Photoimage but since I'd like to resize the image at any time I don't know what to do once it's been converted to PhotoImage. Could someone please show me what I'm doing wrong? I'm relatively new to Tkinter and very new to PIL...Thanks...
from Tkinter import *
from PIL import Image, ImageTk
class imgList(object):
def __init__(self,imgs):
self.list=[]
for i in imgs:
image=Image.open(i)
photo=ImageTk.PhotoImage(image)
self.list.append(photo)
def resize(self,num,fact):
self.list[num]=self.list[num].subsample(fact)
#image=image.resize((50,100),Image.ANTIALIAS)
#photo=ImageTk.PhotoImage(image)
#photo.subsample(2,2)
#self.list[num]=photo
class Slot(object):
def __init__(self,root,canvas,appimg,x,y):
self.root=root
self.canvas=canvas
self.appimage=appimg
self.l=Label(self.root,image=self.appimage)
self.l.place(x=x,y=y)
class View(object):
def __init__(self,canvas):
self.canvas=canvas
class win1(View):
def slotbind(self,event):
print("heloo")
self.imglist.resize(0,2)
def draw(self,root,tv):
self.canvas.delete(ALL)
self.root=root
#self.photolist=pl
self.imglist=imgList(["pic1.bmp"])
self.tv=tv
self.s1=Slot(self.root,self.canvas,self.imglist.list[0],10,10)
self.s1.l.bind("<1>",self.slotbind)
self.qbtn=Button(self.root,text="quit",command=self.quit)
self.qbtn.place(x=270,y=100)
self.qbtn.lift()
def quit(self):
self.qbtn.destroy()
self.s1.l.destroy()
self.tv[1].draw(self.root,self.tv)
class win2(View):
def draw(self,root,tv):
self.canvas.delete(ALL)
self.root=root
self.tv=tv
imglist=imgList(["pic3.bmp","pic4.bmp"])
self.s1=Slot(self.root,self.canvas,imglist.list[1],270,10)
self.qbtn=Button(self.root,text="quit2",command=self.quit)
self.qbtn.place(x=500,y=100)
self.qbtn.lift()
def quit(self):
self.qbtn.destroy()
self.s1.l.destroy()
self.tv[0].draw(self.root,self.tv)
class win3(View):
def draw(self):
pass
class App(object):
def __init__(self, width=640, height=480):
self.width = width
self.height = height
self.root = Tk()
self.root.title("tkinter_test01")
self.root.geometry("%sx%s"%(self.width, self.height))
self.canvas = Canvas(self.root, width=self.width, height=self.height)
self.theviews=[win1(self.canvas),win2(self.canvas),win3(self.canvas)]
self.theviews[0].draw(self.root,self.theviews)
self.canvas.pack()
self.root.mainloop()
app=App()
Bad news, subsample and zoom are not available in ImageTk.PhotoImage, and only in Tkinter.PhotoImage. The later only accepts PPM, PGM, GIF, and if you are using Python with Tk 8.6b2 or later (very unlikely at this time) then there is also support for PNG images.
Maybe this code will solve the problem - I tried it succesfully:
# http://effbot.org/imagingbook/image.htm
import Tkinter
# from PIL
import Image, ImageTk
im = Image.open('z:/g1.jpg')
# imc=im.copy()
imc=im.transpose(Image.ROTATE_270)
(x0,y0,x1,y1)=imc.getbbox() # returns (0,0,w,h)
print 'x0=%d y0=%d x1=%d y1=%d\n' % (x0,y0,x1,y1)
tkroot=Tkinter.Tk()
# tki=ImageTk.PhotoImage(im)
# tkl=Tkinter.Label(tkroot, image=tki)
# tkl.pack()
imc.thumbnail((1+x1/5,1+y1/5)) # changes image in place!
tkic=ImageTk.PhotoImage(imc)
tklc=Tkinter.Label(tkroot,image=tkic)
tklc.pack()
tkroot.mainloop() # Start the GUI
Related
I was just wondering, is there any way in tkinter in python to resize/rescale a picture with the mouse (similar to how you can rescale images in google docs)?
The code I have so far:
def addImage(self, docx):
filename = filedialog.askopenfile(title="Select a file")
myImage = ImageTk.PhotoImage(Image.open(filename.name))
position = docx.index(INSERT)
docx.image_create(position, image=myImage)
docx.photo = myImage
# here I want to be able to resize the image I have put in my document with the mouse
Thanks in advance
I have tried this
from tkinter import *
from PIL import Image,ImageTk
class Resizable:
def __init__(self,label,image):
self.label=label
self.image=image
self.label.master.bind('<Motion>',self.position)
self.flag=False
def position(self,event):
self.dimentions=(self.label.winfo_width(),self.label.winfo_height())
self.x,self.y = event.x,event.y
if (
self.x in range (self.dimentions[0]-5,self.dimentions[0],1) and
self.y in range (self.dimentions[1]-5,self.dimentions[1],1)
):
self.label.config(cursor='sizing')
self.label.master.bind('<ButtonRelease-1>',self.end)
self.label.bind('<Button-1>',self.start)
else:
self.label.config(cursor='')
self.label.unbind('<Button-1>')
def end(self,event):
self.flag=True
self.label.master.unbind('<ButtonRelease-1>')
def start(self,event):
self.flag=False
self.resize()
def resize(self):
if not self.flag:
self.label.config(cursor='sizing')
try:
self.photoimage=ImageTk.PhotoImage(self.image.resize((self.x,self.y),Image.ANTIALIAS))
except:
pass
self.label.config(image=self.photoimage)
self.label.update()
self.label.after(1,self.resize)
root=Tk()
root.geometry('400x300')
img_label=Label(bd=0)
img_label.pack()
image=Image.open('sample.png')
photoimage=ImageTk.PhotoImage(image.resize((100,100),Image.ANTIALIAS))
img_label.config(image=photoimage)
Resizable(img_label,image)
root.mainloop()
I believe you have a different way of inserting your image, but you can modify and improve this approach as per your requirement.
Hi i'm trying to create a class constructor to handle images in tkinter, since i'll be using many images in my code so I need a way to use images without using so much line of code.
I keep getting the error:
return _modes[mode]
KeyError: 'picture.png'
Here's the code:
from tkinter import *
import tkinter
from random import randint
from PIL import Image
from PIL import ImageTk
root = tkinter.Tk()
root.geometry('700x700')
class PhotoHandler:
def __init__(self,imagepath):
self.imagepath = imagepath
self.image = Image.open(self.imagepath)
self.image = ImageTk.PhotoImage(self.imagepath)
def returnn(self):
return self.image
search = PhotoHandler('picture.png').returnn()
root.mainloop()
You passed a string to the tkinter image. It expects a PIL image or mode. Since you passed a string, it thinks you're using an invalid mode. Pass the image instead:
class PhotoHandler:
def __init__(self,imagepath):
self.imagepath = imagepath
self.image = Image.open(self.imagepath)
self.image = ImageTk.PhotoImage(self.image)
def returnn(self):
return self.image
search = PhotoHandler('picture.png').returnn()
# add label for background image
background_label = tkinter.Label(root, image=search)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
Here's the documentation:
https://pillow.readthedocs.io/en/4.2.x/reference/ImageTk.html
I want to make a class that has a picture and it is changed to the next one by mouse click.I'm new to oop, my idea here was to make class similar to real life where there is new class instance for every new picture, is it possible to do it this way? Here is my code
import tkinter as tk
from PIL import Image,ImageTk
class Picture():
_count=1
def __init__(self,window):
self.id=Picture._count
Picture._count+=1
self.img=Image.open(r'C:\ImgArchive\img%s.png' % self.id)
self.pimg = ImageTk.PhotoImage(self.img)
self.lab=tk.Label(window,image=self.pimg)
self.lab.pack()
self.lab.bind('<1>',self.click)
def click(self,event):
self.lab.destroy()
self=self.__init__(window)
window = tk.Tk()
window.title('Album')
window.geometry('1200x900')
pic=Picture(window)
window.mainloop()
It works fine, but i'm not sure that old instances of my class is deleted, are they? And i use self.lab.destroy() because if i dont new picture appears down, like this
instead of this
So why it happens?What is elegant way for it?
Below example produces a simple image viewer tested with path of C:\Users\Public\Pictures\Sample Pictures, let me know if anything's unclear:
import tkinter as tk
from PIL import Image, ImageTk
#required for getting files in a path
import os
class ImageViewer(tk.Label):
def __init__(self, master, path):
super().__init__(master)
self.path = path
self.image_index = 0
self.list_image_files()
self.show_image()
self.bind('<Button-1>', self.show_next_image)
def list_files(self):
(_, _, filenames) = next(os.walk(self.path))
return filenames
def list_image_files(self):
self.image_files = list()
for a_file in self.list_files():
if a_file.lower().endswith(('.jpg', '.png', '.jpeg')):
self.image_files.append(a_file)
def show_image(self):
img = Image.open(self.path + "\\" + self.image_files[self.image_index])
self.img = ImageTk.PhotoImage(img)
self['image'] = self.img
def show_next_image(self, *args):
self.image_index = (self.image_index + 1) % len(self.image_files)
self.show_image()
root = tk.Tk()
mypath = r"C:\Users\Public\Pictures\Sample Pictures"
a = ImageViewer(root, mypath)
a.pack()
root.mainloop()
How to display a Numpy matrix, as a bitmap, into a Tkinter canvas?
More precisely, how to fill a PhotoImage with content from a matrix?
photo = ImageTk.PhotoImage(...)
self.canvas.create_image(0,0,image=photo,anchor=Tkinter.NW)
Here is working solution, slightly modified to make it work (some function was deprecated) and to simplify it to keep only the necessary part. We have to use Image.frombytes(...) to read the data in the numpy matrix.
import Tkinter
from PIL import Image, ImageTk
import numpy
class mainWindow():
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)
data=numpy.array(numpy.random.random((400,500))*100,dtype=int)
self.im=Image.frombytes('L', (data.shape[1],data.shape[0]), data.astype('b').tostring())
self.photo = ImageTk.PhotoImage(image=self.im)
self.canvas.create_image(0,0,image=self.photo,anchor=Tkinter.NW)
self.root.update()
self.root.mainloop()
mainWindow()
I'm trying to show a jpg image as background for a GUI thing I'm building.
I can get it to work in a single method:
from Tkinter import *
from PIL import Image, ImageTk
class MakeGUI(object):
master = None
w = None
def __init__(self):
self.canvasSizeY = 400 #height
self.canvasSizeX = 640 #width
def setupCanvas(self):
"""
preps the canvas for drawing.
"""
self.master = Tk()
self.w = Canvas(self.master, width=self.canvasSizeX, height=self.canvasSizeY)
self.w.config(bg='white')
image = Image.open("background.jpg")
photo = ImageTk.PhotoImage(image)
self.w.create_image(0,0, image=photo, anchor=NW)
self.w.pack()
mainloop()
def main():
makeGUI = MakeGUI()
makeGUI.setupCanvas()
if __name__ == '__main__':
main()
But when I try and make the canvas in one method, and show the canvas in another, it doesn't show the jpg (when I've been testing, I've created and shown & text and rectangles using this approach):
from Tkinter import *
from PIL import Image, ImageTk
class MakeGUI(object):
master = None
w = None
def __init__(self):
self.canvasSizeY = 400 #height
self.canvasSizeX = 640 #width
def setupCanvas(self):
"""
preps the canvas for drawing.
"""
self.master = Tk()
self.w = Canvas(self.master, width=self.canvasSizeX, height=self.canvasSizeY)
self.w.config(bg='white')
image = Image.open("background.jpg")
photo = ImageTk.PhotoImage(image)
self.w.create_image(0,0, image=photo, anchor=NW)
def showImage(self):
"""
pushes the image to the screen
"""
self.w.pack()
self.w.mainloop()
def main():
makeGUI = MakeGUI()
makeGUI.setupCanvas()
if __name__ == '__main__':
main()
I want to use the GUI dynamically to show some text as I work through some editing, so I'm interested to understand what I've got wrong before I get too far into the build in case its a showstopper...
The most obvious problem is that in the second case you are never calling showImage. Even after you do call that function, your image probably won't show up. Images will be garbage-collected if there isn't a reference to them. It may seem like there's a reference because you're adding it to a canvas, but that isn't enough.
You'll need to do something like:
self.photo = ImageTk.PhotoImage(image)
Finally, I recommend that you take the call to mainloop out of showImage. mainloop must always be called exactly once, so most typically it is the last line of code in your program, or the last line of code in your main function.
A more common way to make a Tkinter application is to subclass either the Tk object or a Frame object, rather than having your main application be a generic object. For example:
class MyApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
...
self.setupCanvas(...)
...
if __name__ == "__main__":
app = MyApp()
app.mainloop()