Changeset 280 for pkpgcounter/trunk

Show
Ignore:
Timestamp:
11/06/05 00:07:19 (19 years ago)
Author:
jerome
Message:

Now computes ink coverage in : Black, RGB, CMY, and CMYK.
For CMYK we use PrintBill?'s algorithm since PIL doesn't produce
CMYK datas that are suitable for our own code (inspired from
PrintBill? anyway)

Files:
1 modified

Legend:

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

    r279 r280  
    2626from PIL import Image 
    2727 
    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 getPercentCMY(img, nbpix) : 
    37     result = [] 
    38     (r, g, b) = [ p.histogram() for p in img.split() ] 
    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      
     28def getPercentCMYK(img, nbpix) : 
     29    """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture. 
     30      
     31       PIL doesn't produce useable CMYK for our algorithm, so we use the algorithm from PrintBill. 
     32       Psyco speeds this function up by around 2.5 times on my computer. 
     33    """ 
     34    if img.mode != "RGB" : 
     35        img = img.convert("RGB") 
     36    cyan = magenta = yellow = black = 0     
     37    for (r, g, b) in img.getdata() : 
     38        if r == g == b : 
     39            black += 255 - r 
     40        else :     
     41            cyan += 255 - r 
     42            magenta += 255 - g 
     43            yellow += 255 - b 
     44    return { "C" : 100.0 * (cyan / 255.0) / nbpix, 
     45             "M" : 100.0 * (magenta / 255.0) / nbpix, 
     46             "Y" : 100.0 * (yellow / 255.0) / nbpix, 
     47             "K" : 100.0 * (black / 255.0) / nbpix, 
     48           } 
     49         
     50def getPercent(img, nbpix) : 
     51    """Extracts the percents per color component from a picture. 
     52       
     53       Faster without Pysco. 
     54    """ 
     55    result = {}      
     56    bands = img.split() 
     57    for (i, bandname) in enumerate(img.getbands()) : 
     58        result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix 
     59    return result     
     60         
    4361def getPercentBlack(img, nbpix) : 
     62    """Extracts the percents of Black from a picture, once converted to gray levels.""" 
    4463    if img.mode != "L" : 
    4564        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 
     65    return { "L" : 100.0 - getPercent(img, nbpix)["L"] } 
     66     
     67def getPercentRGB(img, nbpix) : 
     68    """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB.""" 
     69    if img.mode != "RGB" : 
     70        img = img.convert("RGB") 
     71    return getPercent(img, nbpix)     
     72     
     73def getPercentCMY(img, nbpix) : 
     74    """Extracts the percents of Cyan, Magenta, and Yellow from a picture once converted to RGB.""" 
     75    result = getPercentRGB(img, nbpix) 
     76    return { "C" : 100.0 - result["R"], 
     77             "M" : 100.0 - result["G"], 
     78             "Y" : 100.0 - result["B"], 
     79           } 
    4780     
    4881def getPercents(fname) : 
     
    5083    image = Image.open(fname) 
    5184    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 
     85    black = getPercentBlack(image, nbpixels) 
     86    rgb = getPercentRGB(image, nbpixels) 
     87    cmy = getPercentCMY(image, nbpixels) 
     88    cmyk = getPercentCMYK(image, nbpixels) 
     89    print "Black : ", black 
     90    print "RGB : ", rgb 
     91    print "CMY : ", cmy 
     92    print "CMYK : ", cmyk 
    6293 
    6394if __name__ == "__main__" : 
    64     print getPercents(sys.argv[1]) 
     95    getPercents(sys.argv[1])