Show
Ignore:
Timestamp:
10/06/08 00:22:07 (16 years ago)
Author:
jerome
Message:

Removed spaces at EOL.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/inkcoverage.py

    r3410 r3436  
    88# the Free Software Foundation, either version 3 of the License, or 
    99# (at your option) any later version. 
    10 #  
     10# 
    1111# This program is distributed in the hope that it will be useful, 
    1212# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414# GNU General Public License for more details. 
    15 #  
     15# 
    1616# You should have received a copy of the GNU General Public License 
    1717# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     
    2828try : 
    2929    from PIL import Image 
    30 except ImportError :     
     30except ImportError : 
    3131    sys.stderr.write("ERROR: You MUST install the Python Imaging Library (python-imaging) for pkpgcounter to work.\n") 
    3232    raise pdlparser.PDLParserError, "The Python Imaging Library is missing." 
     
    3434def getPercent(img, nbpix) : 
    3535    """Extracts the percents per color component from a picture. 
    36        
     36 
    3737       Faster without Psyco on my own machine. 
    3838    """ 
    39     result = {}      
     39    result = {} 
    4040    bands = img.split() 
    4141    for (i, bandname) in enumerate(img.getbands()) : 
    4242        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 
    4545def getPercentCMYK(img, nbpix) : 
    4646    """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture. 
    47       
     47 
    4848       PIL doesn't produce useable CMYK for our algorithm, so we use the algorithm from PrintBill. 
    4949       Psyco speeds this function up by around 2.5 times on my computer. 
     
    5151    if img.mode != "RGB" : 
    5252        img = img.convert("RGB") 
    53     cyan = magenta = yellow = black = 0     
     53    cyan = magenta = yellow = black = 0 
    5454    for (r, g, b) in img.getdata() : 
    5555        if r == g == b : 
    5656            black += 255 - r 
    57         else :     
     57        else : 
    5858            cyan += 255 - r 
    5959            magenta += 255 - g 
     
    6464             "K" : 100.0 * (black / 255.0) / nbpix, 
    6565           } 
    66          
    67 def getPercentGC(img, nbpix) :         
     66 
     67def getPercentGC(img, nbpix) : 
    6868    """Determines if a page is in grayscale or colour mode.""" 
    6969    if img.mode != "RGB" : 
     
    7575            return { "G" : 0.0, "C" : 100.0 } 
    7676    return { "G" : 100.0, "C" : 0.0 } 
    77      
     77 
    7878def getPercentBW(img, nbpix) : 
    7979    """Extracts the percents of Black from a picture, once converted to gray levels.""" 
     
    8181        img = img.convert("L") 
    8282    return { "B" : 100.0 - getPercent(img, nbpix)["L"] } 
    83      
     83 
    8484def getPercentRGB(img, nbpix) : 
    8585    """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB.""" 
    8686    if img.mode != "RGB" : 
    8787        img = img.convert("RGB") 
    88     return getPercent(img, nbpix)     
    89      
     88    return getPercent(img, nbpix) 
     89 
    9090def getPercentCMY(img, nbpix) : 
    9191    """Extracts the percents of Cyan, Magenta, and Yellow from a picture once converted to RGB.""" 
     
    9595             "Y" : 100.0 - result["B"], 
    9696           } 
    97      
     97 
    9898def 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 
    101101       that particular page. 
    102102    """ 
     
    107107        try : 
    108108            import psyco 
    109         except ImportError :     
     109        except ImportError : 
    110110            pass 
    111         else :     
     111        else : 
    112112            psyco.bind(getPercentCMYK) 
    113      
     113 
    114114    index = 0 
    115115    try : 
    116116        image = Image.open(fname) 
    117     except (IOError, OverflowError), msg :    
     117    except (IOError, OverflowError), msg : 
    118118        raise pdlparser.PDLParserError, "%s (%s)" % (msg, fname) 
    119     else :     
     119    else : 
    120120        try : 
    121121            while True : 
    122122                nbpixels = image.size[0] * image.size[1] 
    123123                result.append(computation(image, nbpixels)) 
    124                 index += 1               
     124                index += 1 
    125125                image.seek(index) 
    126         except EOFError :         
     126        except EOFError : 
    127127            pass 
    128128        return (colorspace, result)