Garbage collection in Python/Pyobjc
Peter Bernheim <[email protected]>
| Newsgroups | gmane.comp.python.pyobjc.devel |
|---|---|
| Message-ID | <[email protected]> |
I'm working on a script to convert each page of a large directory of pdfs to jpgs, using a version of this script:
http://files.macscripter.net/joy/files/pdflib.py
I am walking through a directory in Python, and calling the below function on each file. It seems that there is no garbage collection being performed on any of the objects being created below, and my searches through the docs don't seem to show an explicit way of performing garbage collection on these rather large objects. How to ensure that the below code, if called thousands of times within a Python loop isn't going to consume the entire system's memory?
def pdf2jpgs(pdfpath, pages_dir, resolution=72):
"""I am converting all pages of a PDF file to JPG images."""
pdfdata = NSData.dataWithContentsOfFile_(pdfpath)
pdfrep = NSPDFImageRep.imageRepWithData_(pdfdata)
pagecount = pdfrep.pageCount()
for i in range(0, pagecount):
pdfrep.setCurrentPage_(i)
pdfimage = NSImage.alloc().init()
pdfimage.addRepresentation_(pdfrep)
origsize = pdfimage.size()
width, height = origsize
pdfimage.setScalesWhenResized_(YES)
rf = resolution / 72.0
pdfimage.setSize_((width*rf, height*rf))
tiffimg = pdfimage.TIFFRepresentation()
bmpimg = NSBitmapImageRep.imageRepWithData_(tiffimg)
data = bmpimg.representationUsingType_properties_(NSJPEGFileType, {NSImageCompressionFactor: 1.0})
pagenum = i + 1
jpgpath = "%s/pg%d.jpg" % (pages_dir, pagenum)
if not os.path.exists(jpgpath):
data.writeToFile_atomically_(jpgpath, False)
return ''
------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox