-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebtoon_panel_slicer.py
More file actions
97 lines (70 loc) · 3.09 KB
/
Copy pathwebtoon_panel_slicer.py
File metadata and controls
97 lines (70 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import cv2
import numpy as np
from PIL import Image
from glob import glob
from config import MIN_PANEL_HEIGHT
def is_blank_section(image_slice):
"""
Checks if a given image slice is blank (either all white or all black).
Parameters:
image_slice: numpy array containing grayscale image data
Returns:
bool: True if slice is blank, False otherwise
"""
return np.all(image_slice == 255) or np.all(image_slice == 0)
def slice_webtoon_into_color_panels(image_path):
"""
Detects and slices a colored webtoon into individual panels based on blank sections.
Parameters:
image_path: str, path to the webtoon image
Returns:
list: A list of sliced colored panels (as numpy arrays)
"""
color_image = cv2.imread(image_path)
grayscale_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
image_height, _ = grayscale_image.shape
current_row = 0
sliced_panels = []
for i in range(1, image_height):
row_pixels = grayscale_image[i, :]
if is_blank_section(row_pixels):
candidate_panel = color_image[current_row:i, :, :]
candidate_panel_gray = grayscale_image[current_row:i, :]
if not is_blank_section(candidate_panel_gray) and candidate_panel.shape[0] >= MIN_PANEL_HEIGHT:
sliced_panels.append(candidate_panel)
current_row = i + 1
last_panel = color_image[current_row:, :, :]
last_panel_gray = grayscale_image[current_row:, :]
if not is_blank_section(last_panel_gray) and last_panel.shape[0] >= MIN_PANEL_HEIGHT:
sliced_panels.append(last_panel)
return sliced_panels
def slice_webtoon_into_panels(image_path):
"""
Detects and slices a webtoon into individual panels based on blank sections.
Parameters:
image_path: str, path to the webtoon image
Returns:
list: A list of sliced panels (as numpy arrays)
"""
grayscale_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
image_height, _ = grayscale_image.shape
current_row = 0
sliced_panels = []
for i in range(1, image_height):
row_pixels = grayscale_image[i, :]
if is_blank_section(row_pixels):
candidate_panel = grayscale_image[current_row:i, :]
if not is_blank_section(candidate_panel) and candidate_panel.shape[0] >= MIN_PANEL_HEIGHT:
sliced_panels.append(candidate_panel)
current_row = i + 1
last_panel = grayscale_image[current_row:, :]
if not is_blank_section(last_panel) and last_panel.shape[0] >= MIN_PANEL_HEIGHT:
sliced_panels.append(last_panel)
return sliced_panels
# Example usage:
# color_panels = slice_webtoon_into_color_panels("path/to/your/color/image.jpg")
# for panel in color_panels:
# display(Image.fromarray(cv2.cvtColor(panel, cv2.COLOR_BGR2RGB)))
# grayscale_panels = slice_webtoon_into_panels("path/to/your/grayscale/image.jpg")
# for panel in grayscale_panels:
# display(Image.fromarray(panel, 'L'))