root / pkpgcounter / trunk / pkpgpdls / inkcoverage.py @ 281

Revision 281, 3.8 kB (checked in by jerome, 18 years ago)

Now handles multipage TIFF images correctly

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Revision Id
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 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 2 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, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23
24import sys
25
26from PIL import Image
27
28def getPercentCMYK(img, nbpix) :
29    """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture.
30     
31       PIL doesn't produce useable CMYK for our algorithm, so we use the algorithm from PrintBill.
32       Psyco speeds this function up by around 2.5 times on my computer.
33    """
34    if img.mode != "RGB" :
35        img = img.convert("RGB")
36    cyan = magenta = yellow = black = 0   
37    for (r, g, b) in img.getdata() :
38        if r == g == b :
39            black += 255 - r
40        else :   
41            cyan += 255 - r
42            magenta += 255 - g
43            yellow += 255 - b
44    return { "C" : 100.0 * (cyan / 255.0) / nbpix,
45             "M" : 100.0 * (magenta / 255.0) / nbpix,
46             "Y" : 100.0 * (yellow / 255.0) / nbpix,
47             "K" : 100.0 * (black / 255.0) / nbpix,
48           }
49       
50def getPercent(img, nbpix) :
51    """Extracts the percents per color component from a picture.
52     
53       Faster without Pysco.
54    """
55    result = {}     
56    bands = img.split()
57    for (i, bandname) in enumerate(img.getbands()) :
58        result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix
59    return result   
60       
61def getPercentBlack(img, nbpix) :
62    """Extracts the percents of Black from a picture, once converted to gray levels."""
63    if img.mode != "L" :
64        img = img.convert("L")
65    return { "L" : 100.0 - getPercent(img, nbpix)["L"] }
66   
67def getPercentRGB(img, nbpix) :
68    """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB."""
69    if img.mode != "RGB" :
70        img = img.convert("RGB")
71    return getPercent(img, nbpix)   
72   
73def getPercentCMY(img, nbpix) :
74    """Extracts the percents of Cyan, Magenta, and Yellow from a picture once converted to RGB."""
75    result = getPercentRGB(img, nbpix)
76    return { "C" : 100.0 - result["R"],
77             "M" : 100.0 - result["G"],
78             "Y" : 100.0 - result["B"],
79           }
80   
81def getPercents(fname) :
82    """Extracts the ink percentages from an image."""
83    try :
84        import psyco
85    except ImportError :   
86        pass
87    else :   
88        psyco.bind(getPercentCMYK)
89    result = []
90    index = 0
91    image = Image.open(fname)
92    try :
93        while 1 :
94            nbpixels = image.size[0] * image.size[1]
95            result.append({ "BLACK" : getPercentBlack(image, nbpixels), \
96                            "RGB" : getPercentRGB(image, nbpixels), \
97                            "CMY" : getPercentCMY(image, nbpixels), \
98                            "CMYK" : getPercentCMYK(image, nbpixels), \
99                          })
100            index += 1             
101            image.seek(index)
102    except EOFError :       
103        pass
104    return result
105
106if __name__ == "__main__" :
107    # NB : length of result gives number of pages !
108    print getPercents(sys.argv[1])
Note: See TracBrowser for help on using the browser.