[Pyobjc-dev] Garbage collection in Python/Pyobjc
Brought to you by:
ronaldoussoren
From: Peter B. <pe...@la...> - 2012-01-02 20:45:28
|
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 '' |