image resize doesn't work

Peter Otten __peter__ at web.de
Sun Aug 1 14:08:28 EDT 2010


Chris Hare wrote:

 
> On Aug 1, 2010, at 10:24 AM, rantingrick wrote:
> 
>> On Aug 1, 7:35 am, Chris Hare <ch... at labr.net> wrote:
>>> I have the following chunk of code.  Although it seems to execute fine,
>>> no errors
>> 
>> Not True! it contains syntax errors. Check the posted code and next
>> time post all the code.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> 
> Hmmm... ok
> here is the code.  I get no errors on my console when it execute
> 
> urllib.urlretrieve(findu, "image.png")

I get a NameError on the very first line.

>>> urllib.urlretrieve(findu, "image.png")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined

When you want to demonstrate a problem try to make a self-contained example, 
i. e. one that can be run without the need for us guess the surrounding 
code. Remove everything that is irrelevant for the problem like the logging 
in code below and the png/gif conversion gymnastics.

Anyway, here is a self-contained demo (you have to pass the filename of an 
image on the commandline):

import Tkinter
import ImageTk
import Image
import sys

[filename] = sys.argv[1:]

image = Image.open(filename)

root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame.pack()
label = Tkinter.Label(root)
label.pack()

def make_resize(percent):
    def resize():
        width, height = image.size
        label.image = label["image"] = ImageTk.PhotoImage(
            image=image.resize((width*percent//100, height*percent//100)))
    return resize

make_resize(100)()

pairs = [
    ("Small", 20),
    ("Medium", 50),
    ("Original", 100),
    ("Big", 200)]

for i, (text, percent) in enumerate(pairs):
    button = Tkinter.Button(frame, text=text, command=make_resize(percent))
    button.grid(row=0, column=i)

root.mainloop()

Peter



More information about the Python-list mailing list