From 378350ac995a23659f943115f6b852258c997f59 Mon Sep 17 00:00:00 2001 From: Zhang Yanpo Date: Sun, 22 Feb 2026 20:21:53 +0800 Subject: [PATCH] refactor: extract _trim_and_convert from render_to_img Move image trimming and format conversion into a dedicated helper, reducing render_to_img to ~30 lines focused on orchestration. Co-Authored-By: Claude Opus 4.6 --- down2.py | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/down2.py b/down2.py index a8822d6..06ca75a 100755 --- a/down2.py +++ b/down2.py @@ -321,6 +321,31 @@ def web_to_img(pagefn: str, typ: str) -> bytes: return render_to_img(intyp, page, typ) +def _trim_and_convert(png_data: bytes, typ: str) -> bytes: + """Trim whitespace borders from a screenshot and convert to the target format.""" + img = Image.open(io.BytesIO(png_data)) + + # Trim borders matching the corner pixel color (like ImageMagick -trim). + bg = Image.new(img.mode, img.size, img.getpixel((0, 0))) + diff = ImageChops.difference(img, bg) + bbox = diff.getbbox() + if bbox: + img = img.crop(bbox) + + buf = io.BytesIO() + if typ == "png": + img.save(buf, format="PNG") + else: + background = Image.new("RGB", img.size, (255, 255, 255)) + if img.mode == "RGBA": + background.paste(img, mask=img.split()[3]) + else: + background.paste(img) + background.save(buf, format="JPEG") + + return buf.getvalue() + + def render_to_img( mime: str, content: str | bytes, typ: str, width: int = 1000, height: int = 2000, asset_base: str | None = None ) -> bytes: @@ -378,28 +403,7 @@ def render_to_img( png_data = page.screenshot(omit_background=True) page.close() - img = Image.open(io.BytesIO(png_data)) - - # Trim borders matching the corner pixel color (like ImageMagick -trim). - # getbbox() alone fails for RGB images where the background is opaque white. - bg = Image.new(img.mode, img.size, img.getpixel((0, 0))) - diff = ImageChops.difference(img, bg) - bbox = diff.getbbox() - if bbox: - img = img.crop(bbox) - - buf = io.BytesIO() - if typ == "png": - img.save(buf, format="PNG") - else: - background = Image.new("RGB", img.size, (255, 255, 255)) - if img.mode == "RGBA": - background.paste(img, mask=img.split()[3]) - else: - background.paste(img) - background.save(buf, format="JPEG") - - return buf.getvalue() + return _trim_and_convert(png_data, typ) html_style = """