# Path: kraken/lib/util.py
# def pil2array(im: Image.Image, alpha: int = 0) -> np.ndarray:
#     if im.mode == '1':
#         return np.array(im.convert('L'))
#     return np.array(im)
# 
# def array2pil(a: np.ndarray) -> Image.Image:
#     if a.dtype == np.dtype("B"):
#         if a.ndim == 2:
#             return Image.frombytes("L", (a.shape[1], a.shape[0]),
#                                    a.tobytes())
#         elif a.ndim == 3:
#             return Image.frombytes("RGB", (a.shape[1], a.shape[0]),
#                                    a.tobytes())
#         else:
#             raise Exception("bad image rank")
#     elif a.dtype == np.dtype('float32'):
#         return Image.frombytes("F", (a.shape[1], a.shape[0]), a.tobytes())
#     else:
#         raise Exception("unknown image type")


import warnings
import numpy as np
from PIL import Image
from kraken.lib.util import pil2array, array2pil
from scipy.ndimage import interpolation, filters
    return output


class CenterNormalizer(object):
    def __init__(self, target_height=48, params=(4, 1.0, 0.3)):
        self.target_height = target_height
        self.range, self.smoothness, self.extra = params

    def setHeight(self, target_height):
        self.target_height = target_height

    def measure(self, line):
        h, w = line.shape
        # XXX: this filter is awfully slow
        smoothed = filters.gaussian_filter(line, (h*0.5, h*self.smoothness),
                                           mode='constant')
        smoothed += 0.001*filters.uniform_filter(smoothed, (h*0.5, w),
                                                 mode='constant')
        self.shape = (h, w)
        a = np.argmax(smoothed, axis=0)
        a = filters.gaussian_filter(a, h*self.extra)
        self.center = np.array(a, 'i')
        deltas = np.abs(np.arange(h)[:, np.newaxis]-self.center[np.newaxis, :])
        self.mad = np.mean(deltas[line != 0])
        self.r = int(1+self.range*self.mad)

    def dewarp(self, img, cval=0, dtype=np.dtype('f')):
        if img.shape != self.shape:
            raise Exception('Measured and dewarp image shapes different')
        h, w = img.shape

# Path: kraken/lib/util.py
# def pil2array(im: Image.Image, alpha: int = 0) -> np.ndarray:
#     if im.mode == '1':
#         return np.array(im.convert('L'))
#     return np.array(im)
# 
# def array2pil(a: np.ndarray) -> Image.Image:
#     if a.dtype == np.dtype("B"):
#         if a.ndim == 2:
#             return Image.frombytes("L", (a.shape[1], a.shape[0]),
#                                    a.tobytes())
#         elif a.ndim == 3:
#             return Image.frombytes("RGB", (a.shape[1], a.shape[0]),
#                                    a.tobytes())
#         else:
#             raise Exception("bad image rank")
#     elif a.dtype == np.dtype('float32'):
#         return Image.frombytes("F", (a.shape[1], a.shape[0]), a.tobytes())
#     else:
#         raise Exception("unknown image type")


# Path: kraken/lib/lineest.py
import warnings
import numpy as np
from PIL import Image
from kraken.lib.util import pil2array, array2pil
from scipy.ndimage import interpolation, filters

    return output


class CenterNormalizer(object):
    def __init__(self, target_height=48, params=(4, 1.0, 0.3)):
        self.target_height = target_height
        self.range, self.smoothness, self.extra = params

    def setHeight(self, target_height):
        self.target_height = target_height

    def measure(self, line):
        h, w = line.shape
        # XXX: this filter is awfully slow
        smoothed = filters.gaussian_filter(line, (h*0.5, h*self.smoothness),
                                           mode='constant')
        smoothed += 0.001*filters.uniform_filter(smoothed, (h*0.5, w),
                                                 mode='constant')
        self.shape = (h, w)
        a = np.argmax(smoothed, axis=0)
        a = filters.gaussian_filter(a, h*self.extra)
        self.center = np.array(a, 'i')
        deltas = np.abs(np.arange(h)[:, np.newaxis]-self.center[np.newaxis, :])
        self.mad = np.mean(deltas[line != 0])
        self.r = int(1+self.range*self.mad)

    def dewarp(self, img, cval=0, dtype=np.dtype('f')):
        if img.shape != self.shape:
            raise Exception('Measured and dewarp image shapes different')
        h, w = img.shape

        padded = np.vstack([cval*np.ones((h, w)), img, cval*np.ones((h, w))])