cv2pil: limit color depth on output imgs

This commit is contained in:
Robert Sachunsky 2025-06-13 01:21:25 +02:00
parent dadb879376
commit 42a3cc2335

View file

@ -7,12 +7,14 @@ from cv2 import COLOR_GRAY2BGR, COLOR_RGB2BGR, COLOR_BGR2RGB, cvtColor, imread
# 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', '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: