Drawing and Displaying an Image with PIL

Discussion in 'Python' started by W. eWatson, Jan 28, 2009.

  1. W. eWatson

    W. eWatson Guest

    Here's my program:

    # fun and games
    import Image, ImageDraw

    im = Image.open("wagon.tif") # it exists in the same Win XP
    # folder as the program
    draw = ImageDraw.Draw(im)
    draw.line((0, 0) + im.size, fill=128)
    draw.line((0,0),(20,140), fill=128)

    # How show this final image on a display?

    root.mainloop()

    It has two problems. One is it crashes with:
    draw.line((0,0),(20,140), fill=128)
    TypeError: line() got multiple values for keyword argument 'fill'

    Secondly, it has no way to display the image drawn on. Is it possible, or do
    I have to pass the image off to another module's methods?


    --
    W. eWatson

    (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

    Web Page: <www.speckledwithstars.net/>
     
    W. eWatson, Jan 28, 2009
    #1
    1. Advertising

  2. W. eWatson

    r Guest

    On Jan 27, 9:15 pm, "W. eWatson" <> wrote:
    > Here's my program:
    >
    > # fun and games
    > import Image, ImageDraw
    >
    > im = Image.open("wagon.tif") # it exists in the same Win XP
    > # folder as the program
    > draw = ImageDraw.Draw(im)
    > draw.line((0, 0) + im.size, fill=128)
    > draw.line((0,0),(20,140), fill=128)
    >
    > # How show this final image on a display?
    >
    > root.mainloop()
    >
    > It has two problems. One is it crashes with:
    >      draw.line((0,0),(20,140), fill=128)
    > TypeError: line() got multiple values for keyword argument 'fill'
    >
    > Secondly, it has no way to display the image drawn on. Is it possible, or do
    > I have to pass the image off to another module's methods?
    >
    > --
    >                                 W. eWatson
    >
    >               (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    >                Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
    >
    >                      Web Page: <www.speckledwithstars.net/>


    I have not tried your code but i think you need to put your coodinates
    in one tuple. Here is an example from the docs

    Example
    Example: Draw a Grey Cross Over an Image
    import Image, ImageDraw
    im = Image.open("lena.pgm")
    draw = ImageDraw.Draw(im)
    draw.line((0, 0) + im.size, fill=128)
    draw.line((0, im.size[1], im.size[0], 0), fill=128)
    del draw
    # write to stdout
    im.save(sys.stdout, "PNG")

    Hope that helps
     
    r, Jan 28, 2009
    #2
    1. Advertising

  3. W. eWatson

    W. eWatson Guest

    r wrote:
    > On Jan 27, 9:15 pm, "W. eWatson" <> wrote:
    >> Here's my program:
    >>
    >> # fun and games
    >> import Image, ImageDraw
    >>
    >> im = Image.open("wagon.tif") # it exists in the same Win XP
    >> # folder as the program
    >> draw = ImageDraw.Draw(im)
    >> draw.line((0, 0) + im.size, fill=128)
    >> draw.line((0,0),(20,140), fill=128)
    >>
    >> # How show this final image on a display?
    >>
    >> root.mainloop()
    >>
    >> It has two problems. One is it crashes with:
    >> draw.line((0,0),(20,140), fill=128)
    >> TypeError: line() got multiple values for keyword argument 'fill'
    >>
    >> Secondly, it has no way to display the image drawn on. Is it possible, or do
    >> I have to pass the image off to another module's methods?
    >>
    >> --
    >> W. eWatson
    >>
    >> (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    >> Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
    >>
    >> Web Page: <www.speckledwithstars.net/>

    >
    > I have not tried your code but i think you need to put your coodinates
    > in one tuple. Here is an example from the docs
    >
    > Example
    > Example: Draw a Grey Cross Over an Image
    > import Image, ImageDraw
    > im = Image.open("lena.pgm")
    > draw = ImageDraw.Draw(im)
    > draw.line((0, 0) + im.size, fill=128)
    > draw.line((0, im.size[1], im.size[0], 0), fill=128)
    > del draw
    > # write to stdout
    > im.save(sys.stdout, "PNG")
    >
    > Hope that helps

    That's pretty much the code I used. In fact, I borrowed it from the pdf. I
    just tried it, and it output "%PNG".

    I'd like to see this displayed in a window. If the fine had written
    properly, I could see whether it really drew the lines. It did not fail on
    the same draw stmts in my program.

    I see my problem, , instead of + between the tuples. I thought I'd seen
    another example where the 2-d tuples could be separated.

    I see a ImageFile module, but it's not for writing image files simply.

    --
    W. eWatson

    (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

    Web Page: <www.speckledwithstars.net/>
     
    W. eWatson, Jan 28, 2009
    #3
  4. W. eWatson

    r Guest

    Change this line:
    draw.line((0,0),(20,140), fill=128)

    To This:
    draw.line((0,0, 20,140), fill=128)

    And you should be good to go. Like you said, if you need to combine 2
    tuples you can do:
    (1,2)+(3,4)
     
    r, Jan 28, 2009
    #4
  5. W. eWatson

    W. eWatson Guest

    r wrote:
    > Change this line:
    > draw.line((0,0),(20,140), fill=128)
    >
    > To This:
    > draw.line((0,0, 20,140), fill=128)
    >
    > And you should be good to go. Like you said, if you need to combine 2
    > tuples you can do:
    > (1,2)+(3,4)

    Yes, that's true, but the big question is how to "see" the final image?
    Either one employees another module or writes the file into a folder, then
    displays it with a paint program?

    --
    W. eWatson

    (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

    Web Page: <www.speckledwithstars.net/>
     
    W. eWatson, Jan 28, 2009
    #5
  6. W. eWatson

    Bill McClain Guest

    On 2009-01-28, W. eWatson <> wrote:
    > Yes, that's true, but the big question is how to "see" the final image?
    > Either one employees another module or writes the file into a folder, then
    > displays it with a paint program?


    Does im.show() not work?

    -Bill
    --
    Sattre Press Tales of War
    http://sattre-press.com/ by Lord Dunsany
    http://sattre-press.com/tow.html
     
    Bill McClain, Jan 28, 2009
    #6
  7. W. eWatson

    Peter Otten Guest

    W. eWatson wrote:

    > r wrote:
    >> Change this line:
    >> draw.line((0,0),(20,140), fill=128)
    >>
    >> To This:
    >> draw.line((0,0, 20,140), fill=128)
    >>
    >> And you should be good to go. Like you said, if you need to combine 2
    >> tuples you can do:
    >> (1,2)+(3,4)

    > Yes, that's true, but the big question is how to "see" the final image?
    > Either one employees another module or writes the file into a folder, then
    > displays it with a paint program?


    For debugging purposes you can just invoke the show() method

    im = Image.open(...)
    # modify image
    im.show()

    If you want to integrate the image into your own Tkinter program -- that is
    explained here:

    http://effbot.org/tkinterbook/photoimage.htm

    Following these instruction you code might become

    import Tkinter as tk
    import Image
    import ImageTk
    import ImageDraw
    import sys

    filename = sys.argv[1]
    im = Image.open(filename)

    draw = ImageDraw.Draw(im)
    draw.line((0, 0) + im.size, fill=128)
    draw.line(((0,0),(20,140)), fill=128)


    root = tk.Tk()
    pi = ImageTk.PhotoImage(im)
    label = tk.Label(root, image=pi)
    label.pack()
    root.mainloop()

    Peter
     
    Peter Otten, Jan 28, 2009
    #7
  8. W. eWatson

    W. eWatson Guest

    Peter Otten wrote:
    > W. eWatson wrote:
    >
    >> r wrote:
    >>> Change this line:
    >>> draw.line((0,0),(20,140), fill=128)
    >>>
    >>> To This:
    >>> draw.line((0,0, 20,140), fill=128)
    >>>
    >>> And you should be good to go. Like you said, if you need to combine 2
    >>> tuples you can do:
    >>> (1,2)+(3,4)

    >> Yes, that's true, but the big question is how to "see" the final image?
    >> Either one employees another module or writes the file into a folder, then
    >> displays it with a paint program?

    >
    > For debugging purposes you can just invoke the show() method
    >
    > im = Image.open(...)
    > # modify image
    > im.show()
    >
    > If you want to integrate the image into your own Tkinter program -- that is
    > explained here:
    >
    > http://effbot.org/tkinterbook/photoimage.htm
    >
    > Following these instruction you code might become
    >
    > import Tkinter as tk
    > import Image
    > import ImageTk
    > import ImageDraw
    > import sys
    >
    > filename = sys.argv[1]
    > im = Image.open(filename)
    >
    > draw = ImageDraw.Draw(im)
    > draw.line((0, 0) + im.size, fill=128)
    > draw.line(((0,0),(20,140)), fill=128)
    >
    >
    > root = tk.Tk()
    > pi = ImageTk.PhotoImage(im)
    > label = tk.Label(root, image=pi)
    > label.pack()
    > root.mainloop()
    >
    > Peter

    My initial quest was to do it in PIL. That seems impossible, and the way out
    is Tkinter. I'm not yet savvy enough with Pythons graphics. I was definitely
    leaning towards PhotoImage as the way out. What module is show in?

    Repairing my (0,0), ... to (0,0)+, and. replacing arg with ImageOPen,
    produces a correct solution.

    My NM Tech pdf misses the boat on PhotoImage. I've seen your reference
    before, but never looked at PhotoImage. I'll bookmark it. I sure wish it was
    in pdf format.

    Thanks.


    --
    W. eWatson

    (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
    Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

    Web Page: <www.speckledwithstars.net/>
     
    W. eWatson, Jan 29, 2009
    #8
    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. Replies:
    1
    Views:
    686
    Kevin Spencer
    Jan 9, 2006
  2. byrd48
    Replies:
    0
    Views:
    3,602
    byrd48
    Jul 6, 2006
  3. Leonard J. Reder

    PIL and line drawing

    Leonard J. Reder, May 20, 2005, in forum: Python
    Replies:
    4
    Views:
    3,774
  4. defn noob
    Replies:
    1
    Views:
    434
    Mark Space
    Jun 28, 2008
  5. Sverre
    Replies:
    2
    Views:
    3,691
    Sverre
    Dec 17, 2009
Loading...

Share This Page