88from PIL import Image
99from playwright .sync_api import sync_playwright
1010
11+ # ==== Configurable defaults ====
12+ DEFAULT_WIDTH = 758
13+ DEFAULT_HEIGHT = 930
14+ DEFAULT_TEXT_SIZE = "16px"
15+ # ===============================
16+
1117class EPUBSmartSplitter :
1218 def __init__ (self , epub_path : Path , out_dir : Path , width : int , height : int , threshold : int , text_size : str ):
1319 self .epub_path = epub_path
@@ -102,17 +108,15 @@ def process_html(self, page, html_path: Path, start_idx: int) -> int:
102108 idx = start_idx
103109
104110 while current_top < total_height :
105- remaining_height = total_height - current_top # Fixed: Calculate remaining height
111+ remaining_height = total_height - current_top
106112 viewport_height = self .find_clean_break (
107113 page , scroller , current_top , remaining_height
108114 )
109115
110- # Set final viewport size and position
111116 page .set_viewport_size ({"width" : self .width , "height" : viewport_height })
112117 scroller .evaluate ("(el, top) => el.scrollTop = top" , current_top )
113118 page .wait_for_timeout (100 )
114119
115- # Capture and save
116120 screenshot = Image .open (BytesIO (page .screenshot ())).convert ("L" )
117121 bw_image = screenshot .point (lambda p : 255 if p > self .threshold else 0 , mode = "1" )
118122
@@ -153,11 +157,10 @@ def main():
153157 )
154158 parser .add_argument ("epub" , type = Path , help = "Input EPUB file" )
155159 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 ,
161164 help = "Font size to enforce (e.g., '12px', '1.2em')" )
162165 args = parser .parse_args ()
163166
@@ -175,4 +178,4 @@ def main():
175178 splitter .process ()
176179
177180if __name__ == "__main__" :
178- main ()
181+ main ()
0 commit comments