I'm trying to display a resized image, but I'm not sure how to do that using classes. This code works fine:
image = Image.open(Image_Location)
image = image.resize((200, 200), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
But I need to do this in a class method (because I need multiple for the main project and it would take too long to do it seperately), how do I do it? This is what I have tried so far:
class Planet:
def __init__(self, name, picture):
self.name = name
self.picture = tk.PhotoImage(file=picture)
@classmethod
def resize_image(cls, picture):
image = Image.open(picture)
image = image.resize((200, 200), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
earth = Planet("earth",Image_Location)
earth_resized = earth.resize_image(Image_Location)
test1 = Label(root, image=earth_resized)
test2 = Label(root, image=earth.picture)
test1.pack()
test2.pack()
And when I pack both labels, I get a white space (I think that is supposed to be the resized image) and the unsized image. Thanks!
CodePudding user response:
You forgot to return the resized image from the method -- it should look like this:
@classmethod
def resize_image(cls, picture):
image = Image.open(picture)
image = image.resize((200, 200), Image.ANTIALIAS)
return ImageTk.PhotoImage(image)
