root / pkpgcounter / trunk / pkpgpdls / pil.py @ 544

Revision 527, 2.1 kB (checked in by jerome, 17 years ago)

Now the presence of executable dependencies is tested at runtime.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22
23"""This modules implements a page counter for image formats supported by the Python Imaging Library."""
24
25import pdlparser
26
27try :
28    from PIL import Image
29except ImportError :   
30    sys.stderr.write("ERROR: You MUST install the Python Imaging Library (python-imaging) for pkpgcounter to work.\n")
31    raise pdlparser.PDLParserError, "The Python Imaging Library is missing."
32
33import version
34
35class Parser(pdlparser.PDLParser) :
36    """A parser for plain text documents."""
37    totiffcommands = [ 'convert "%(infname)s" "%(outfname)s"' ] 
38    required = [ "convert" ]
39    def isValid(self) :   
40        """Returns True if data is an image format supported by PIL, else False."""   
41        try :
42            image = Image.open(self.filename)
43        except (IOError, OverflowError) :   
44            return False
45        else :   
46            self.logdebug("DEBUG: Input file seems to be an image in the %s (%s) format." % (image.format, image.format_description))
47            return True
48           
49    def getJobSize(self) :
50        """Counts pages in an image file."""
51        index = 0
52        image = Image.open(self.filename)
53        try :
54            while True :
55                index += 1             
56                image.seek(index)
57        except EOFError :       
58            pass
59        return index   
Note: See TracBrowser for help on using the browser.