image.thumbnail(width, height) not working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: image.thumbnail(width, height) not working (/thread-33225.html) |
image.thumbnail(width, height) not working - PCesarano - Apr-07-2021 Good afternoon. My code, and the errors it throws, seem very basic, but I am following all of the instructions to use the thumbnail function that I can find and I'm still getting errors. I've isolated the problematic line, starting with "smallerimage", in the code snippet below. Each image currimage is large ~(3000 x 4000 pixels). My window, however, is always fixed at 800x480. The idea is to take the larger image, and use thumbnail to resize it while keeping the original aspect ratio. However, I keep getting the (abbreviated) error "TypeError: 'int' object is not iterable". The full error, followed by the full code, and given after the code snippet. Thanks in advance. Code snippet: def GetNewImage(): index = random.randint(0, 840) imgstring = "Images/" + str(file_list[index]) currimage = Image.open(imgstring) smallerimage = currimage.thumbnail(800, 480) newimg = ImageTk.PhotoImage(smallerimage) image_layer.configure(image=newimg) image_layer.image = newimg image_layer.update() refresh() return newimg, currimageProgram errors: "C:\Users\Patrick Cesarano\PycharmProjects\GUITutorial\venv\Scripts\python.exe" "C:/Users/Patrick Cesarano/PycharmProjects/GUITutorial/GuiTutorial_experimental.py" Traceback (most recent call last): File "C:\Users\Patrick Cesarano\PycharmProjects\GUITutorial\GuiTutorial_experimental.py", line 92, in <module> mousecall() File "C:\Users\Patrick Cesarano\PycharmProjects\GUITutorial\GuiTutorial_experimental.py", line 68, in mousecall A = GetNewImage() File "C:\Users\Patrick Cesarano\PycharmProjects\GUITutorial\GuiTutorial_experimental.py", line 50, in GetNewImage smallerimage = currimage.thumbnail(800, 480) File "C:\Users\Patrick Cesarano\PycharmProjects\GUITutorial\venv\lib\site-packages\PIL\Image.py", line 2320, in thumbnail x, y = map(math.floor, size) TypeError: 'int' object is not iterable Process finished with exit code 1 Full code: import tkinter as tk import tkinter.font as font import PIL from PIL import ImageTk from PIL import Image import time import os import random HEIGHT = 480 WIDTH = 800 file_list = os.listdir(r"Images") file_number = len(file_list) root = tk.Tk() root.overrideredirect(True) # removes title bar canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH) canvas.pack() frame = tk.Frame(canvas, height=HEIGHT, width=WIDTH) frame.place(relwidth=1, relheight=1) image_layer = tk.Label(frame) image_layer.place(relwidth=1, relheight=1) button_rotateleft = tk.Button(image_layer, text=u"\u21B6", command=lambda: RotateImageLt('')) button_rotateleft.place(relx=0.015, rely=0.93) button_rotateright = tk.Button(image_layer, text=u"\u21B7") button_rotateright.place(relx=0.047, rely=0.93) button_incrementright = tk.Button(image_layer, text=u"\u2190") button_incrementright.place(relx=0.96, rely=0.93) button_incrementleft = tk.Button(image_layer, text=u"\u2192") button_incrementleft.place(relx=0.93, rely=0.93) button_exit = tk.Button(image_layer, text="\u2716", command=root.quit) button_exit.place(relx=0.965, rely=0.01) button_newpicture = tk.Button(image_layer, text=u"\u2764", command=lambda: mousecall()) button_newpicture['font'] = font.Font(size=16) button_newpicture.place(relx=0.015, rely=0.835, relwidth=0.061) def GetNewImage(): index = random.randint(0, 840) imgstring = "Images/" + str(file_list[index]) currimage = Image.open(imgstring) smallerimage = currimage.thumbnail(800, 480) newimg = ImageTk.PhotoImage(smallerimage) image_layer.configure(image=newimg) image_layer.image = newimg image_layer.update() refresh() return newimg, currimage def resizeimage(largeimage): pass def mousecall(): A = GetNewImage() refresh() def refresh(): canvas.update() frame.update() image_layer.update() button_newpicture.flash() button_rotateright.flash() button_rotateleft.flash() button_exit.flash() button_incrementleft.flash() button_incrementright.flash() def RotateImageLt(redimage): imaget = redimage.rotate(270) image_layer.configure(image=imaget) image_layer.image = imaget image_layer.update() refresh() mousecall() root.mainloop() RE: image.thumbnail(width, height) not working - BashBedlam - Apr-08-2021 I cannot get ImageTk to work correctly on my system so I can't be 100% certain, but I think that your problem is in line 50. The width and height should be a tuple like this: smallerimage = currimage.thumbnail((800, 480))Note the double sets of parenthesis. RE: image.thumbnail(width, height) not working - PCesarano - Apr-08-2021 That was it! Bravo! Thank you. Patrick |