Changeset 279 for pkpgcounter

Show
Ignore:
Timestamp:
11/05/05 10:04:18 (18 years ago)
Author:
jerome
Message:

Now extracts black as well. For black the result is identical
to PrintBill?'s one when parsing the same 8-bit gray png images,
although my own code accepts other formats.

Files:
1 modified

Legend:

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

    r278 r279  
    3434"""         
    3535 
    36 def percent_cmy(fname) : 
     36def getPercentCMY(img, nbpix) : 
    3737    result = [] 
    38     img = Image.open(fname) 
    3938    (r, g, b) = [ p.histogram() for p in img.split() ] 
    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     
     43def 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     
     48def 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 
    4462 
    4563if __name__ == "__main__" : 
    46     print percent_cmy(sys.argv[1]) 
     64    print getPercents(sys.argv[1])