image resize doesn't work

Discussion in 'Python' started by Chris Hare, Aug 1, 2010.

  1. Chris Hare

    Chris Hare Guest

    I have the following chunk of code. Although it seems to execute fine, no errors, the image is never resized. What am I missing?

    imagePNG = Image.open("image.png")
    photo = ImageTk.PhotoImage(imagePNG
    canvasWidth = 300
    canvasHeight = 275
    photo = ImagePNG.resize((canvasWidth,canvasHeight),Image.ANTIALIAS)
    netRadarImage = Label(w, image=photo)
     
    Chris Hare, Aug 1, 2010
    #1
    1. Advertising

  2. Chris Hare

    rantingrick Guest

    On Aug 1, 7:35 am, Chris Hare <> 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.
     
    rantingrick, Aug 1, 2010
    #2
    1. Advertising

  3. Chris Hare

    Chris Hare Guest

    On Aug 1, 2010, at 10:24 AM, rantingrick wrote:

    > On Aug 1, 7:35 am, Chris Hare <> 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")
    logging.debug("createRadarWidgets(): radar download complete")
    logging.debug("createRadarWidgets(): user has radarPanelSize of " + root.radarPanelSize.get())
    #
    # open the image file
    #
    if os.path.exists("image.gif"):
    ctime = os.stat(dbPath)[ST_CTIME]
    print ctime
    filetime = datetime.fromtimestamp(ctime)
    filetime = filetime.strftime("%Y%m%d%H%M%S")
    print str(filetime)
    #filetime = datetime(ctime).isoformat()
    #print ctime
    imagePNG = Image.open("image.png")
    #photo = ImageTk.PhotoImage(imagePNG)
    Image.open("image.png").save("image.gif")
    image = Image.open("image.gif")
    photo = PhotoImage(file="image.gif")
    #photoimg = ImageTk.PhotoImage(photo)
    #
    # NOTE: - will want to track the size of the image displayed based upon
    # the actual screen resolution
    # Full/External = 600x550 - full file size
    # Large = 450x412
    # Medium = 300x275
    # Small = 150x137
    #
    if root.radarPanelSize.get() == "Large":
    canvasWidth = 450
    canvasHeight = 412
    elif root.radarPanelSize.get() == "Medium":
    canvasWidth = 300
    canvasHeight = 275
    elif root.radarPanelSize.get() == "Small":
    canvasWidth = 150
    canvasHeight = 137
    logging.debug("createRadarWidgets(): creating image size " + str(canvasWidth) + "x" + str(canvasHeight))
    #
    # create a canvas of the appropriate size for the image
    #
    w = Canvas(f, width=canvasWidth, height=canvasHeight)
    if root.radarPanelSize.get() == "Off":
    logging.debug("createRadarWidgets(): no net, no radar")
    netRadarImage = Label(w, text="No current radar")
    else:
    #
    # convert it into a photo item we can use in the display
    #
    # photo = photo.resize((canvasWidth,canvasHeight),Image.ANTIALIAS)
    netRadarImage = Label(w, image=photo)
    netRadarImage.image = photo
    w.grid(row=1, column=0, columnspan=3)
    netRadarImage.grid( row=1, column=0)
     
    Chris Hare, Aug 1, 2010
    #3
  4. Chris Hare

    Peter Otten Guest

    Chris Hare wrote:


    > On Aug 1, 2010, at 10:24 AM, rantingrick wrote:
    >
    >> On Aug 1, 7:35 am, Chris Hare <> 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
     
    Peter Otten, Aug 1, 2010
    #4
  5. Chris Hare

    Chris Hare Guest

    On Aug 1, 2010, at 1:08 PM, Peter Otten wrote:

    > Chris Hare wrote:
    >
    >
    >> On Aug 1, 2010, at 10:24 AM, rantingrick wrote:
    >>
    >>> On Aug 1, 7:35 am, Chris Hare <> 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
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Thanks for the help. My one week of python is getting a workout.

    I have shortened it all down and made it a standalone example, using yours as a model. Your example, works, but it will take a lot of effort to retrofit it into the code I have. (which is maybe not a bad idea,).

    Anyway

    from Tkinter import *
    import ImageTk
    import Image
    import sys

    def sizeit(filename):
    image = Image.open(filename)
    w,h = image.size
    print w, h
    photo = ImageTk.PhotoImage(file=filename)
    canvasWidth = 450
    canvasHeight = 412
    image = image.resize((canvasWidth,canvasHeight),Image.ANTIALIAS)
    w,h = image.size
    print w, h
    netRadarImage = Label(frame, image=image)
    netRadarImage.image = photo
    w.grid(row=1, column=0, columnspan=3)
    netRadarImage.grid( row=1, column=0)

    [filename] = sys.argv[1:]

    root = Tk()
    frame = Frame(root)
    frame.grid()
    sizeit(filename)

    root.mainloop()

    Just like yours it takes a filename. Unlike yours, mine gets an error that I can't figure out and is likely the root of the problem.

    When I run this code I get

    600 550 <== ORIGINAL image size
    450 412 <== resized image size
    Traceback (most recent call last):
    File "zztest.py", line 26, in <module>
    sizeit(filename)
    File "zztest.py", line 16, in sizeit
    netRadarImage = Label(frame, image=image)
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 2466, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 1932, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
    _tkinter.TclError: image "<Image.Image image mode=P size=450x412 at 0x1016095A8>" doesn't exist

    So, my problem appeared to be the resize, but in fact is just getting it onto the label.

    What am I doing wrong?
     
    Chris Hare, Aug 1, 2010
    #5
  6. Chris Hare

    Peter Otten Guest

    Chris Hare wrote:

    > Thanks for the help. My one week of python is getting a workout.
    >
    > I have shortened it all down and made it a standalone example, using yours
    > as a model. Your example, works, but it will take a lot of effort to
    > retrofit it into the code I have. (which is maybe not a bad idea,).


    You mean retrofit something that works into something tha doesn't?
    Seriously, you should never be afraid to throw away code, especially while
    you're still in the early phase of learning the language.

    > def sizeit(filename):
    > image = Image.open(filename)


    Now you have an Image

    > w,h = image.size
    > print w, h
    > photo = ImageTk.PhotoImage(file=filename)


    and now a photo, both created from the same file but otherwise independent

    > canvasWidth = 450
    > canvasHeight = 412
    > image = image.resize((canvasWidth,canvasHeight),Image.ANTIALIAS)


    Now you have a new resized Image

    > w,h = image.size
    > print w, h
    > netRadarImage = Label(frame, image=image)


    Label(image=...) expects a PhotoImage

    > netRadarImage.image = photo
    > w.grid(row=1, column=0, columnspan=3)


    Hmm...

    > netRadarImage.grid( row=1, column=0)


    Here's the fixed code:

    def sizeit(filename):
    image = Image.open(filename)
    canvasWidth = 450
    canvasHeight = 412
    image = image.resize((canvasWidth, canvasHeight),Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image=image)
    netRadarImage = Label(frame, image=photo)
    netRadarImage.image = photo
    netRadarImage.grid(row=0, column=0)

    In plain English:

    - open the Image using the PIL
    - resize it
    - wrap it into a PhotoImage
    - wrap the PhotoImage into a Tkinter.Label either by passing it to the
    initialiser or by assigning to label["image"]
    - make sure the PhotoImage isn't garbage-collected e. g. by assigning to
    label.image

    Peter
     
    Peter Otten, Aug 1, 2010
    #6
  7. Chris Hare

    Chris Hare Guest

    Thanks Peter - I know what I said sounded stupid :)

    I have been working with Python for a week and as you know sometimes it is easier to learn by seeing what you did wrong as compared to what should have been done with the same example. I loved your code by the way -

    Thanks for help just another beginner ....


    On Aug 1, 2010, at 4:32 PM, Peter Otten wrote:

    > Chris Hare wrote:
    >
    >> Thanks for the help. My one week of python is getting a workout.
    >>
    >> I have shortened it all down and made it a standalone example, using yours
    >> as a model. Your example, works, but it will take a lot of effort to
    >> retrofit it into the code I have. (which is maybe not a bad idea,).

    >
    > You mean retrofit something that works into something tha doesn't?
    > Seriously, you should never be afraid to throw away code, especially while
    > you're still in the early phase of learning the language.
    >
    >> def sizeit(filename):
    >> image = Image.open(filename)

    >
    > Now you have an Image
    >
    >> w,h = image.size
    >> print w, h
    >> photo = ImageTk.PhotoImage(file=filename)

    >
    > and now a photo, both created from the same file but otherwise independent
    >
    >> canvasWidth = 450
    >> canvasHeight = 412
    >> image = image.resize((canvasWidth,canvasHeight),Image.ANTIALIAS)

    >
    > Now you have a new resized Image
    >
    >> w,h = image.size
    >> print w, h
    >> netRadarImage = Label(frame, image=image)

    >
    > Label(image=...) expects a PhotoImage
    >
    >> netRadarImage.image = photo
    >> w.grid(row=1, column=0, columnspan=3)

    >
    > Hmm...
    >
    >> netRadarImage.grid( row=1, column=0)

    >
    > Here's the fixed code:
    >
    > def sizeit(filename):
    > image = Image.open(filename)
    > canvasWidth = 450
    > canvasHeight = 412
    > image = image.resize((canvasWidth, canvasHeight),Image.ANTIALIAS)
    > photo = ImageTk.PhotoImage(image=image)
    > netRadarImage = Label(frame, image=photo)
    > netRadarImage.image = photo
    > netRadarImage.grid(row=0, column=0)
    >
    > In plain English:
    >
    > - open the Image using the PIL
    > - resize it
    > - wrap it into a PhotoImage
    > - wrap the PhotoImage into a Tkinter.Label either by passing it to the
    > initialiser or by assigning to label["image"]
    > - make sure the PhotoImage isn't garbage-collected e. g. by assigning to
    > label.image
    >
    > Peter
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Chris Hare, Aug 1, 2010
    #7
  8. Chris Hare

    Chris Hare Guest

    And I see now what I did wrong - thanks for putting up with the questions.

    On Aug 1, 2010, at 4:32 PM, Peter Otten wrote:

    > Chris Hare wrote:
    >
    >> Thanks for the help. My one week of python is getting a workout.
    >>
    >> I have shortened it all down and made it a standalone example, using yours
    >> as a model. Your example, works, but it will take a lot of effort to
    >> retrofit it into the code I have. (which is maybe not a bad idea,).

    >
    > You mean retrofit something that works into something tha doesn't?
    > Seriously, you should never be afraid to throw away code, especially while
    > you're still in the early phase of learning the language.
    >
    >> def sizeit(filename):
    >> image = Image.open(filename)

    >
    > Now you have an Image
    >
    >> w,h = image.size
    >> print w, h
    >> photo = ImageTk.PhotoImage(file=filename)

    >
    > and now a photo, both created from the same file but otherwise independent
    >
    >> canvasWidth = 450
    >> canvasHeight = 412
    >> image = image.resize((canvasWidth,canvasHeight),Image.ANTIALIAS)

    >
    > Now you have a new resized Image
    >
    >> w,h = image.size
    >> print w, h
    >> netRadarImage = Label(frame, image=image)

    >
    > Label(image=...) expects a PhotoImage
    >
    >> netRadarImage.image = photo
    >> w.grid(row=1, column=0, columnspan=3)

    >
    > Hmm...
    >
    >> netRadarImage.grid( row=1, column=0)

    >
    > Here's the fixed code:
    >
    > def sizeit(filename):
    > image = Image.open(filename)
    > canvasWidth = 450
    > canvasHeight = 412
    > image = image.resize((canvasWidth, canvasHeight),Image.ANTIALIAS)
    > photo = ImageTk.PhotoImage(image=image)
    > netRadarImage = Label(frame, image=photo)
    > netRadarImage.image = photo
    > netRadarImage.grid(row=0, column=0)
    >
    > In plain English:
    >
    > - open the Image using the PIL
    > - resize it
    > - wrap it into a PhotoImage
    > - wrap the PhotoImage into a Tkinter.Label either by passing it to the
    > initialiser or by assigning to label["image"]
    > - make sure the PhotoImage isn't garbage-collected e. g. by assigning to
    > label.image
    >
    > Peter
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Chris Hare, Aug 1, 2010
    #8
  9. Chris Hare

    Aahz Guest

    In article <>,
    Chris Hare <> wrote:
    >
    >And I see now what I did wrong - thanks for putting up with the questions.


    Posting that information is useful for any other newbies who might be
    following along....
    --
    Aahz () <*> http://www.pythoncraft.com/

    "...if I were on life-support, I'd rather have it run by a Gameboy than a
    Windows box." --Cliff Wells
     
    Aahz, Aug 12, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eldon Lei [MSFT]
    Replies:
    0
    Views:
    647
    Eldon Lei [MSFT]
    Jul 10, 2003
  2. =?Utf-8?B?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=

    How to resize an image from SQL IMAGE field

    =?Utf-8?B?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=, Sep 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,526
    Hermit Dave
    Sep 10, 2004
  3. Chubby Arse
    Replies:
    0
    Views:
    575
    Chubby Arse
    Oct 6, 2005
  4. Chubby Arse

    Custom control renders table but doesn't resize at design time.

    Chubby Arse, Oct 6, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    188
    Chubby Arse
    Oct 6, 2005
  5. Replies:
    2
    Views:
    1,297
    Adrienne
    Feb 13, 2005
Loading...

Share This Page