Do we need to delete "ImageDraw.Draw" after using it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Mark

    Do we need to delete "ImageDraw.Draw" after using it?

    Hello all:

    I am looking the sample code posted on PIL website
    The page you are looking for doesn't exist. Return to PythonWare for free online image conversion tools.


    ############### ############### ############### ###
    <<Draw a Grey Cross Over an Image>>

    import Image, ImageDraw

    im = Image.open("len a.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 # ===>>>Why we should delete this object draw?

    # write to stdout
    im.save(sys.std out, "PNG")
    ############### ############### ############### ####

    Is there any general rule that we must delete the object after using
    it?


    Thank you
    -Daniel

  • John Bokma

    #2
    Re: Do we need to delete &quot;ImageDraw .Draw&quot; after using it?

    "Daniel Mark" <danielmarkhot@ gmail.comwrote:
    Is there any general rule that we must delete the object after using
    it?
    In general: if the object is not destroyed when it goes out of scope (but
    later) and uses precious resources [1], or if a special clean up is
    required, you should delete it explicitly.


    [1] like file handles

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html

    Comment

    • Steve Holden

      #3
      Re: Do we need to delete &quot;ImageDraw .Draw&quot; after using it?

      Daniel Mark wrote:
      Hello all:
      >
      I am looking the sample code posted on PIL website
      The page you are looking for doesn't exist. Return to PythonWare for free online image conversion tools.

      >
      ############### ############### ############### ###
      <<Draw a Grey Cross Over an Image>>
      >
      import Image, ImageDraw
      >
      im = Image.open("len a.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 # ===>>>Why we should delete this object draw?
      >
      # write to stdout
      im.save(sys.std out, "PNG")
      ############### ############### ############### ####
      >
      Is there any general rule that we must delete the object after using
      it?
      >
      Just consider it good hygiene.

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://holdenweb.blogspot.com
      Recent Ramblings http://del.icio.us/steve.holden

      Comment

      • Fredrik Lundh

        #4
        Re: Do we need to delete &quot;ImageDraw .Draw&quot; after using it?

        Steve Holden wrote:
        >Is there any general rule that we must delete the object after using
        >it?
        >>
        Just consider it good hygiene.
        in this specific case, it may not be obvious for the casual reader that
        the global "draw" variable will contain an indirect reference to the
        original image object, so even if you throw away (or replace) the "im"
        variable with something else, the original image will still be present
        in memory.

        if you put the same code inside a function, you can safely leave the
        garbage handling to Python.

        </F>

        Comment

        Working...