# 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
def scale_to_h(img, target_height, order=1, dtype=np.dtype('f'), cval=0):
    h, w = img.shape
    scale = target_height*1.0/h
    target_width = int(scale*w)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', UserWarning)
        output = interpolation.affine_transform(1.0*img, np.ones(2)/scale,
                                                order=order,
                                                output_shape=(target_height,
                                                              target_width),
                                                mode='constant', cval=cval)
    output = np.array(output, dtype=dtype)
    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')

# 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

def scale_to_h(img, target_height, order=1, dtype=np.dtype('f'), cval=0):
    h, w = img.shape
    scale = target_height*1.0/h
    target_width = int(scale*w)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', UserWarning)
        output = interpolation.affine_transform(1.0*img, np.ones(2)/scale,
                                                order=order,
                                                output_shape=(target_height,
                                                              target_width),
                                                mode='constant', cval=cval)
    output = np.array(output, dtype=dtype)
    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)