4

皆さん、PhotoImageクラスのサブサンプルメソッドを使用して、TkinterのLabelウィジェットにリンクするリストにある画像のサイズを変更しようとしていますが、Pythonはそのようなメソッドはないと言っています。Photoimageを呼び出す前にImage.resizeを使用できることは知っていますが、いつでも画像のサイズを変更したいので、PhotoImageに変換されたらどうすればよいかわかりません。誰かが私が間違っていることを教えてもらえますか?私はTkinterに比較的慣れておらず、PILに非常に慣れていません...ありがとう...

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

2 に答える 2

2

悪いニュースでsubsampleありzoom、では利用できずImageTk.PhotoImage、でのみ利用できますTkinter.PhotoImage。後者はPPM、PGM、GIFのみを受け入れ、PythonをTk 8.6b2以降で使用している場合(現時点ではほとんどありません)、PNG画像もサポートされています。

于 2012-12-18T14:03:01.783 に答える
0

多分このコードは問題を解決するでしょう - 私はそれをうまく試しました:

# 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
于 2013-04-02T17:16:06.217 に答える