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

Revision 3474, 2.0 kB (checked in by jerome, 15 years ago)

Changed copyright years.

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