mirror of
https://github.com/qurator-spk/eynollah.git
synced 2025-07-01 15:09:54 +02:00
cv2pil: limit color depth on output imgs
This commit is contained in:
parent
dadb879376
commit
42a3cc2335
1 changed files with 3 additions and 1 deletions
|
@ -7,12 +7,14 @@ from cv2 import COLOR_GRAY2BGR, COLOR_RGB2BGR, COLOR_BGR2RGB, cvtColor, imread
|
||||||
# from sbb_binarization
|
# from sbb_binarization
|
||||||
|
|
||||||
def cv2pil(img):
|
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):
|
def pil2cv(img):
|
||||||
# from ocrd/workspace.py
|
# from ocrd/workspace.py
|
||||||
color_conversion = COLOR_GRAY2BGR if img.mode in ('1', 'L', 'LA') 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)
|
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:
|
if pil_as_np_array.shape[-1] == 2:
|
||||||
pil_as_np_array = pil_as_np_array[:,:,0]
|
pil_as_np_array = pil_as_np_array[:,:,0]
|
||||||
elif pil_as_np_array.shape[-1] == 4:
|
elif pil_as_np_array.shape[-1] == 4:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue