import gradio as gr import numpy as np import cv2 from pathlib import Path import zipfile import tempfile import shutil import os import re import time import requests from PIL import Image from io import BytesIO import base64 import urllib.parse try: import undetected_chromedriver as uc from selenium.webdriver.common.by import By from pyvirtualdisplay import Display import ssl ssl._create_default_https_context = ssl._create_unverified_context SCRAPER_AVAILABLE = True except ImportError: SCRAPER_AVAILABLE = False from manhwa_extractor import ManhwaProcessor from bs4 import BeautifulSoup # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- IMAGE_SUFFIXES = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"} NATURAL_TOKEN_RE = re.compile(r"(\d+)") def _common_prefix_name(names: list) -> str: if not names: return "manhwa_batch" if len(names) == 1: return names[0] prefix = os.path.commonprefix(names).rstrip("_- ") return prefix if len(prefix) >= 3 else f"{names[0]}_{names[-1]}_batch" def _natural_key(value) -> list: text = str(value).lower() return [int(token) if token.isdigit() else token for token in NATURAL_TOKEN_RE.split(text)] def _is_image_path(path: Path) -> bool: return path.suffix.lower() in IMAGE_SUFFIXES and not any(part.startswith(".") for part in path.parts) def _resolve_gradio_path(file_obj): if file_obj is None: return None return Path(file_obj if isinstance(file_obj, str) else getattr(file_obj, "name", str(file_obj))) def _sanitize_folder_name(name: str, fallback: str) -> str: cleaned = re.sub(r"[^\w.\- /]+", "_", (name or "").strip()).strip(" ./") return cleaned or fallback def _build_settings(min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels): return dict( min_panel_area=min_panel_area, edge_sigma=edge_sigma, low_threshold=low_threshold, high_threshold=high_threshold, reading_order=reading_order, refine_panels=refine_panels, ) def _extract_panels_for_image(image_path: Path, settings: dict): processor = ManhwaProcessor( min_panel_area_ratio=settings["min_panel_area"] / 100, edge_detection_sigma=settings["edge_sigma"], low_threshold=settings["low_threshold"], high_threshold=settings["high_threshold"], reading_order=settings["reading_order"], ) loaded = processor.load_image(str(image_path)) if loaded is None: raise ValueError(f"Could not load {image_path.name}") segmentation = processor.detect_edges() processor.identify_panels(segmentation) processor.extract_panels() processor.order_panels() panels = [] for panel_data in processor.ordered_panels: panel = panel_data["panel"] if settings["refine_panels"]: panel = processor.refine_panel(panel)[0] panels.append(panel) return panels def _zip_directory_to_file(root_dir: Path, zip_name: str) -> str: root_dir.mkdir(parents=True, exist_ok=True) zip_path = Path(tempfile.gettempdir()) / f"{zip_name}.zip" with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf: for file_path in sorted(root_dir.rglob("*"), key=lambda p: _natural_key(p.relative_to(root_dir).as_posix())): if file_path.is_file(): zf.write(file_path, arcname=str(file_path.relative_to(root_dir))) return str(zip_path) def _process_chapter_sequence(image_paths: list[Path], chapter_rel_path: Path, settings: dict, output_root: Path): chapter_dir = output_root / chapter_rel_path chapter_dir.mkdir(parents=True, exist_ok=True) saved_paths = [] issues = [] panel_index = 1 for image_path in sorted(image_paths, key=lambda p: _natural_key(p.name)): try: panels = _extract_panels_for_image(image_path, settings) except Exception as exc: issues.append(f"{chapter_rel_path.as_posix()}/{image_path.name} (error: {exc})") continue if not panels: issues.append(f"{chapter_rel_path.as_posix()}/{image_path.name} (no panels)") continue for panel in panels: out_path = chapter_dir / f"panel_{panel_index:04d}.png" cv2.imwrite(str(out_path), cv2.cvtColor(panel, cv2.COLOR_RGB2BGR)) saved_paths.append(out_path) panel_index += 1 return saved_paths, issues def _extract_zip_groups(zip_path: Path, extract_root: Path): groups = {} with zipfile.ZipFile(zip_path) as zf: for info in sorted(zf.infolist(), key=lambda item: _natural_key(item.filename)): if info.is_dir(): continue rel_name = info.filename.replace("\\", "/") rel_path = Path(*[part for part in Path(rel_name).parts if part not in {"", "."}]) if not rel_path.parts or ".." in rel_path.parts or not _is_image_path(rel_path): continue dest = extract_root / rel_path dest.parent.mkdir(parents=True, exist_ok=True) with zf.open(info) as src, open(dest, "wb") as dst: shutil.copyfileobj(src, dst) chapter_key = rel_path.parent if rel_path.parent != Path(".") else Path(zip_path.stem) groups.setdefault(chapter_key, []).append(dest) return sorted(groups.items(), key=lambda item: _natural_key(item[0].as_posix())) def _process_single(image_path: Path, settings: dict, temp_dir: Path): panels = _extract_panels_for_image(image_path, settings) if not panels: return 0, [] parent_name = image_path.parent.name out_dir = (temp_dir / parent_name / image_path.stem if parent_name.lower().startswith("chapter") else temp_dir / image_path.stem) out_dir.mkdir(parents=True, exist_ok=True) panel_paths = [] for panel_id, panel_to_save in enumerate(panels, start=1): out_path = out_dir / f"panel_{panel_id:02d}.png" cv2.imwrite(str(out_path), cv2.cvtColor(panel_to_save, cv2.COLOR_RGB2BGR)) panel_paths.append(out_path) return len(panels), panel_paths # --------------------------------------------------------------------------- # Scraper Logic — site-agnostic # --------------------------------------------------------------------------- def _chap_num(url: str) -> float: """Extract a sortable chapter number from any URL.""" url_l = url.lower().rstrip('/') # 1. Look for explicit chapter keywords m = re.search(r'(?:chapter|ch|episode|ep|scan)[-_]?(\d+(?:\.\d+)?)', url_l) if m: return float(m.group(1)) # 2. Look backwards for the last number block parts = url_l.split('/') for part in reversed(parts): nums = re.findall(r'\b(\d+(?:\.\d+)?)\b', part) if nums: return float(nums[-1]) # 3. Fallback: last contiguous string of digits digits = re.findall(r'\d+', url_l) return float(digits[-1]) if digits else 0.0 def setup_uc_driver(): options = uc.ChromeOptions() # No options.headless! We want it to be fully headed within the Virtual Display options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-gpu") options.add_argument("--window-size=1920,1080") options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") # In Hugging Face Spaces, chromium is usually installed via packages.txt # and ends up in /usr/bin/chromium or similar. possible_bins = ["/usr/bin/chromium", "/usr/bin/google-chrome", "/usr/bin/chromium-browser"] for bin_path in possible_bins: if os.path.exists(bin_path): options.binary_location = bin_path break kwargs = {"options": options} # Also specify the chromedriver path if it exists possible_drivers = ["/usr/bin/chromedriver", "/usr/local/bin/chromedriver"] for driver_path in possible_drivers: if os.path.exists(driver_path): kwargs["driver_executable_path"] = driver_path break # Force version 146 as fallback if auto-download triggers # since Debian currently has chromium 146. kwargs["version_main"] = 146 return uc.Chrome(**kwargs) def _scroll_to_bottom(driver, pause=1.5, max_scrolls=15): last = driver.execute_script("return document.body.scrollHeight") for _ in range(max_scrolls): driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(pause) new = driver.execute_script("return document.body.scrollHeight") if new == last: break last = new def get_chapter_links(driver, series_url: str) -> list: driver.get(series_url) time.sleep(6) # Let Cloudflare clear _scroll_to_bottom(driver) chapter_links = [] for link in driver.find_elements(By.TAG_NAME, "a"): href = link.get_attribute("href") or "" href_l = href.lower() if not href or "#" in href or "login" in href_l or "register" in href_l: continue href_clean = href_l.rstrip('/') is_chapter = False if re.search(r'[-/](chapter|ch|episode|ep|scan)[-_]?\d+', href_clean): is_chapter = True elif re.search(r'/[a-z0-9-]+-chapter-\d+', href_clean): is_chapter = True elif re.search(r'/\d+(?:\.\d+)?$', href_clean): is_chapter = True if is_chapter: chapter_links.append(href) return sorted(set(chapter_links), key=_chap_num, reverse=True) def parse_images_from_html(html_source: str, output_dir: str) -> list: soup = BeautifulSoup(html_source, 'html.parser') badKeywords = ['logo', 'avatar', 'icon', 'banner', 'button', 'ads', 'sprite', 'thumbnail', 'discord'] img_urls = [] for img in soup.find_all('img'): src = "" for name, val in img.attrs.items(): name_l = name.lower() if isinstance(val, list): val = val[0] val_str = str(val).strip() if ('src' in name_l or 'url' in name_l) and val_str.startswith('http'): if name_l.startswith('data-'): src = val_str break if not src: src = val_str if src: src_l = src.lower() if len(src_l) > 5 and not src_l.startswith("data:") and not any(bad in src_l for bad in badKeywords): if src not in img_urls: img_urls.append(src) os.makedirs(output_dir, exist_ok=True) paths = [] headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'} for i, url in enumerate(img_urls): try: r = requests.get(url, headers=headers, stream=True, timeout=10) if r.status_code == 200: ext = url.split("?")[0].split(".")[-1] if len(ext) > 5 or not ext: ext = "jpg" p = Path(output_dir) / f"{i:03d}.{ext}" with open(p, 'wb') as f: for chunk in r.iter_content(1024): f.write(chunk) paths.append(p) except Exception: pass return paths # --------------------------------------------------------------------------- # API Hub Integrations (MangaDex & Comick) # --------------------------------------------------------------------------- def search_manga_api(source, query): if not query.strip(): return [] results = [] if source == "MangaDex": url = f"https://api.mangadex.org/manga?title={urllib.parse.quote(query)}&limit=15&includes[]=cover_art" try: r = requests.get(url, timeout=10).json() for manga in r.get("data", []): title = manga["attributes"]["title"].get("en", "Unknown Title") m_id = manga["id"] results.append((title, m_id)) except: pass elif source == "Comick": url = f"https://api.comick.fun/v1.0/search?q={urllib.parse.quote(query)}&limit=15" try: r = requests.get(url, timeout=10).json() for manga in r: title = manga.get("title", "Unknown") hid = manga.get("hid") if hid: results.append((title, hid)) except: pass return results def get_chapters_api(source, manga_id): if not manga_id: return [] chapters = [] if source == "MangaDex": url = f"https://api.mangadex.org/manga/{manga_id}/feed?limit=500&translatedLanguage[]=en&order[chapter]=desc" try: r = requests.get(url, timeout=10).json() for ch in r.get("data", []): num = ch["attributes"].get("chapter") tit = ch["attributes"].get("title") cid = ch["id"] label = f"Chapter {num}" if num else "Oneshot" if tit: label += f" - {tit}" chapters.append((label, cid)) except: pass elif source == "Comick": url = f"https://api.comick.fun/comic/{manga_id}/chapters?lang=en&limit=500" try: r = requests.get(url, timeout=10).json() for ch in r.get("chapters", []): num = ch.get("chap") tit = ch.get("title") cid = ch.get("hid") label = f"Chapter {num}" if num else "Oneshot" if tit: label += f" - {tit}" chapters.append((label, cid)) except: pass return chapters def download_api_chapter(source, chapter_id, output_dir): os.makedirs(output_dir, exist_ok=True) paths = [] if source == "MangaDex": url = f"https://api.mangadex.org/at-home/server/{chapter_id}" try: r = requests.get(url, timeout=10).json() base = r.get("baseUrl") h = r.get("chapter", {}).get("hash") data = r.get("chapter", {}).get("data", []) for i, filename in enumerate(data): img_url = f"{base}/data/{h}/{filename}" p = Path(output_dir) / f"{i:03d}.jpg" img_r = requests.get(img_url, stream=True, timeout=10) with open(p, 'wb') as f: for chunk in img_r.iter_content(1024): f.write(chunk) paths.append(p) except: pass elif source == "Comick": url = f"https://api.comick.fun/chapter/{chapter_id}" try: r = requests.get(url, timeout=10).json() images = r.get("chapter", {}).get("images", []) for i, img in enumerate(images): img_url = img.get("url") if img_url: p = Path(output_dir) / f"{i:03d}.jpg" img_r = requests.get(img_url, stream=True, timeout=10) with open(p, 'wb') as f: for chunk in img_r.iter_content(1024): f.write(chunk) paths.append(p) except: pass return paths def hub_search(query, source): res = search_manga_api(source, query) choices = [f"{t} ({i})" for t, i in res] return gr.update(choices=choices, value=choices[0] if choices else None), res def hub_get_chapters(selected_str, res_state, source): if not selected_str: return gr.update(choices=[]), [] manga_id = None for t, i in res_state: if f"{t} ({i})" == selected_str: manga_id = i break chaps = get_chapters_api(source, manga_id) choices = [f"{t} [{i}]" for t, i in chaps] return gr.update(choices=choices, value=choices[0] if choices else None), chaps def hub_download_chapter(selected_chap_str, chaps_state, source): if not selected_chap_str: raise gr.Error("Select a chapter first") chap_id = None for t, i in chaps_state: if f"{t} [{i}]" == selected_chap_str: chap_id = i break td = Path(tempfile.mkdtemp()) / "hub_chapter" td.mkdir(parents=True, exist_ok=True) imgs = download_api_chapter(source, chap_id, str(td)) if not imgs: raise gr.Error("Failed to fetch chapter images from API.") pil_images = [Image.open(p) for p in imgs] choices = [p.name for p in imgs] state_paths = {p.name: str(p) for p in imgs} return pil_images, gr.update(choices=choices, value=choices, visible=True), state_paths, f"✅ Fetched {len(imgs)} pages via {source} API." def download_chapter_images(driver, chapter_url: str, output_dir: str) -> list: driver.get(chapter_url) time.sleep(14) _scroll_to_bottom(driver) js_images = driver.execute_script(""" const imgs = Array.from(document.querySelectorAll('img')); const badKeywords = ['logo', 'avatar', 'icon', 'banner', 'button', 'ads', 'sprite', 'thumbnail', 'discord']; return imgs.map(img => { let src = img.src || ""; for (let attr of img.attributes) { let name = attr.name.toLowerCase(); let val = attr.value; if ((name.includes('src') || name.includes('url')) && val.startsWith('http')) { if (name.startsWith('data-')) return val; src = val; } } return src; }).filter(s => { if (!s) return false; let s_lower = s.toLowerCase(); if (s_lower.length < 5 || s_lower.startsWith("data:")) return false; for (let bad of badKeywords) { if (s_lower.includes(bad)) return false; } return true; }); """) img_urls = [] for url in (js_images or []): if url not in img_urls: img_urls.append(url) os.makedirs(output_dir, exist_ok=True) paths = [] headers = {"Referer": chapter_url, "User-Agent": "Mozilla/5.0"} driver.set_script_timeout(15) for i, url in enumerate(img_urls): out = os.path.join(output_dir, f"{i+1:03d}.jpg") # 1. Try fetching via the authenticated browser context to bypass Cloudflare try: script = """ var url = arguments[0]; var done = arguments[arguments.length - 1]; fetch(url) .then(response => response.blob()) .then(blob => { var reader = new FileReader(); reader.onloadend = function() { done(reader.result); } reader.readAsDataURL(blob); }) .catch(err => done(null)); """ b64_data = driver.execute_async_script(script, url) if b64_data and "," in b64_data: img_data = base64.b64decode(b64_data.split(',')[1]) # Check dimensions to filter out thumbnails/icons try: img_pil = Image.open(BytesIO(img_data)) if img_pil.width < 400 and img_pil.height < 400: continue except Exception: pass with open(out, "wb") as f: f.write(img_data) paths.append(out) continue except Exception: pass # 2. Fallback to requests try: r = requests.get(url, timeout=15, headers=headers) r.raise_for_status() img_data = r.content # Check dimensions to filter out thumbnails/icons try: img_pil = Image.open(BytesIO(img_data)) if img_pil.width < 400 and img_pil.height < 400: continue except Exception: pass with open(out, "wb") as f: f.write(img_data) paths.append(out) except Exception: pass return [Path(p) for p in paths] # --------------------------------------------------------------------------- # Processor Wrappers # --------------------------------------------------------------------------- def process_manhwa_image(input_image, min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels): if input_image is None: return None, "Please upload an image first", [] try: img_array = np.array(input_image) settings = _build_settings( min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels ) with tempfile.TemporaryDirectory() as tmp: td = Path(tmp) inp = td / "input.png" cv2.imwrite(str(inp), cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)) n, paths = _process_single(inp, settings, td) if n == 0: return None, "No panels detected.", [] imgs = [Image.open(p) for p in paths] pzip = Path(tempfile.gettempdir()) / f"manhwa_{id(input_image)}.zip" with zipfile.ZipFile(pzip, "w") as zf: for p in paths: zf.write(p, p.name) return str(pzip), f"✅ Extracted {n} panel(s)" + (" (refined)" if refine_panels else ""), imgs except Exception as e: return None, f"❌ Error: {e}", [] def batch_process_images(files, min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels): if not files: return None, "⚠️ Please upload at least one image." settings = _build_settings( min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels ) file_paths = sorted([Path(f if isinstance(f, str) else getattr(f, "name", str(f))) for f in files], key=lambda p: p.stem.lower()) zip_name = _common_prefix_name([p.stem for p in file_paths]) tot_panels, tot_imgs, failed = 0, 0, [] with tempfile.TemporaryDirectory() as tmp: td = Path(tmp) for img_path in file_paths: try: n, _ = _process_single(img_path, settings, td) tot_panels += n tot_imgs += 1 if n == 0: failed.append(f"{img_path.name} (no panels)") except Exception as e: failed.append(f"{img_path.name} (error: {e})") pzip = Path(tempfile.gettempdir()) / f"{zip_name}.zip" with zipfile.ZipFile(pzip, "w", compression=zipfile.ZIP_DEFLATED) as zf: for folder in td.iterdir(): if folder.is_dir(): for pf in sorted(folder.iterdir()): zf.write(pf, f"{folder.name}/{pf.name}") msgs = [f"✅ Processed **{tot_imgs}** images", f"📦 Extracted **{tot_panels}** panels"] if failed: msgs.append("⚠️ Issues:\n" + "\n".join(f" • {f}" for f in failed)) return str(pzip), "\n".join(msgs) def process_chapter_batch(folder_files, folder_name, zip_file, min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels): if not folder_files and not zip_file: return None, "⚠️ Upload a chapter folder or a ZIP file first." settings = _build_settings( min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels ) with tempfile.TemporaryDirectory() as tmp: td = Path(tmp) output_root = td / "processed" output_root.mkdir(parents=True, exist_ok=True) chapter_groups = [] if folder_files: file_paths = [ path for path in (_resolve_gradio_path(f) for f in folder_files) if path is not None and _is_image_path(path) ] if file_paths: chapter_name = _sanitize_folder_name( folder_name, _common_prefix_name([path.stem for path in file_paths]) or "uploaded_chapter", ) chapter_groups.append((Path(chapter_name), sorted(file_paths, key=lambda p: _natural_key(p.name)))) if zip_file: zip_path = _resolve_gradio_path(zip_file) if zip_path is not None: chapter_groups.extend(_extract_zip_groups(zip_path, td / "unzipped")) if not chapter_groups: return None, "⚠️ No valid chapter images were found in the uploaded folder/ZIP." total_pages = 0 total_panels = 0 failed = [] processed_chapters = [] for chapter_rel_path, image_paths in sorted(chapter_groups, key=lambda item: _natural_key(item[0].as_posix())): if not image_paths: failed.append(f"{chapter_rel_path.as_posix()} (empty chapter)") continue saved_paths, issues = _process_chapter_sequence(image_paths, chapter_rel_path, settings, output_root) total_pages += len(image_paths) total_panels += len(saved_paths) processed_chapters.append(chapter_rel_path.as_posix()) failed.extend(issues) if not list(output_root.rglob("*.png")): return None, "⚠️ No panels were extracted. Try lowering sensitivity or checking the source images." zip_sources = [] if zip_file: zip_path = _resolve_gradio_path(zip_file) if zip_path is not None: zip_sources.append(zip_path.stem) if folder_files: zip_sources.append(_sanitize_folder_name(folder_name, "folder_upload")) zip_base = _common_prefix_name(zip_sources or ["processed_chapters"]) + "_processed" pzip = _zip_directory_to_file(output_root, zip_base) msgs = [ f"✅ Processed **{len(processed_chapters)}** chapter folder(s)", f"📄 Read **{total_pages}** page(s)", f"📦 Extracted **{total_panels}** panel(s)", "📁 Output keeps the chapter folder layout and saves panels directly inside each chapter folder.", ] if failed: msgs.append("⚠️ Issues:\n" + "\n".join(f" • {issue}" for issue in failed)) return str(pzip), "\n".join(msgs) def process_series_flow(series_url, start_chap, end_chap, do_extract, min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels, progress=gr.Progress(track_tqdm=True)): if not SCRAPER_AVAILABLE: raise gr.Error("Scraper dependencies not installed (undetected-chromedriver).") if not series_url.strip(): raise gr.Error("Please provide a Comic Series URL.") if start_chap > end_chap: raise gr.Error("Start chapter must be ≤ End chapter.") settings = _build_settings( min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels ) progress(0, desc="Booting browser (Cloudflare bypass)…") disp = None driver = None try: disp = Display(visible=0, size=(1920, 1080)) disp.start() driver = setup_uc_driver() except Exception as e: if disp: disp.stop() raise gr.Error(f"Failed to start browser: {e}") try: progress(0.05, desc="Fetching chapter list…") links = get_chapter_links(driver, series_url) if not links: raise gr.Error("No chapter links found — check the URL and try again.") filtered = [l for l in links if start_chap <= _chap_num(l) <= end_chap] if not filtered: raise gr.Error(f"No chapters found between {int(start_chap)} and {int(end_chap)}.") tot_panels, tot_imgs, failed = 0, 0, [] label = "extracted_panels" if do_extract else "raw_chapters" pzip = Path(tempfile.gettempdir()) / f"manga_{label}.zip" with tempfile.TemporaryDirectory() as tmp: td = Path(tmp) raw_root = td / "raw" out_root = td / "output" raw_root.mkdir(parents=True, exist_ok=True) out_root.mkdir(parents=True, exist_ok=True) for idx, chap_url in enumerate(reversed(filtered)): chap_name = f"Chapter-{_chap_num(chap_url):g}" progress(0.1 + 0.7 * (idx / len(filtered)), desc=f"{'Extracting' if do_extract else 'Downloading'} {chap_name}…") raw_dir = raw_root / chap_name imgs = download_chapter_images(driver, chap_url, str(raw_dir)) if not imgs: failed.append(f"{chap_name}: nothing downloaded") continue tot_imgs += len(imgs) if not do_extract: continue saved_paths, issues = _process_chapter_sequence(imgs, Path(chap_name), settings, out_root) tot_panels += len(saved_paths) failed.extend(issues) if do_extract: pzip = _zip_directory_to_file(out_root, f"manga_{label}") else: pzip = _zip_directory_to_file(raw_root, f"manga_{label}") msgs = [f"✅ {len(filtered)} chapters downloaded."] if do_extract: msgs.append(f"✅ {tot_imgs} pages → {tot_panels} panels extracted.") else: msgs.append(f"✅ {tot_imgs} raw pages saved.") if failed: msgs.append("⚠️ Issues:\n" + "\n".join(f" • {f}" for f in failed)) return str(pzip), "\n".join(msgs) except gr.Error: raise except Exception as e: raise gr.Error(str(e)) finally: if driver is not None: driver.quit() if disp: disp.stop() def fetch_chapter_images_only(chapter_url, html_source=""): if not str(chapter_url).strip() and not str(html_source).strip(): raise gr.Error("Please provide a Chapter URL or HTML Source.") td = Path(tempfile.mkdtemp()) / "single_chapter" td.mkdir(parents=True, exist_ok=True) try: if str(html_source).strip(): imgs = parse_images_from_html(html_source, str(td)) else: if not SCRAPER_AVAILABLE: raise gr.Error("Browser scraping is disabled in this Space build. Paste HTML source instead, or use the API Hub tab.") disp = None try: disp = Display(visible=0, size=(1920, 1080)) disp.start() driver = setup_uc_driver() except Exception as e: if disp: disp.stop() raise gr.Error(f"Failed to start browser: {e}") imgs = download_chapter_images(driver, chapter_url, str(td)) driver.quit() if disp: disp.stop() if not imgs: raise gr.Error("No images found on this page. Check the URL or HTML contents.") pil_images = [Image.open(p) for p in imgs] choices = [p.name for p in imgs] state_paths = {p.name: str(p) for p in imgs} return pil_images, gr.update(choices=choices, value=choices, visible=True), state_paths, f"✅ Fetched {len(imgs)} pages successfully." except gr.Error: raise except Exception as e: raise gr.Error(str(e)) def _create_zip_from_selected(state_paths, selected_names, zip_name): paths = [Path(state_paths[name]) for name in selected_names if name in state_paths] pzip = Path(tempfile.gettempdir()) / f"{zip_name}.zip" with zipfile.ZipFile(pzip, "w", compression=zipfile.ZIP_DEFLATED) as zf: for p in paths: zf.write(p, p.name) return str(pzip) def download_selected_single(state_paths, selected_names): if not selected_names: return None, "⚠️ No images selected." try: pzip = _create_zip_from_selected(state_paths, selected_names, "manga_chapter_raw") return str(pzip), f"✅ Prepared zip with {len(selected_names)} raw images." except Exception as e: return None, f"❌ Error: {e}" def extract_selected_single(state_paths, selected_names, min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels): if not selected_names: return None, "⚠️ No images selected." settings = _build_settings( min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels ) paths = [Path(state_paths[name]) for name in selected_names if name in state_paths] with tempfile.TemporaryDirectory() as tmp: td = Path(tmp) output_root = td / "chapter_panels" saved_paths, issues = _process_chapter_sequence(paths, Path("selected_chapter"), settings, output_root) pzip = _zip_directory_to_file(output_root, "manga_chapter_panels") msgs = [ f"✅ Processed **{len(paths)}** images", f"📦 Extracted **{len(saved_paths)}** panels", "📁 Panels are saved directly inside the chapter folder with continuous numbering.", ] if issues: msgs.append("⚠️ Issues:\n" + "\n".join(f" • {issue}" for issue in issues)) return str(pzip), "\n".join(msgs) def download_only_series(url, start, end, ma, es, lt, ht, ro, rp, progress=gr.Progress(track_tqdm=True)): return process_series_flow(url, start, end, False, ma, es, lt, ht, ro, rp, progress) def download_and_extract_series(url, start, end, ma, es, lt, ht, ro, rp, progress=gr.Progress(track_tqdm=True)): return process_series_flow(url, start, end, True, ma, es, lt, ht, ro, rp, progress) def load_sample(): url = "https://raw.githubusercontent.com/kushalchoksi/manhwa-panel-extractor/refs/heads/main/jinwoo.jpg" return Image.open(BytesIO(requests.get(url).content)) # --------------------------------------------------------------------------- # UI # --------------------------------------------------------------------------- def _settings_block(): with gr.Accordion("⚙️ Advanced Settings", open=False): with gr.Row(): min_panel_area = gr.Slider(0.1, 10.0, value=1.0, step=0.1, label="Min Panel Area (%)") edge_sigma = gr.Slider(0.1, 3.0, value=1.0, step=0.1, label="Edge Sensitivity") with gr.Row(): low_threshold = gr.Slider(0.01, 0.5, value=0.1, step=0.01, label="Low Threshold") high_threshold = gr.Slider(0.05, 0.8, value=0.2, step=0.01, label="High Threshold") with gr.Row(): reading_order = gr.Radio(["ltr", "rtl"], value="ltr", label="Reading Order") refine_panels = gr.Checkbox(value=False, label="Refine Panels (Trim Bubbles)") return min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels def create_demo(): CSS = """ .gradio-container { font-family: 'Segoe UI', Tahoma, sans-serif; } .hdr { text-align:center; background:linear-gradient(135deg,#667eea,#764ba2); color:#fff; padding:1.8rem; border-radius:10px; margin-bottom:1.2rem; } """ with gr.Blocks(css=CSS, title="Manhwa Panel Extractor") as demo: gr.HTML("""
Extract panels from any manhwa/manga page — upload images or scrape directly from the web.