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 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 | |
---|
24 | import sys |
---|
25 | |
---|
26 | from PIL import Image |
---|
27 | |
---|
28 | def 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 | data = img.getdata() |
---|
37 | cyan = magenta = yellow = black = 0 |
---|
38 | for (r, g, b) in data : |
---|
39 | if r == g == b : |
---|
40 | black += 255 - r |
---|
41 | else : |
---|
42 | cyan += 255 - r |
---|
43 | magenta += 255 - g |
---|
44 | yellow += 255 - b |
---|
45 | return { "C" : 100.0 * (cyan / 255.0) / nbpix, |
---|
46 | "M" : 100.0 * (magenta / 255.0) / nbpix, |
---|
47 | "Y" : 100.0 * (yellow / 255.0) / nbpix, |
---|
48 | "K" : 100.0 * (black / 255.0) / nbpix, |
---|
49 | } |
---|
50 | |
---|
51 | def getPercent(img, nbpix) : |
---|
52 | """Extracts the percents per color component from a picture. |
---|
53 | |
---|
54 | Faster without Psyco. |
---|
55 | """ |
---|
56 | result = {} |
---|
57 | bands = img.split() |
---|
58 | for (i, bandname) in enumerate(img.getbands()) : |
---|
59 | result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix |
---|
60 | return result |
---|
61 | |
---|
62 | def getPercentBlack(img, nbpix) : |
---|
63 | """Extracts the percents of Black from a picture, once converted to gray levels.""" |
---|
64 | if img.mode != "L" : |
---|
65 | img = img.convert("L") |
---|
66 | return { "L" : 100.0 - getPercent(img, nbpix)["L"] } |
---|
67 | |
---|
68 | def getPercentRGB(img, nbpix) : |
---|
69 | """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB.""" |
---|
70 | if img.mode != "RGB" : |
---|
71 | img = img.convert("RGB") |
---|
72 | return getPercent(img, nbpix) |
---|
73 | |
---|
74 | def getPercentCMY(img, nbpix) : |
---|
75 | """Extracts the percents of Cyan, Magenta, and Yellow from a picture once converted to RGB.""" |
---|
76 | result = getPercentRGB(img, nbpix) |
---|
77 | return { "C" : 100.0 - result["R"], |
---|
78 | "M" : 100.0 - result["G"], |
---|
79 | "Y" : 100.0 - result["B"], |
---|
80 | } |
---|
81 | |
---|
82 | def getPercents(fname) : |
---|
83 | """Extracts the ink percentages from an image.""" |
---|
84 | try : |
---|
85 | import psyco |
---|
86 | except ImportError : |
---|
87 | pass |
---|
88 | else : |
---|
89 | psyco.bind(getPercentCMYK) |
---|
90 | result = [] |
---|
91 | index = 0 |
---|
92 | image = Image.open(fname) |
---|
93 | try : |
---|
94 | while 1 : |
---|
95 | nbpixels = image.size[0] * image.size[1] |
---|
96 | result.append((image.size, \ |
---|
97 | { "BLACK" : getPercentBlack(image, nbpixels), \ |
---|
98 | "RGB" : getPercentRGB(image, nbpixels), \ |
---|
99 | "CMY" : getPercentCMY(image, nbpixels), \ |
---|
100 | "CMYK" : getPercentCMYK(image, nbpixels), \ |
---|
101 | })) |
---|
102 | index += 1 |
---|
103 | image.seek(index) |
---|
104 | except EOFError : |
---|
105 | pass |
---|
106 | return result |
---|
107 | |
---|
108 | if __name__ == "__main__" : |
---|
109 | # NB : length of result gives number of pages ! |
---|
110 | print getPercents(sys.argv[1]) |
---|