ocrd_cis/ocropy/common.py
def check_page(binary, zoom=1.0):
    """Validate binary as a plausible printed text page image.

    Given a binarized, inverted image as Numpy array `binary`
    (with 0 for white and 1 for black),
    check the array has the right dimensions, is in fact inverted,
    and does not have too few or too many connected black components.

    Returns an error report, or None if valid.
    """
    if np.prod(binary.shape)==0: return "image dimensions are zero"
    if len(binary.shape)==3: return "image not monochrome %s"%(binary.shape,)
    if np.amax(binary)==np.amin(binary): return "image is blank"
    if np.mean(binary)<np.median(binary): return "image may be inverted"
    h,w = binary.shape
    if h<600/zoom: return "image not tall enough for a page image %s"%(binary.shape,)
    if h>10000/zoom: return "image too tall for a page image %s"%(binary.shape,)
    if w<600/zoom: return "image too narrow for a page image %s"%(binary.shape,)
    if w>10000/zoom: return "image too wide for a page image %s"%(binary.shape,)
    # zoom factor (DPI relative) and 4 (against fragmentation from binarization)
    slots = int(w*h*1.0/(30*30)*zoom*zoom) * 4
    _,ncomps = measurements.label(binary)
    if ncomps<10: return "too few connected components for a page image (got %d)"%(ncomps,)
    if ncomps>slots and ncomps>10: return "too many connected components for a page image (%d > %d)"%(ncomps,slots)
    return None
Validate binary as a plausible printed text page image.

    Given a binarized, inverted image as Numpy array `binary`
    (with 0 for white and 1 for black),
    check the array has the right dimensions, is in fact inverted,
    and does not have too few or too many connected black components.

    Returns an error report, or None if valid.
    
Validate binary as a plausible printed text page image.
Given a binarized, inverted image as Numpy array `binary`
(with 0 for white and 1 for black),
check the array has the right dimensions, is in fact inverted,
and does not have too few or too many connected black components.

Returns an error report, or None if valid.
Validate binary as a plausible printed text page image . Given a binarized inverted image as Numpy array ` binary ` ( with 0 for white and 1 for black ) check the array has the right dimensions is in fact inverted and does not have too few or too many connected black components . Returns an error report or None if valid .
def check_page(binary, zoom=1.0):
    if np.prod(binary.shape)==0: return "image dimensions are zero"
    if len(binary.shape)==3: return "image not monochrome %s"%(binary.shape,)
    if np.amax(binary)==np.amin(binary): return "image is blank"
    if np.mean(binary)<np.median(binary): return "image may be inverted"
    h,w = binary.shape
    if h<600/zoom: return "image not tall enough for a page image %s"%(binary.shape,)
    if h>10000/zoom: return "image too tall for a page image %s"%(binary.shape,)
    if w<600/zoom: return "image too narrow for a page image %s"%(binary.shape,)
    if w>10000/zoom: return "image too wide for a page image %s"%(binary.shape,)
    slots = int(w*h*1.0/(30*30)*zoom*zoom) * 4
    _,ncomps = measurements.label(binary)
    if ncomps<10: return "too few connected components for a page image (got %d)"%(ncomps,)
    if ncomps>slots and ncomps>10: return "too many connected components for a page image (%d > %d)"%(ncomps,slots)
    return None
def check_page ( binary , zoom = 1.0 ) : if np . prod ( binary . shape ) == 0 : return "image dimensions are zero" if len ( binary . shape ) == 3 : return "image not monochrome %s" % ( binary . shape , ) if np . amax ( binary ) == np . amin ( binary ) : return "image is blank" if np . mean ( binary ) < np . median ( binary ) : return "image may be inverted" h , w = binary . shape if h < 600 / zoom : return "image not tall enough for a page image %s" % ( binary . shape , ) if h > 10000 / zoom : return "image too tall for a page image %s" % ( binary . shape , ) if w < 600 / zoom : return "image too narrow for a page image %s" % ( binary . shape , ) if w > 10000 / zoom : return "image too wide for a page image %s" % ( binary . shape , ) slots = int ( w * h * 1.0 / ( 30 * 30 ) * zoom * zoom ) * 4 _ , ncomps = measurements . label ( binary ) if ncomps < 10 : return "too few connected components for a page image (got %d)" % ( ncomps , ) if ncomps > slots and ncomps > 10 : return "too many connected components for a page image (%d > %d)" % ( ncomps , slots ) return None
Validate binary as a plausible printed text page image.
Validate binary as a plausible printed text page image .
"""Validate binary as a plausible printed text page image.

    Given a binarized, inverted image as Numpy array `binary`
    (with 0 for white and 1 for black),
    check the array has the right dimensions, is in fact inverted,
    and does not have too few or too many connected black components.

    Returns an error report, or None if valid.
    """ # zoom factor (DPI relative) and 4 (against fragmentation from binarization)
binary
zoom