[277] | 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 | from PIL import Image |
---|
| 27 | |
---|
| 28 | """ RGB to CMYK formula : |
---|
| 29 | |
---|
| 30 | Black = min(1 - r, 1 - g, 1 - b) |
---|
| 31 | Cyan = (1 - r - Black) / (1 - Black) |
---|
| 32 | Magenta = (1 - g - Black) / (1 - Black) |
---|
| 33 | Yellow = (1 - b - Black) / (1 - Black) |
---|
| 34 | """ |
---|
| 35 | |
---|
| 36 | def percent_cmy(fname) : |
---|
| 37 | result = [] |
---|
| 38 | img = Image.open(fname) |
---|
| 39 | (r, g, b) = [ p.histogram() for p in img.split() ] |
---|
| 40 | nbpix = sum(r) |
---|
| 41 | for histo in (r, g, b) : |
---|
[278] | 42 | result.append((100 * (reduce(lambda x,y: x + (y[1] * (255 - y[0])), enumerate(histo), 0) / 255.0)) / nbpix) |
---|
[277] | 43 | return tuple(result) |
---|
| 44 | |
---|
| 45 | if __name__ == "__main__" : |
---|
| 46 | print percent_cmy(sys.argv[1]) |
---|