Python Forum
image.thumbnail(width, height) not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
image.thumbnail(width, height) not working
#1
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, currimage
Program 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()
Reply
#2
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.
PCesarano likes this post
Reply
#3
That was it! Bravo! Thank you.

Patrick
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 1,175 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Fixed colum width for rowLabels i Matplotlib pandabay 0 422 Jun-10-2023, 03:40 PM
Last Post: pandabay
  width of Unicode character Skaperen 6 2,705 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  Help me get this image converter code working? NeTgHoSt 0 2,078 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Applying row height to all rows including and after row 7 curranjohn46 2 6,573 Oct-14-2019, 03:10 PM
Last Post: curranjohn46
  How can I get the width of a string in Python? aquerci 14 16,123 May-27-2019, 06:00 PM
Last Post: heiner55
  fixed width numbers Skaperen 15 8,591 May-27-2019, 09:42 AM
Last Post: Skaperen
  Better Tree Height Function Clunk_Head 2 2,163 Feb-24-2019, 09:15 PM
Last Post: Clunk_Head
  printing text tables with consistent width Skaperen 7 10,672 Jul-01-2018, 02:34 AM
Last Post: Skaperen
  How to measure an inclined beam width and height in image using python? zyb1003 1 3,224 Nov-07-2017, 05:02 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020