471,337 Members | 986 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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

Hello all:

I am looking the sample code posted on PIL website
http://www.pythonware.com/library/pi.../imagedraw.htm

################################################
<<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 # ===>>>Why we should delete this object draw?

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

Is there any general rule that we must delete the object after using
it?
Thank you
-Daniel

Sep 20 '06 #1
3 5947
"Daniel Mark" <da***********@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
Sep 20 '06 #2
Daniel Mark wrote:
Hello all:

I am looking the sample code posted on PIL website
http://www.pythonware.com/library/pi.../imagedraw.htm

################################################
<<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 # ===>>>Why we should delete this object draw?

# write to stdout
im.save(sys.stdout, "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

Sep 20 '06 #3
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>

Sep 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by Tobias Froehlich | last post: by
8 posts views Thread by gw7rib | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.