3

I'm trying to place a .png image within a LabelFrame in a Tkinter window. I imported PIL so .png image types should be supported (right?). I can't seem to get the image to show up.

Here is my revised code:

import Tkinter
from Tkinter import *
from PIL import Image, ImageTk

root = Tk()

make_frame = LabelFrame(root, text="Sample Image", width=150, height=150)
make_frame.pack()


stim = "image.png"
width = 100
height = 100
stim1 = stim.resize((width, height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(image.open(stim1))
in_frame = Label(make_frame, image = img)
in_frame.pack()

root.mainloop()

With this code, I got an AttributeError that reads: "'str' has no attribute 'resize'"

rjonnal
  • 1,137
  • 7
  • 17
Mickey
  • 131
  • 3
  • 12
  • use an absolute path for the image file? – Eric Levieil Jul 10 '15 at 18:39
  • Couldn't reproduce. All I did was change `Tkinter` to `tkinter` (I only have PIL installed for Python 3) and `image.png` to the name of a PNG file in the working directory, and it worked fine. – TigerhawkT3 Jul 10 '15 at 18:41
  • I couldn't reproduce it either. It worked perfectly in Python 2.7.9. Could you edit your post and add the exact error text? – rjonnal Jul 10 '15 at 18:42
  • There's a file in my repository named "image.png," so I don't think I need to use an absolute path. @TigerhawkT3 did you change the name from the initial import? – Mickey Jul 10 '15 at 18:44
  • Okay, I got it to work, but my image is too big. Can I shrink it to the size of my `LabelFrame`? – Mickey Jul 10 '15 at 18:46
  • `LabelFrame`, like other container widgets, will expand to the size of its contents. If you want, you can comment out the image code to find the default size of a `LabelFrame`, then resize the image to that size with PIL's `resize()` method. – TigerhawkT3 Jul 10 '15 at 18:48
  • Since I specified the size of my `LabelFrame` can I just fit the image to that size? What arguments does `resize()` take? – Mickey Jul 10 '15 at 18:55
  • I figured it out! Thanks, friends – Mickey Jul 10 '15 at 19:11
  • @Mickey You should post your solution as an answer. – fenceop Jul 10 '15 at 19:29
  • I spoke too soon! I got an error: "'str' object has no attribute 'resize'" – Mickey Jul 10 '15 at 19:56
  • I edited the original question with the code I've got now and the error that comes up. – Mickey Jul 10 '15 at 20:01

1 Answers1

3

@Mickey,

You have to call the .resize method on the PIL.Image object and not the filename, which is a string. Also, you may prefer to use PIL.Image.thumbnail instead of PIL.Image.resize, for reasons described clearly here. Your code was close, but this might be what you need:

import Tkinter
from Tkinter import *
from PIL import Image, ImageTk

root = Tk()

make_frame = LabelFrame(root, text="Sample Image", width=100, height=100)
make_frame.pack()

stim_filename = "image.png"

# create the PIL image object:
PIL_image = Image.open(stim_filename)

width = 100
height = 100

# You may prefer to use Image.thumbnail instead
# Set use_resize to False to use Image.thumbnail
use_resize = True

if use_resize:
    # Image.resize returns a new PIL.Image of the specified size
    PIL_image_small = PIL_image.resize((width,height), Image.ANTIALIAS)
else:
    # Image.thumbnail converts the image to a thumbnail, in place
    PIL_image_small = PIL_image
    PIL_image_small.thumbnail((width,height), Image.ANTIALIAS)

# now create the ImageTk PhotoImage:
img = ImageTk.PhotoImage(PIL_image_small)
in_frame = Label(make_frame, image = img)
in_frame.pack()

root.mainloop()
Community
  • 1
  • 1
rjonnal
  • 1,137
  • 7
  • 17