ocrd_cis/ocropy/common.py
def check_line(binary, zoom=1.0):
    """Validate binary as a plausible text line 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 is 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<20/zoom: return "image not tall enough for a text line %s"%(binary.shape,)
    if h>200/zoom: return "image too tall for a text line %s"%(binary.shape,)
    ##if w<1.5*h: return "line too short %s"%(binary.shape,)
    if w<1.5*h and w<32/zoom: return "image too short for a line image %s"%(binary.shape,)
    if w>4000/zoom: return "image too long for a line image %s"%(binary.shape,)
    ratio = w*1.0/h
    _, ncomps = measurements.label(binary)
    lo = int(0.5*ratio+0.5)
    hi = int(4*ratio)+1
    if ncomps<lo: return "too few connected components (got %d, wanted >=%d)"%(ncomps,lo)
    ##if ncomps>hi*ratio: return "too many connected components (got %d, wanted <=%d)"%(ncomps,hi)
    if ncomps>hi*ratio and ncomps>10: return "too many connected components (got %d, wanted <=%d)"%(ncomps,hi)
    return None
Validate binary as a plausible text line 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 text line 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 text line 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_line(binary, zoom=1.0):
    if np.prod(binary.shape)==0: return "image dimensions are zero"
    if len(binary.shape)==3: return "image is 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<20/zoom: return "image not tall enough for a text line %s"%(binary.shape,)
    if h>200/zoom: return "image too tall for a text line %s"%(binary.shape,)
    if w<1.5*h and w<32/zoom: return "image too short for a line image %s"%(binary.shape,)
    if w>4000/zoom: return "image too long for a line image %s"%(binary.shape,)
    ratio = w*1.0/h
    _, ncomps = measurements.label(binary)
    lo = int(0.5*ratio+0.5)
    hi = int(4*ratio)+1
    if ncomps<lo: return "too few connected components (got %d, wanted >=%d)"%(ncomps,lo)
    if ncomps>hi*ratio and ncomps>10: return "too many connected components (got %d, wanted <=%d)"%(ncomps,hi)
    return None
def check_line ( binary , zoom = 1.0 ) : if np . prod ( binary . shape ) == 0 : return "image dimensions are zero" if len ( binary . shape ) == 3 : return "image is 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 < 20 / zoom : return "image not tall enough for a text line %s" % ( binary . shape , ) if h > 200 / zoom : return "image too tall for a text line %s" % ( binary . shape , ) if w < 1.5 * h and w < 32 / zoom : return "image too short for a line image %s" % ( binary . shape , ) if w > 4000 / zoom : return "image too long for a line image %s" % ( binary . shape , ) ratio = w * 1.0 / h _ , ncomps = measurements . label ( binary ) lo = int ( 0.5 * ratio + 0.5 ) hi = int ( 4 * ratio ) + 1 if ncomps < lo : return "too few connected components (got %d, wanted >=%d)" % ( ncomps , lo ) if ncomps > hi * ratio and ncomps > 10 : return "too many connected components (got %d, wanted <=%d)" % ( ncomps , hi ) return None
Validate binary as a plausible text line image.
Validate binary as a plausible text line image .
"""Validate binary as a plausible text line 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 w<1.5*h: return "line too short %s"%(binary.shape,) ##if ncomps>hi*ratio: return "too many connected components (got %d, wanted <=%d)"%(ncomps,hi)
binary
zoom