Changeset 363 for pkpgcounter/trunk/pkpgpdls/inkcoverage.py
- Timestamp:
- 08/16/06 01:12:57 (18 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/inkcoverage.py
r361 r363 28 28 from PIL import Image 29 29 30 def getPercent(img, nbpix) : 31 """Extracts the percents per color component from a picture. 32 33 Faster without Psyco on my own machine. 34 """ 35 result = {} 36 bands = img.split() 37 for (i, bandname) in enumerate(img.getbands()) : 38 result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix 39 return result 40 30 41 def getPercentCMYK(img, nbpix) : 31 42 """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture. … … 51 62 } 52 63 53 def getPercent(img, nbpix) : 54 """Extracts the percents per color component from a picture. 55 56 Faster without Psyco on my own machine. 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 64 def getPercentBlack(img, nbpix) : 64 def getPercentBW(img, nbpix) : 65 65 """Extracts the percents of Black from a picture, once converted to gray levels.""" 66 66 if img.mode != "L" : 67 67 img = img.convert("L") 68 return { " L" : 100.0 - getPercent(img, nbpix)["L"] }68 return { "B" : 100.0 - getPercent(img, nbpix)["L"] } 69 69 70 70 def getPercentRGB(img, nbpix) : … … 82 82 } 83 83 84 def getPercents(fname) : 85 """Extracts the ink percentages from an image.""" 86 try : 87 import psyco 88 except ImportError : 89 pass 90 else : 91 psyco.bind(getPercentCMYK) 84 def getInkCoverage(fname, colorspace) : 85 """Returns a list of dictionnaries containing for each page, 86 for each color component, the percent of ink coverage on 87 that particular page. 88 """ 92 89 result = [] 90 colorspace = colorspace.upper() 91 computation = globals()["getPercent%s" % colorspace] 92 if colorspace == "CMYK" : # faster with psyco on my machine 93 try : 94 import psyco 95 except ImportError : 96 pass 97 else : 98 psyco.bind(computation) 99 93 100 index = 0 94 101 image = Image.open(fname) … … 96 103 while 1 : 97 104 nbpixels = image.size[0] * image.size[1] 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 })) 105 result.append({ colorspace : computation(image, nbpixels) }) 104 106 index += 1 105 107 image.seek(index) … … 110 112 if __name__ == "__main__" : 111 113 # NB : length of result gives number of pages ! 112 print get Percents(sys.argv[1])114 print getInkCoverage(sys.argv[1], "CMYK")