8
8
from PIL import Image
9
9
from playwright .sync_api import sync_playwright
10
10
11
+ # ==== Configurable defaults ====
12
+ DEFAULT_WIDTH = 758
13
+ DEFAULT_HEIGHT = 930
14
+ DEFAULT_TEXT_SIZE = "16px"
15
+ # ===============================
16
+
11
17
class EPUBSmartSplitter :
12
18
def __init__ (self , epub_path : Path , out_dir : Path , width : int , height : int , threshold : int , text_size : str ):
13
19
self .epub_path = epub_path
@@ -102,17 +108,15 @@ def process_html(self, page, html_path: Path, start_idx: int) -> int:
102
108
idx = start_idx
103
109
104
110
while current_top < total_height :
105
- remaining_height = total_height - current_top # Fixed: Calculate remaining height
111
+ remaining_height = total_height - current_top
106
112
viewport_height = self .find_clean_break (
107
113
page , scroller , current_top , remaining_height
108
114
)
109
115
110
- # Set final viewport size and position
111
116
page .set_viewport_size ({"width" : self .width , "height" : viewport_height })
112
117
scroller .evaluate ("(el, top) => el.scrollTop = top" , current_top )
113
118
page .wait_for_timeout (100 )
114
119
115
- # Capture and save
116
120
screenshot = Image .open (BytesIO (page .screenshot ())).convert ("L" )
117
121
bw_image = screenshot .point (lambda p : 255 if p > self .threshold else 0 , mode = "1" )
118
122
@@ -153,11 +157,10 @@ def main():
153
157
)
154
158
parser .add_argument ("epub" , type = Path , help = "Input EPUB file" )
155
159
parser .add_argument ("outdir" , type = Path , help = "Output directory" )
156
- parser .add_argument ("--width" , type = int , default = 758 , help = "Viewport width" )
157
- parser .add_argument ("--height" , type = int , default = 940 , help = "Base viewport height" )
158
- parser .add_argument ("--threshold" , type = int , default = 220 ,
159
- help = "White threshold (0-255)" )
160
- parser .add_argument ("--text-size" , type = str , default = "16px" ,
160
+ parser .add_argument ("--width" , type = int , default = DEFAULT_WIDTH , help = "Viewport width" )
161
+ parser .add_argument ("--height" , type = int , default = DEFAULT_HEIGHT , help = "Base viewport height" )
162
+ parser .add_argument ("--threshold" , type = int , default = 220 , help = "White threshold (0-255)" )
163
+ parser .add_argument ("--text-size" , type = str , default = DEFAULT_TEXT_SIZE ,
161
164
help = "Font size to enforce (e.g., '12px', '1.2em')" )
162
165
args = parser .parse_args ()
163
166
@@ -175,4 +178,4 @@ def main():
175
178
splitter .process ()
176
179
177
180
if __name__ == "__main__" :
178
- main ()
181
+ main ()
0 commit comments