Changeset 3436 for pkpgcounter/trunk/pkpgpdls/inkcoverage.py
- Timestamp:
- 10/06/08 00:22:07 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/inkcoverage.py
r3410 r3436 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 28 28 try : 29 29 from PIL import Image 30 except ImportError : 30 except ImportError : 31 31 sys.stderr.write("ERROR: You MUST install the Python Imaging Library (python-imaging) for pkpgcounter to work.\n") 32 32 raise pdlparser.PDLParserError, "The Python Imaging Library is missing." … … 34 34 def getPercent(img, nbpix) : 35 35 """Extracts the percents per color component from a picture. 36 36 37 37 Faster without Psyco on my own machine. 38 38 """ 39 result = {} 39 result = {} 40 40 bands = img.split() 41 41 for (i, bandname) in enumerate(img.getbands()) : 42 42 result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix 43 return result 44 43 return result 44 45 45 def getPercentCMYK(img, nbpix) : 46 46 """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture. 47 47 48 48 PIL doesn't produce useable CMYK for our algorithm, so we use the algorithm from PrintBill. 49 49 Psyco speeds this function up by around 2.5 times on my computer. … … 51 51 if img.mode != "RGB" : 52 52 img = img.convert("RGB") 53 cyan = magenta = yellow = black = 0 53 cyan = magenta = yellow = black = 0 54 54 for (r, g, b) in img.getdata() : 55 55 if r == g == b : 56 56 black += 255 - r 57 else : 57 else : 58 58 cyan += 255 - r 59 59 magenta += 255 - g … … 64 64 "K" : 100.0 * (black / 255.0) / nbpix, 65 65 } 66 67 def getPercentGC(img, nbpix) : 66 67 def getPercentGC(img, nbpix) : 68 68 """Determines if a page is in grayscale or colour mode.""" 69 69 if img.mode != "RGB" : … … 75 75 return { "G" : 0.0, "C" : 100.0 } 76 76 return { "G" : 100.0, "C" : 0.0 } 77 77 78 78 def getPercentBW(img, nbpix) : 79 79 """Extracts the percents of Black from a picture, once converted to gray levels.""" … … 81 81 img = img.convert("L") 82 82 return { "B" : 100.0 - getPercent(img, nbpix)["L"] } 83 83 84 84 def getPercentRGB(img, nbpix) : 85 85 """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB.""" 86 86 if img.mode != "RGB" : 87 87 img = img.convert("RGB") 88 return getPercent(img, nbpix) 89 88 return getPercent(img, nbpix) 89 90 90 def getPercentCMY(img, nbpix) : 91 91 """Extracts the percents of Cyan, Magenta, and Yellow from a picture once converted to RGB.""" … … 95 95 "Y" : 100.0 - result["B"], 96 96 } 97 97 98 98 def getInkCoverage(fname, colorspace) : 99 """Returns a list of dictionnaries containing for each page, 100 for each color component, the percent of ink coverage on 99 """Returns a list of dictionnaries containing for each page, 100 for each color component, the percent of ink coverage on 101 101 that particular page. 102 102 """ … … 107 107 try : 108 108 import psyco 109 except ImportError : 109 except ImportError : 110 110 pass 111 else : 111 else : 112 112 psyco.bind(getPercentCMYK) 113 113 114 114 index = 0 115 115 try : 116 116 image = Image.open(fname) 117 except (IOError, OverflowError), msg : 117 except (IOError, OverflowError), msg : 118 118 raise pdlparser.PDLParserError, "%s (%s)" % (msg, fname) 119 else : 119 else : 120 120 try : 121 121 while True : 122 122 nbpixels = image.size[0] * image.size[1] 123 123 result.append(computation(image, nbpixels)) 124 index += 1 124 index += 1 125 125 image.seek(index) 126 except EOFError : 126 except EOFError : 127 127 pass 128 128 return (colorspace, result)