Changeset 500

Show
Ignore:
Timestamp:
11/21/07 22:47:27 (16 years ago)
Author:
jerome
Message:

Added parser for image files.

Files:
1 copied

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/image.py

    r495 r500  
    2121# 
    2222 
    23 """This modules implements a page counter for plain text documents.""" 
     23"""This modules implements a page counter for image formats supported by the Python Imaging Library.""" 
     24 
     25from PIL import Image 
    2426 
    2527import pdlparser 
     
    2830class Parser(pdlparser.PDLParser) : 
    2931    """A parser for plain text documents.""" 
    30     totiffcommands = [ 'enscript --quiet --portrait --no-header --columns 1 --output - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -', 
    31                        'a2ps --borders 0 --quiet --portrait --no-header --columns 1 --output - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -', 
     32    totiffcommands = [ 'convert "%(infname)s" "%(outfname)s"', 
    3233                     ]   
    33     openmode = "rU"                  
    3434    def isValid(self) :     
    35         """Returns True if data is plain text, else False. 
    36          
    37            It's hard to detect a plain text file, so we just try to 
    38            extract lines from the first block (sufficiently large). 
    39            If it's impossible to find one we consider it's not plain text. 
    40         """    
    41         lines = self.firstblock.split("\r\n") 
    42         if len(lines) == 1 : 
    43             lines = lines[0].split("\r") 
    44             if len(lines) == 1 : 
    45                 lines = lines[0].split("\n") 
    46         if len(lines) > 1 : 
    47             self.logdebug("DEBUG: Input file seems to be in the plain text format.") 
     35        """Returns True if data is an image format supported by PIL, else False."""    
     36        try : 
     37            image = Image.open(self.filename) 
     38        except IOError :     
     39            return False 
     40        else :     
    4841            return True 
    49         else :     
    50             return False 
    5142             
    5243    def getJobSize(self) : 
    53         """Counts pages in a plain text document.""" 
    54         pagesize = 66   # TODO : Does this vary wrt the default page size ? 
    55                         # TODO : /etc/papersize and /etc/paper.config 
    56         pagecount = 0 
    57         linecount = 0 
    58         for line in self.infile : 
    59             if line.endswith("\n") : 
    60                 linecount += 1     
    61                 if (linecount > pagesize) : 
    62                     pagecount += 1 
    63                     linecount = 0 
    64                 else :     
    65                     cnt = line.count("\f") 
    66                     if cnt : 
    67                         pagecount += cnt 
    68                         linecount = 0 
    69             else :         
    70                 raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__ 
    71         return pagecount + 1    # NB : empty files are catched in isValid() 
     44        """Counts pages in an image file.""" 
     45        index = 0 
     46        image = Image.open(self.filename) 
     47        try : 
     48            while True : 
     49                index += 1               
     50                image.seek(index) 
     51        except EOFError :         
     52            pass 
     53        return index