import os from PIL import Image, ImageDraw BASE_IMAGE = r"d:\dsWork\aiData\Output\debug_click_a0819f84-4174-4d71-ae52-71dc9631b93d.jpg" OUTPUT_DIR = r"d:\dsWork\aiData\Output\debug_offset_candidates" BEST_DY = -30 def find_red_circle_center(img): w, h = img.size pixels = img.load() xs = [] ys = [] y_start = int(h * 0.5) y_end = h x_start = 0 x_end = int(w * 0.5) for y in range(y_start, y_end): for x in range(x_start, x_end): r, g, b = pixels[x, y] if r > 230 and g < 40 and b < 40: xs.append(x) ys.append(y) if not xs: raise RuntimeError("未在图像中找到红色圆圈像素") cx = sum(xs) / len(xs) cy = sum(ys) / len(ys) return int(cx), int(cy) def generate_vertical_candidates(): if not os.path.exists(BASE_IMAGE): raise FileNotFoundError(BASE_IMAGE) os.makedirs(OUTPUT_DIR, exist_ok=True) img = Image.open(BASE_IMAGE).convert("RGB") cx, cy = find_red_circle_center(img) w, h = img.size radius = max(6, int(h * 0.012)) offsets = [-40, -30, -20, -10, -5, 0, 5, 10, 15, 20] for dy in offsets: img2 = img.copy() draw = ImageDraw.Draw(img2) y2 = max(radius, min(h - radius - 1, cy + dy)) draw.ellipse((cx - radius, y2 - radius, cx + radius, y2 + radius), outline="lime", width=3) name = f"candidate_dy_{dy}.jpg" path = os.path.join(OUTPUT_DIR, name) img2.save(path) print(path) def generate_horizontal_candidates(): if not os.path.exists(BASE_IMAGE): raise FileNotFoundError(BASE_IMAGE) os.makedirs(OUTPUT_DIR, exist_ok=True) img = Image.open(BASE_IMAGE).convert("RGB") cx, cy = find_red_circle_center(img) w, h = img.size radius = max(6, int(h * 0.012)) y2 = max(radius, min(h - radius - 1, cy + BEST_DY)) offsets = [-30, -20, -10, -5, 0, 5, 10, 20, 30] for dx in offsets: img2 = img.copy() draw = ImageDraw.Draw(img2) x2 = max(radius, min(w - radius - 1, cx + dx)) draw.ellipse((x2 - radius, y2 - radius, x2 + radius, y2 + radius), outline="cyan", width=3) name = f"candidate_dx_{dx}.jpg" path = os.path.join(OUTPUT_DIR, name) img2.save(path) print(path) if __name__ == "__main__": generate_vertical_candidates() generate_horizontal_candidates()