Home > Mobile >  I am getting an AttributeError when I try to resize an image I have inserted to a Tkinter window
I am getting an AttributeError when I try to resize an image I have inserted to a Tkinter window

Time:06-10

I am trying to resize an image I inserted in my Tkinter window, but keep receiving this error message: "AttributeError: 'PhotoImage' object has no attribute 'resize'" This is my code to resize the image:

    self.path = 'S:/Öffentliche Ordner/Logos/Core Solution/Logo/CoreSolution_Logo_RGB.jpg'
    self.img = ImageTk.PhotoImage(Image.open(self.path))
    self.resized = self.img.resize(50,50)
    self.new_img = ImageTk.PhotoImage(self.resized)

    self.label = Label(master, image = self.new_img)
    self.label.pack()
    self.Main = Frame(self.master)

How can I resolve this error? All help is welcomed and appreciated.

CodePudding user response:

As in this tutorial, it looks like it is easier to import the file as an image. Then resize it, then convert it to PhotoImage. Can you give it a try ?

https://www.tutorialspoint.com/resizing-images-with-imagetk-photoimage-with-tkinter

CodePudding user response:

As far as I can see, the Pillow Image.PhotoImage class is meant for displaying in tkinter but does not have all the methods of the tkinter.PhotoImage class.

Easiest is to resize the Pillow.Image before converting to Pillow Image.PhotoImage.

from tkinter import *
from PIL import Image, ImageTk

master = Tk()

path = 'images/cleese.png'
img = Image.open(path)
img.thumbnail((50,50))      # Resize Pillow Image
new_img = ImageTk.PhotoImage(img)   # Convert

label = Label(master, image=new_img)
label.pack()

master.mainloop()
  • Related