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

Recommended Answers

All 2 Replies

You will have to keep the PIL images in your list and convert them to something Tkinter can handle with photo=ImageTk.PhotoImage(image) just before you use them.

Thanks! Still not sure I'm properly differentiating between Tkinter's and Pil's PhotoImage class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.