Probleme image PILimage pour PhotoImage - pyimage doesnt exists
Bonjour à tous,
Est-ce que quelqu'un peut m'expliquer pourquoi ce code ne fonctionne pas?
Je voudrais sauver l'image dans une variable pour permettre de préserver sa qualité et la redimensionner pour une utilisaiton ultérieure.
J'ai fais une simulation en mouse enter et mouse leave.
Le mouseleave pose un problème et je ne comprends pas pourquoi...
L'image est bien présente puisque qu'un image.show fonctionne avant d'essayer de modifier l'image du label...
bref je ne comprends pas...
Merci de vos lumières !
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| from functools import partial
from tkinter import *
import pdf2image
from PIL import Image, ImageTk
class custombutton(Button):
def click(self):
print(self)
def __init__(self, master, image, width=200, height=10, previewlabel=None, **kwargs):
Button.__init__(self, master, **kwargs)
self.image=image
self.width = width
self.height = height
self.previewlabel=previewlabel
self['width']=self.width
self['height']=self.height
self['image']=image
self['command']=self.click
self.pack()
def mouseover(image, e):
lblimage['image']=image
def mouseleave(image, e):
imageresized= ImageTk.PhotoImage(image.resize((200,200) , Image.NEAREST)) #-------- NOT WORKING
lblimage['image'] =imageresized
win= Tk()
win.title("Hello")
win.geometry("700x700")
lblimage=Label(text="aaa")
lblimage.pack()
pages = pdf2image.convert_from_path('C:/Users/user1/test.pdf', poppler_path=r'C:\python\Lib\poppler-21.03.0\Library\bin')
i=0
for im in pages:
i+=1
srcimg=im
resizedimg=ImageTk.PhotoImage(srcimg.resize((200,200) ,Image.NEAREST))
button = custombutton(win, resizedimg, previewlabel=lblimage)
button.bind("<Enter>", partial(mouseover,resizedimg))
button.bind("<Leave>", partial(mouseleave,srcimg)) #----------------- NOT WORKING
win.mainloop() |