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 | |
---|
24 | import sys |
---|
25 | |
---|
26 | import psyco |
---|
27 | |
---|
28 | from PIL import Image |
---|
29 | |
---|
30 | """ RGB to CMYK formula : |
---|
31 | |
---|
32 | Black = min(1 - r, 1 - g, 1 - b) |
---|
33 | Cyan = (1 - r - Black) / (1 - Black) |
---|
34 | Magenta = (1 - g - Black) / (1 - Black) |
---|
35 | Yellow = (1 - b - Black) / (1 - Black) |
---|
36 | """ |
---|
37 | |
---|
38 | def percent_cmy(fname) : |
---|
39 | result = [] |
---|
40 | img = Image.open(fname) |
---|
41 | (r, g, b) = [ p.histogram() for p in img.split() ] |
---|
42 | nbpix = sum(r) |
---|
43 | for histo in (r, g, b) : |
---|
44 | value = 0 |
---|
45 | for i in range(len(histo)) : |
---|
46 | value += histo[i] * (255 - i) |
---|
47 | result.append((100 * (value / 255.0)) / nbpix) |
---|
48 | return tuple(result) |
---|
49 | |
---|
50 | if __name__ == "__main__" : |
---|
51 | psyco.bind(percent_cmy) |
---|
52 | print percent_cmy(sys.argv[1]) |
---|