Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

修正透明 Logo 的方图补边

longmeidao7780d9ec44ee
diff --git a/docs/STATUS.md b/docs/STATUS.md
index 8d44b15aa..28ea158e4 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -36,6 +36,7 @@
## 本批修正与验证
+- 厂牌长条 Logo 的方图补边不再只看四角颜色:原图只要含透明像素就继续透明,完全不透明时才取整圈边缘主色。旧算法曾把贴边字样误认成底色,导致 `PREMIUM` 整张铺蓝、`HEYZO` 白字消失;17 个受影响的已安装 Logo 均有原图备份且当前文件仍与归一记录一致,可安全重建。
- 沉浸模式按用户提供的 YouTube Shorts 桌面截图重排:竖屏使用居中的 9:16 播放舞台,横屏使用在整个视口视觉居中的 16:9 大舞台;48 px 圆形操作按钮与文字分离,作者头像、身份和标题留在页面左下;390×844 手机继续使用全视口播放器。保留 Peach 的三项本地反馈、混合横竖屏队列和完整画面优先,不复制评论、分享、Remix 或订阅。
- 用户生产截图确认上一版横屏舞台贴右导致视觉失衡;同一截图还显示 HTTPS 服务 PID 47788 同时预读三部已切走的视频,子进程 PID 43080 正在转码 `IDBD-446_C.mkv`。根因是沉浸模式没有复用详情播放器已有的可取消 `session` 契约。本批为每个沉浸视频分配独立会话,并在切片、关闭、失败和页面离开时通知服务端取消。取证后 PID 43080 已自行退出;PID 47788 是正常 HTTPS 服务,不是下载器,3 秒复核读写增量均为 0,未强行终止任何进程。
- 正式 Windows 全量测试 1123 项通过、13 项按平台跳过。临时静态页面在 1440×900 下实测横屏舞台 922×518、中心偏差 0 px、无横向溢出;390×844 下舞台为完整视口且无横向溢出。浏览器控制台无错误;尚未重启生产服务。
diff --git a/src/peach/images.py b/src/peach/images.py
index 82fc44da1..0c76172d2 100644
--- a/src/peach/images.py
+++ b/src/peach/images.py
@@ -7,6 +7,7 @@
from __future__ import annotations
import io
+from collections import Counter
from PIL import Image
@@ -42,21 +43,19 @@ def classify(width: int, height: int) -> tuple[str, float, str]:
def _background_color(image: Image.Image) -> tuple[int, int, int, int]:
- """取四角出现最多的颜色当底色,让补出来的边和 Logo 自身背景连成一片。
+ """取边缘主色当不透明底色;图片带透明像素时继续保持透明。
- 纯色底的 Logo 这样补完看不出接缝;四角本身透明就保持透明,不要凭空刷白。
+ Logo 字样可能贴到四角,不能把某个角上的文字颜色误当成背景。只要原图有
+ 透明像素,补边就保持透明;完全不透明时再从整圈边缘取出现最多的颜色。
"""
width, height = image.size
- corners = [
- image.getpixel((0, 0)),
- image.getpixel((width - 1, 0)),
- image.getpixel((0, height - 1)),
- image.getpixel((width - 1, height - 1)),
- ]
- opaque = [pixel for pixel in corners if pixel[3] > 8]
- if not opaque:
+ if image.getchannel("A").getextrema()[0] < 255:
return (0, 0, 0, 0)
- return max(set(opaque), key=opaque.count)
+ border = [image.getpixel((x, 0)) for x in range(width)]
+ border.extend(image.getpixel((x, height - 1)) for x in range(width))
+ border.extend(image.getpixel((0, y)) for y in range(1, height - 1))
+ border.extend(image.getpixel((width - 1, y)) for y in range(1, height - 1))
+ return Counter(border).most_common(1)[0][0]
def pad_to_square(payload: bytes) -> bytes | None:
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index cf8980cd3..413d4f5d9 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -279,6 +279,18 @@ class OperationalScriptTests(unittest.TestCase):
self.assertEqual(squared.getpixel((5, 5)), (10, 20, 30, 255))
self.assertEqual(squared.getpixel((200, 200)), (10, 20, 30, 255))
+ # 透明 Logo 的字样可能贴到四角。角上的蓝字不是底色,补边必须继续透明;
+ # 否则 PREMIUM 会整张铺蓝,白字 Logo 也会因错误白底而消失。
+ transparent = Image.new("RGBA", (400, 100), (0, 0, 0, 0))
+ for x in range(80):
+ transparent.putpixel((x, 0), (0, 174, 239, 255))
+ buffer = io.BytesIO()
+ transparent.save(buffer, "PNG")
+ squared = Image.open(io.BytesIO(pad_to_square(buffer.getvalue())))
+ self.assertEqual(squared.getpixel((5, 5)), (0, 0, 0, 0))
+ self.assertEqual(squared.getpixel((200, 200)), (0, 0, 0, 0))
+ self.assertEqual(squared.getpixel((20, 150)), (0, 174, 239, 255))
+
def test_studio_avatar_candidates_never_guess_a_handle_by_default(self):
"""猜错 handle 会产出一个「看起来很官方」的错误 Logo,和它要取代的搜索猜测同一种失败。"""
module = load_script("fetch_studio_avatar_candidates")