943,907 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5915
  • Python RSS
Jul 7th, 2008
0

Memory Error

Expand Post »
So, I wrote a little script to convert tifs into jpegs. The tifs are about 600mb in size, I reduce the size, and then convert. Here is the code in its entirety:

python Syntax (Toggle Plain Text)
  1. ## First we import the image library, Image object, and garbage
  2. ## collector, as well as os
  3. from PIL import Image
  4. import gc, os
  5. ## Get the path for the directory to be converted, then change to that path
  6. print "Please enter the path to the files that you wish to convert."
  7. path = raw_input()
  8. os.chdir(path)
  9. ## For each file in every subdirectory, see if it's a tif file
  10. for root, dir, files in os.walk(path):
  11. for name in files:
  12. if name[-4:] == ".tif":
  13. print 'Opening ' + name
  14. os.chdir(path)
  15. im = Image.open(root + '/' + name)
  16. x, y = im.size
  17. ## Resize the tiff tile at a 2/3 scale. Make a new directory to
  18. ## mimic the file heiarchy of the original file, only on the C
  19. ## drive instead of wherever it was to begin with.
  20. print 'Resizing'
  21. im2 = im.resize((int(x*.66), int(y*.66)), Image.ANTIALIAS)
  22. n = 'c' + root[1:]
  23. savedfile = n +"/jpegs/"
  24. try:
  25. os.makedirs(savedfile)
  26. os.chdir(savedfile)
  27. except WindowsError:
  28. os.chdir(savedfile)
  29. savedfile = name[:-4] + ".jpg"
  30. ## Save the file as a jpg, with a high quality
  31. print 'Saving'
  32. im2.save(savedfile, quality=85)
  33. del im
  34. del im2
  35. ## Force a memory dump. Otherwise memory will get cluttered
  36. ## up, very quickly too.
  37. gc.collect()
  38. print 'Memory Wiped'

Due to space limitations in the workplace, I need it to copy the directory structure onto the c drive of the computer the script is running off of, hence the funky directory dance. I'm still kinda new to all of this, and I thought the garbage collector would help me out. But, it'll wipe the memory at first, but things will still accumulate. After converting about 30-40 images (and I need to convert literally tens of thousands) the program crashed with a MemoryError. Page File usage was at about 2G. There is about 3 gigs of ram on this PC, which is running XP. Any tips or advice?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Edwards8 is offline Offline
8 posts
since Apr 2008
Jul 8th, 2008
0

Re: Memory Error

Well, a quick tangential note: your method for extracting the extension

name[-4:]

is buggy. What if the file extension is ".jpeg"?

Better:

Python Syntax (Toggle Plain Text)
  1. filename, ext = os.path.splitext(name)
  2. if ext == ".tif":
  3. do stuff ...
  4. savedfile = filename+".jpg"
  5. do more stuff...
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jul 8th, 2008
0

Re: Memory Error

WRT to the main problem, the memory error, I agree that there's something wrong, and it doesn't appear to be your code.

You might try e-mailing the PIL support at

image-sig@python.org

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jul 8th, 2008
0

Re: Memory Error

If it's anything but a tif, it'll be ignored, as I don't need to do anything with it. However, I think that the method you showed me is much more practical than what I've got (I wasn't aware of that tool to grab the extension name). So, I'll be modifying it. Thanks for the tip. And I'll shoot them an email. Thanks for the help, really. Happy to know that I at least had the code right.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Edwards8 is offline Offline
8 posts
since Apr 2008
Jul 11th, 2008
0

Re: Memory Error

Hi Edwards8,

It turns out that PIL will let you read pieces of an image file in sequence without loading the entire thing into memory. Fred Lundh has a post about it on a python mailing list here. I don't know if this will work for TIFs, but you can always give it a shot.

It sounds like some piece of the file is being kept resident in memory, despite your deleting and garbage collection - and from the sizes you quoted, it looks to be about a tenth of each file. Maybe PIL is reading the files in pieces, and hanging onto the last piece of each? Or maybe the file is being kept open in some way, even though you delete the image object? One thing you could try is to create a file handle on the image file, then use Image.open() on that, instead of the file name. Then, when you are finished, close the original file handle. This may convince PIL to let go of whatever data it's holding onto.

Unfortunately, I don't know a whole lot about PIL, so I'm shooting in the dark, here.

Hope this helps!
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Jul 12th, 2008
0

Re: Memory Error

Try running everything after "if name[-4:] == ".tif":" in a separate function. The memory should be garbage collected when the function exits.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: A python script with input
Next Thread in Python Forum Timeline: Python programs on other computers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC