[Image-SIG] less disk IO

Carl Karsten carl at personnelware.com
Thu Jan 3 00:32:05 CET 2008


I am trying to convert a pdf (data in string, not disk file) to a png.  This 
will be run in a django view on a server (PyCon's even) so it would be good if 
it didn't hit the disk as much.  Shouldn't I be able to use .tostring if I can 
use .save?

I gave up trying to convert the pdf.  The things that might have actually worked 
were not stable enough to be put onto the production server.   If you think you 
know a way, please code it first.  If it takes you longer than 10 min, or more 
than 100 words to describe the setup, it probably isn't going to get used :)

Also, I have been told about 10 times now that it should be easy.  I agree.  so 
far no one has proved it.

Thanks,
Carl K

def sample(ds):

     """ make a sample badge"""

     # this is some clunky code.
     # it bounces the data off the disk more times than I care to count
     # It would be nice if it was all done in memory,
     # but I couldn't figure out how.

     import PIL, Image
     import tempfile

     ds['attendeeID']='SAMPLE'
     pdf = mkpdf(ds)

     tempDir = tempfile.mkdtemp( prefix = "badgePreview_" )
     pdfname = path.join( tempDir, 'badge.pdf' )
     imgname = path.join( tempDir, 'badge.png' )

     open(pdfname,'wb').write(pdf)

     # convert pdf to some image format (uses ext to determine type)
     system('convert %s %s' % (pdfname, imgname))

     # crop the badge (pdf may be 8x11 page)
     Image.open(imgname).crop([0,0,300,235]).save(imgname)

     # I am sure there is a way to get the image out of im
     # so that I don't have to send it back to the disk.
     # im=Image.open(imgname)
     # im=im.crop([0,0,300,235])
     # img=im.tostring('raw') # The image x cannot be displayed, because it 
contains errors.
     # img=im.tostring('PNG') # encoder PNG not available
     # img=im.tostring('png') # encoder png not available

     img = open(imgname).read()
     remove(pdfname)
     remove(imgname)
     rmdir(tempDir)

     return img


More information about the Image-SIG mailing list