kraken API

Kraken provides routines which are usable by third party tools. In general you can expect function in the kraken package to remain stable. We will try to keep these backward compatible, but as kraken is still in an early development stage and the API is still quite rudimentary nothing can be garantueed.

kraken.binarization module

kraken.binarization.is_bitonal(im)

Tests a PIL.Image for bitonality.

Parameters:im (PIL.Image) – Image to test
Returns:True if the image contains only two different color values. False otherwise.
kraken.binarization.nlbin(im, threshold=0.5, zoom=0.5, escale=1.0, border=0.1, perc=80, range=20, low=5, high=90)

Performs binarization using non-linear processing.

Parameters:
  • im (PIL.Image) –
  • threshold (float) –
  • zoom (float) – Zoom for background page estimation
  • escale (float) – Scale for estimating a mask over the text region
  • border (float) – Ignore this much of the border
  • perc (int) – Percentage for filters
  • range (int) – Range for filters
  • low (int) – Percentile for black estimation
  • high (int) – Percentile for white estimation
Returns:

PIL.Image containing the binarized image

kraken.serialize module

kraken.pageseg module

kraken.pageseg.binary_objects(binary)
kraken.pageseg.compute_black_colseps(binary, scale)

Computes column separators from vertical black lines.

Parameters:
  • binary (numpy.array) – Numpy array of the binary image
  • scale (float) –
Returns:

Return type:

(colseps, binary)

kraken.pageseg.compute_boxmap(binary, scale, threshold=(0.5, 4), dtype=u'i')
kraken.pageseg.compute_colseps_conv(binary, scale=1.0, minheight=10, maxcolseps=2)

Find column separators by convolution and thresholding.

Parameters:
  • binary (numpy.array) –
  • scale (float) –
  • minheight (int) –
  • maxcolseps (int) –
Returns:

Separators

kraken.pageseg.compute_gradmaps(binary, scale, gauss=False)

Use gradient filtering to find baselines

Parameters:
  • binary (numpy.array) –
  • scale (float) –
  • gauss (bool) – Use gaussian instead of uniform filtering
Returns:

(bottom, top, boxmap)

kraken.pageseg.compute_line_seeds(binary, bottom, top, colseps, scale, threshold=0.2)

Base on gradient maps, computes candidates for baselines and xheights. Then, it marks the regions between the two as a line seed.

kraken.pageseg.compute_lines(segmentation, scale)

Given a line segmentation map, computes a list of tuples consisting of 2D slices and masked images.

kraken.pageseg.compute_separators_morph(binary, scale, sepwiden=10, maxcolseps=2)

Finds vertical black lines corresponding to column separators.

kraken.pageseg.compute_white_colseps(binary, scale)

Computes column separators either from vertical black lines or whitespace.

Parameters:
  • binary (numpy.array) – Numpy array of the binary image
  • scale (float) –
Returns:

Return type:

colseps

kraken.pageseg.estimate_scale(binary)
kraken.pageseg.find(condition)

Return the indices where ravel(condition) is true

kraken.pageseg.norm_max(v)
kraken.pageseg.reading_order(lines)

Given the list of lines (a list of 2D slices), computes the partial reading order. The output is a binary 2D array such that order[i,j] is true if line i comes before line j in reading order.

class kraken.pageseg.record(**kw)

Bases: future.types.newobject.newobject

kraken.pageseg.remove_hlines(binary, scale, maxsize=10)

Removes horizontal black lines that only interfere with page segmentation.

Args:
binary (numpy.array): scale (float): maxsize (int): maximum size of removed lines
Returns:
numpy.array containing the filtered image.
kraken.pageseg.segment(im, scale=None, black_colseps=False)

Segments a page into text lines.

Segments a page into text lines and returns the absolute coordinates of each line in reading order.

Parameters:
  • im (PIL.Image) – A bi-level page of mode ‘1’ or ‘L’
  • scale (float) – Scale of the image
  • black_colseps (bool) – Whether column separators are assumed to be vertical black lines or not
Returns:

[ – A list of tuples containing the bounding boxes

of the segmented lines in reading order.

Return type:

x1, y1, x2, y2

Raises:

KrakenInputException if the input image is not binarized

kraken.pageseg.topsort(order)

Given a binary array defining a partial order (o[i,j]==True means i<j), compute a topological sort. This is a quick and dirty implementation that works for up to a few thousand elements.

kraken.rpred module

kraken.transcrib module

Utility functions for ground truth transcription.

class kraken.transcrib.TranscriptionInterface(font=None, font_style=None)

Bases: object

add_page(im, segmentation=None, records=None)

Adds an image to the transcription interface, optionally filling in information from a list of ocr_record objects.

Parameters:
  • im (PIL.Image) – Input image
  • records (list) – A list of ocr_record objects.
write(fd)

Writes the HTML file to a file descriptor.

Parameters:fd (File) – File descriptor to write to.

kraken.train module

kraken.linegen module

= linegen ~~~~~~~

An advanced line generation tool using Pango for proper text shaping. The actual drawing code was adapted from the create_image utility from nototools available at [0].

[0] https://github.com/googlei18n/nototools

class kraken.linegen.LineGenerator(family='Sans', font_size=32, language=None)

Bases: future.types.newobject.newobject

Produces degraded line images using a single collection of font families.

render_line(text)

Draws a line onto a Cairo surface which will be converted to an pillow Image.

Parameters:

text (unicode) – A string which will be rendered as a single line.

Returns:

PIL.Image of mode ‘L’.

Raises:
  • KrakenCairoSurfaceException if the Cairo surface couldn’t be created
  • (usually caused by invalid dimensions.
class kraken.linegen.PangoContext

Bases: _ctypes.Structure

class kraken.linegen.PangoFontDescription

Bases: _ctypes.Structure

class kraken.linegen.PangoLayout

Bases: _ctypes.Structure

class kraken.linegen.PangoRectangle

Bases: _ctypes.Structure

height

Structure/Union member

width

Structure/Union member

x

Structure/Union member

y

Structure/Union member

kraken.linegen.degrade_line(im, mean=0.0, sigma=0.001, density=0.002)

Degrades a line image by adding several kinds of noise.

Parameters:
  • im (PIL.Image) – Input image
  • mean (float) – Mean of distribution for Gaussian noise
  • sigma (float) – Standard deviation for Gaussian noise
  • density (float) – Noise density for Salt and Pepper noiase
Returns:

PIL.Image in mode ‘L’

kraken.linegen.distort_line(im, distort=3.0, sigma=10.0, eps=0.03, delta=0.3)

Distorts a line image.

Run BEFORE degrade_line as a white border of 5 pixels will be added.

Parameters:
  • im (PIL.Image) – Input image
  • distort (float) –
  • sigma (float) –
  • eps (float) –
  • delta (float) –
Returns:

PIL.Image in mode ‘L’

class kraken.linegen.ensureBytes

Bases: future.types.newobject.newobject

Simple class ensuring the arguments of type char * are actually a series of bytes.

classmethod from_param(value)
kraken.linegen.ocropy_degrade(im, distort=1.0, dsigma=20.0, eps=0.03, delta=0.3, degradations=[(0.5, 0.0, 0.5, 0.0)])

Degrades and distorts a line using the same noise model used by ocropus.

Parameters:
  • im (PIL.Image) – Input image
  • distort (float) –
  • dsigma (float) –
  • eps (float) –
  • delta (float) –
  • degradations (list) – list returning 4-tuples corresponding to the degradations argument of ocropus-linegen.
Returns:

PIL.Image in mode ‘L’