Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/eynollah/ocrd_cli_binarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor

from .sbb_binarize import SbbBinarizer


def cv2pil(img):
return Image.fromarray(img.astype('uint8'))

def pil2cv(img):
# from ocrd/workspace.py
color_conversion = cv2.COLOR_GRAY2BGR if img.mode in ('1', 'L') else cv2.COLOR_RGB2BGR
pil_as_np_array = np.array(img).astype('uint8') if img.mode == '1' else np.array(img)
return cv2.cvtColor(pil_as_np_array, color_conversion)
from .utils.pil_cv2 import cv2pil, pil2cv

class SbbBinarizeProcessor(Processor):
# already employs GPU (without singleton process atm)
Expand Down
10 changes: 8 additions & 2 deletions src/eynollah/utils/pil_cv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
# from sbb_binarization

def cv2pil(img):
return Image.fromarray(np.array(cvtColor(img, COLOR_BGR2RGB)))
# reduce depth because cvtColor is limited
return Image.fromarray(np.array(cvtColor(img.astype(np.uint8), COLOR_BGR2RGB)))

def pil2cv(img):
# from ocrd/workspace.py
color_conversion = COLOR_GRAY2BGR if img.mode in ('1', 'L') else COLOR_RGB2BGR
color_conversion = COLOR_GRAY2BGR if img.mode in ('1', 'L', 'LA') else COLOR_RGB2BGR
pil_as_np_array = np.array(img).astype('uint8') if img.mode == '1' else np.array(img)
# cvtColor cannot handle alpha
if pil_as_np_array.shape[-1] == 2:
pil_as_np_array = pil_as_np_array[:,:,0]
elif pil_as_np_array.shape[-1] == 4:
pil_as_np_array = pil_as_np_array[:,:,:3]
return cvtColor(pil_as_np_array, color_conversion)

def check_dpi(img):
Expand Down