Apr-07-2021, 02:59 PM
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:
"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:
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()
