40 | | nbpix = sum(r) |
41 | | for histo in (r, g, b) : |
42 | | result.append((100 * (reduce(lambda x,y: x + (y[1] * (255 - y[0])), enumerate(histo), 0) / 255.0)) / nbpix) |
43 | | return tuple(result) |
| 39 | for colorhisto in (r, g, b) : |
| 40 | result.append(100.0 * (reduce(lambda current, next: current + (next[1] * (255 - next[0])), enumerate(colorhisto), 0) / 255.0) / nbpix) |
| 41 | return tuple(result) |
| 42 | |
| 43 | def getPercentBlack(img, nbpix) : |
| 44 | if img.mode != "L" : |
| 45 | img = img.convert("L") |
| 46 | return 100.0 * (reduce(lambda current, next: current + (next[1] * (255 - next[0])), enumerate(img.histogram()[:-1]), 0) / 255.0) / nbpix |
| 47 | |
| 48 | def getPercents(fname) : |
| 49 | """Extracts the ink percentages from an image.""" |
| 50 | image = Image.open(fname) |
| 51 | nbpixels = image.size[0] * image.size[1] |
| 52 | result = {} |
| 53 | try : |
| 54 | result["black"] = getPercentBlack(image, nbpixels) |
| 55 | except : |
| 56 | sys.stderr.write("Problem when extracting BLACK !\n") |
| 57 | try : |
| 58 | result["cmy"] = getPercentCMY(image, nbpixels) |
| 59 | except : |
| 60 | sys.stderr.write("Problem when extracting CMY !\n") |
| 61 | return result |