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

Revision 357, 3.9 kB (checked in by jerome, 18 years ago)

Added missing docstrings, thanks to pylint.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Revision Id
RevLine 
[277]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[303]6# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
[277]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
[357]24"""This modules implements the computation of ink coverage in different colorspaces."""
25
[277]26import sys
27
28from PIL import Image
29
[280]30def getPercentCMYK(img, nbpix) :
31    """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture.
32     
33       PIL doesn't produce useable CMYK for our algorithm, so we use the algorithm from PrintBill.
34       Psyco speeds this function up by around 2.5 times on my computer.
35    """
36    if img.mode != "RGB" :
37        img = img.convert("RGB")
[287]38    data = img.getdata()   
[280]39    cyan = magenta = yellow = black = 0   
[287]40    for (r, g, b) in data :
[280]41        if r == g == b :
42            black += 255 - r
43        else :   
44            cyan += 255 - r
45            magenta += 255 - g
46            yellow += 255 - b
47    return { "C" : 100.0 * (cyan / 255.0) / nbpix,
48             "M" : 100.0 * (magenta / 255.0) / nbpix,
49             "Y" : 100.0 * (yellow / 255.0) / nbpix,
50             "K" : 100.0 * (black / 255.0) / nbpix,
51           }
52       
53def getPercent(img, nbpix) :
54    """Extracts the percents per color component from a picture.
55     
[342]56       Faster without Psyco.
[280]57    """
58    result = {}     
59    bands = img.split()
60    for (i, bandname) in enumerate(img.getbands()) :
61        result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix
62    return result   
63       
[279]64def getPercentBlack(img, nbpix) :
[280]65    """Extracts the percents of Black from a picture, once converted to gray levels."""
[279]66    if img.mode != "L" :
67        img = img.convert("L")
[280]68    return { "L" : 100.0 - getPercent(img, nbpix)["L"] }
[279]69   
[280]70def getPercentRGB(img, nbpix) :
71    """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB."""
72    if img.mode != "RGB" :
73        img = img.convert("RGB")
74    return getPercent(img, nbpix)   
75   
76def getPercentCMY(img, nbpix) :
77    """Extracts the percents of Cyan, Magenta, and Yellow from a picture once converted to RGB."""
78    result = getPercentRGB(img, nbpix)
79    return { "C" : 100.0 - result["R"],
80             "M" : 100.0 - result["G"],
81             "Y" : 100.0 - result["B"],
82           }
83   
[279]84def getPercents(fname) :
85    """Extracts the ink percentages from an image."""
[281]86    try :
87        import psyco
88    except ImportError :   
89        pass
90    else :   
91        psyco.bind(getPercentCMYK)
92    result = []
93    index = 0
[279]94    image = Image.open(fname)
[281]95    try :
96        while 1 :
97            nbpixels = image.size[0] * image.size[1]
[282]98            result.append((image.size, \
99                           { "BLACK" : getPercentBlack(image, nbpixels), \
100                             "RGB" : getPercentRGB(image, nbpixels), \
101                             "CMY" : getPercentCMY(image, nbpixels), \
102                             "CMYK" : getPercentCMYK(image, nbpixels), \
103                           }))
[281]104            index += 1             
105            image.seek(index)
106    except EOFError :       
107        pass
108    return result
[277]109
110if __name__ == "__main__" :
[281]111    # NB : length of result gives number of pages !
112    print getPercents(sys.argv[1])
Note: See TracBrowser for help on using the browser.