Files
douyin_guaji-main/tools/read_image_rgb.py
T
Your Name 8b47b506d6 feat: Enhance 福袋 detection and reservation logic
- 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.
2025-12-20 13:32:32 +08:00

176 lines
5.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
图片RGB颜色范围读取工具
用法:
python tools/read_image_rgb.py <图片路径> [x] [y] [width] [height]
示例:
# 读取整张图片的RGB范围
python tools/read_image_rgb.py pic/screenshot.png
# 读取特定坐标的RGB值
python tools/read_image_rgb.py pic/screenshot.png 100 200
# 读取特定区域的RGB范围
python tools/read_image_rgb.py pic/screenshot.png 0 0 200 300
"""
import sys
import os
from PIL import Image
def get_rgb_range(image_path: str, x: int = None, y: int = None, width: int = None, height: int = None):
"""读取图片的RGB颜色范围
参数:
image_path: 图片路径
x: 起始X坐标(可选,用于区域检测)
y: 起始Y坐标(可选,用于区域检测)
width: 区域宽度(可选,用于区域检测)
height: 区域高度(可选,用于区域检测)
返回:
dict: 包含RGB范围信息的字典
"""
try:
img = Image.open(image_path)
img_rgb = img.convert("RGB")
img_width, img_height = img_rgb.size
print(f"图片尺寸: {img_width} x {img_height}")
print(f"图片模式: {img.mode}")
print("-" * 60)
# 如果指定了坐标,读取单个像素
if x is not None and y is not None:
if not (0 <= x < img_width and 0 <= y < img_height):
print(f"错误: 坐标 ({x}, {y}) 超出图片范围")
return None
r, g, b = img_rgb.getpixel((x, y))
print(f"坐标 ({x}, {y}) 的RGB值:")
print(f" R: {r}")
print(f" G: {g}")
print(f" B: {b}")
print(f" 十六进制: #{r:02X}{g:02X}{b:02X}")
return {"r": r, "g": g, "b": b, "x": x, "y": y}
# 如果指定了区域,读取区域范围
if x is not None and y is not None and width is not None and height is not None:
x_end = min(x + width, img_width)
y_end = min(y + height, img_height)
print(f"区域: ({x}, {y}) 到 ({x_end-1}, {y_end-1})")
print(f"区域大小: {x_end - x} x {y_end - y}")
pixels = []
for py in range(y, y_end):
for px in range(x, x_end):
try:
r, g, b = img_rgb.getpixel((px, py))
pixels.append((r, g, b))
except Exception:
continue
if not pixels:
print("错误: 区域中没有有效像素")
return None
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)
print(f"\nRGB颜色范围:")
print(f" R: {r_min} ~ {r_max}")
print(f" G: {g_min} ~ {g_max}")
print(f" B: {b_min} ~ {b_max}")
print(f"\n用于代码的颜色范围判断:")
print(f" {r_min} <= r <= {r_max} and {g_min} <= g <= {g_max} and {b_min} <= b <= {b_max}")
print(f"\n像素总数: {len(pixels)}")
return {
"r_range": (r_min, r_max),
"g_range": (g_min, g_max),
"b_range": (b_min, b_max),
"pixel_count": len(pixels)
}
# 读取整张图片的RGB范围
print("扫描整张图片...")
pixels = []
for py in range(img_height):
for px in range(img_width):
try:
r, g, b = img_rgb.getpixel((px, py))
pixels.append((r, g, b))
except Exception:
continue
if not pixels:
print("错误: 图片中没有有效像素")
return None
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)
print(f"\n整张图片的RGB颜色范围:")
print(f" R: {r_min} ~ {r_max}")
print(f" G: {g_min} ~ {g_max}")
print(f" B: {b_min} ~ {b_max}")
print(f"\n像素总数: {len(pixels)}")
return {
"r_range": (r_min, r_max),
"g_range": (g_min, g_max),
"b_range": (b_min, b_max),
"pixel_count": len(pixels)
}
except FileNotFoundError:
print(f"错误: 找不到图片文件: {image_path}")
return None
except Exception as e:
print(f"错误: {type(e).__name__}: {e}")
return None
def main():
if len(sys.argv) < 2:
print(__doc__)
sys.exit(1)
image_path = sys.argv[1]
# 解析参数
x = int(sys.argv[2]) if len(sys.argv) > 2 else None
y = int(sys.argv[3]) if len(sys.argv) > 3 else None
width = int(sys.argv[4]) if len(sys.argv) > 4 else None
height = int(sys.argv[5]) if len(sys.argv) > 5 else None
# 如果只提供了x和y,就是读取单个像素
if x is not None and y is not None and width is None and height is None:
get_rgb_range(image_path, x=x, y=y)
# 如果提供了x, y, width, height,就是读取区域
elif x is not None and y is not None and width is not None and height is not None:
get_rgb_range(image_path, x=x, y=y, width=width, height=height)
# 否则读取整张图片
else:
get_rgb_range(image_path)
if __name__ == "__main__":
main()