pil2cv: remove alpha, if any

This commit is contained in:
Robert Sachunsky 2026-07-28 15:23:10 +02:00
parent e71923c684
commit 72f4a254af

View file

@ -10,6 +10,9 @@ def cv2pil(img):
return Image.fromarray(np.array(cvtColor(img, COLOR_BGR2RGB)))
def pil2cv(img):
if img.mode.endswith(('a', 'A')):
# remove transparency (RGBA, LA, PA, and pre-multiplied variants)
img = img.convert(img.mode[:-1])
# from ocrd/workspace.py
color_conversion = COLOR_GRAY2BGR if img.mode in ('1', 'L') else COLOR_RGB2BGR
pil_as_np_array = np.array(img).astype('uint8') if img.mode == '1' else np.array(img)