Skip to content

Commit 8e7e212

Browse files
authored
Merge pull request #376 from sbrunner/support-rgb-on-gray
Support r,g,b when we use a gray image
2 parents f7a9f1a + 9e0a9fe commit 8e7e212

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

deskew/cli.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import argparse
22
import sys
3-
from typing import List, Union
43

54
import numpy as np
65
from skimage import io
@@ -31,24 +30,23 @@ def main() -> None:
3130
print(f"Estimated angle: {angle}")
3231
else:
3332
if options.background:
34-
background: Union[int, List[int]]
35-
if len(image.shape) == 2:
36-
try:
37-
background = int(options.background)
38-
except: # pylint: disable=bare-except
33+
try:
34+
background = [int(c) for c in options.background.split(",")]
35+
except: # pylint: disable=bare-except
36+
if len(image.shape) == 2:
3937
print("Wrong background color, should be gray")
40-
sys.exit(1)
41-
else:
42-
try:
43-
background = [int(c) for c in options.background.split(",")]
44-
except: # pylint: disable=bare-except
38+
else:
4539
print("Wrong background color, should be r,g,b")
46-
sys.exit(1)
40+
sys.exit(1)
41+
42+
if len(image.shape) != 2 and len(background) != image.shape[2]:
43+
print("Wrong background color, should be r,g,b")
44+
sys.exit(1)
4745

4846
rotated = rotate(image, angle, resize=True, cval=-1) * 255
4947
pos = np.where(rotated == -255)
5048
if len(image.shape) == 2:
51-
rotated[pos[0], pos[1]] = background
49+
rotated[pos[0], pos[1]] = int(round(np.mean(background)))
5250
else:
5351
rotated[pos[0], pos[1], :] = background
5452
else:

0 commit comments

Comments
 (0)