- Updated `config.json` to introduce new settings for infinite loop mode and adjust various thresholds for 福袋 detection. - Refined the pixel detection logic in `douyin_detection.py` to improve error handling and retry mechanisms. - Enhanced reservation handling in `douyin_flow_control.py` to prioritize high-value 福袋 and streamline the switching process. - Improved the GUI in `gui_app.py` to support new configuration options and ensure better user experience. - Added new tools for analyzing 福袋 colors and comparing them with coupon colors to aid in detection accuracy. - Updated documentation to reflect changes in configuration and usage instructions.
141 lines
5.3 KiB
Python
141 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
福袋图标RGB颜色范围检测工具
|
||
自动排除黑色背景,只检测福袋图标本身的RGB范围
|
||
"""
|
||
|
||
import sys
|
||
from PIL import Image
|
||
from collections import Counter
|
||
|
||
|
||
def get_fudai_rgb_range(image_path: str, exclude_bg: bool = True, bg_threshold: int = 30):
|
||
"""检测福袋图标的RGB颜色范围(排除背景)
|
||
|
||
参数:
|
||
image_path: 图片路径
|
||
exclude_bg: 是否排除背景色(默认True,排除RGB值都小于30的像素)
|
||
bg_threshold: 背景色阈值(RGB值都小于此值认为是背景)
|
||
"""
|
||
try:
|
||
img = Image.open(image_path)
|
||
img_rgb = img.convert("RGB")
|
||
width, height = img_rgb.size
|
||
|
||
print(f"图片尺寸: {width} x {height}")
|
||
print(f"图片模式: {img.mode}")
|
||
print("-" * 60)
|
||
|
||
# 收集所有非背景像素的RGB值
|
||
pixels = []
|
||
for y in range(height):
|
||
for x in range(width):
|
||
try:
|
||
r, g, b = img_rgb.getpixel((x, y))
|
||
# 如果启用背景排除,跳过黑色/深色背景
|
||
if exclude_bg and r < bg_threshold and g < bg_threshold and b < bg_threshold:
|
||
continue
|
||
pixels.append((r, g, b))
|
||
except Exception:
|
||
continue
|
||
|
||
if not pixels:
|
||
print("错误: 没有检测到有效像素(可能整张图都是背景色)")
|
||
return None
|
||
|
||
# 计算RGB范围
|
||
r_values = [p[0] for p in pixels]
|
||
g_values = [p[1] for p in pixels]
|
||
b_values = [p[2] for p in pixels]
|
||
|
||
r_min, r_max = min(r_values), max(r_values)
|
||
g_min, g_max = min(g_values), max(g_values)
|
||
b_min, b_max = min(b_values), max(b_values)
|
||
|
||
# 计算RGB平均值(用于参考)
|
||
r_avg = sum(r_values) // len(r_values)
|
||
g_avg = sum(g_values) // len(g_values)
|
||
b_avg = sum(b_values) // len(b_values)
|
||
|
||
print(f"\n福袋图标RGB颜色范围(已排除背景):")
|
||
print(f" R: {r_min} ~ {r_max} (平均值: {r_avg})")
|
||
print(f" G: {g_min} ~ {g_max} (平均值: {g_avg})")
|
||
print(f" B: {b_min} ~ {b_max} (平均值: {b_avg})")
|
||
print(f"\n有效像素数: {len(pixels)} / {width * height}")
|
||
|
||
# 分析主要颜色(找出最常见的RGB组合)
|
||
print(f"\n主要颜色分析:")
|
||
color_counter = Counter(pixels)
|
||
top_colors = color_counter.most_common(5)
|
||
for i, (color, count) in enumerate(top_colors, 1):
|
||
r, g, b = color
|
||
percentage = (count / len(pixels)) * 100
|
||
print(f" {i}. RGB({r}, {g}, {b}) - 占比 {percentage:.1f}% - #{r:02X}{g:02X}{b:02X}")
|
||
|
||
# 生成代码建议
|
||
print(f"\n" + "=" * 60)
|
||
print("用于代码的颜色范围判断(建议):")
|
||
print("=" * 60)
|
||
|
||
# 方法1:使用完整范围(容错性最好,但可能误检)
|
||
print(f"\n方法1:完整范围(容错性最好)")
|
||
print(f" {r_min} <= r <= {r_max} and {g_min} <= g <= {g_max} and {b_min} <= b <= {b_max}")
|
||
|
||
# 方法2:使用平均值±范围(更精确,但需要根据实际情况调整)
|
||
r_range = max(10, (r_max - r_min) // 2)
|
||
g_range = max(10, (g_max - g_min) // 2)
|
||
b_range = max(10, (b_max - b_min) // 2)
|
||
|
||
print(f"\n方法2:平均值±范围(更精确)")
|
||
print(f" {r_avg - r_range} <= r <= {r_avg + r_range} and "
|
||
f"{g_avg - g_range} <= g <= {g_avg + g_range} and "
|
||
f"{b_avg - b_range} <= b <= {b_avg + b_range}")
|
||
|
||
# 方法3:基于主要颜色的范围(最精确,但可能漏检)
|
||
if top_colors:
|
||
main_r, main_g, main_b = top_colors[0][0]
|
||
tolerance = 20 # 容差
|
||
print(f"\n方法3:主要颜色±容差(最精确,基于主要颜色RGB({main_r}, {main_g}, {main_b}))")
|
||
print(f" {main_r - tolerance} <= r <= {main_r + tolerance} and "
|
||
f"{main_g - tolerance} <= g <= {main_g + tolerance} and "
|
||
f"{main_b - tolerance} <= b <= {main_b + tolerance}")
|
||
|
||
# 当前代码中的范围(用于对比)
|
||
print(f"\n当前代码中的范围(用于对比):")
|
||
print(f" 190 <= r <= 206 and 173 <= g <= 196 and 234 <= b <= 250")
|
||
|
||
return {
|
||
"r_range": (r_min, r_max),
|
||
"g_range": (g_min, g_max),
|
||
"b_range": (b_min, b_max),
|
||
"r_avg": r_avg,
|
||
"g_avg": g_avg,
|
||
"b_avg": b_avg,
|
||
"pixel_count": len(pixels)
|
||
}
|
||
|
||
except FileNotFoundError:
|
||
print(f"错误: 找不到图片文件: {image_path}")
|
||
return None
|
||
except Exception as e:
|
||
print(f"错误: {type(e).__name__}: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return None
|
||
|
||
|
||
def main():
|
||
if len(sys.argv) < 2:
|
||
print("用法: python tools/read_fudai_rgb.py <图片路径>")
|
||
print("示例: python tools/read_fudai_rgb.py pic/fudai_icon.png")
|
||
sys.exit(1)
|
||
|
||
image_path = sys.argv[1]
|
||
get_fudai_rgb_range(image_path)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
|