完整 teacher 计划与代码
{
"visual_evidence": [
"A person stands on a tennis court holding a racket in the hand on the right side of the image.",
"The racket's dark handle angles downward and right from the hand.",
"The oval racket head and strings lie near the right image edge against the gray court.",
"The scene has daylight illumination, fine photographic texture, and a chain-link fence in the background."
],
"target_object": "The tennis racket",
"target_region": "The handle beside the person's rightmost hand and the oval racket head extending toward the lower-right image edge.",
"editing_intent": "Replace the racket with a metallic lightsaber hilt held by the same hand and a luminous blue blade pointing downward and right.",
"editing_plan": [
"Mask the racket shaft, rim, and strings without covering the person's hand.",
"Remove the racket using inpainting and nearby court texture, matching local color and retaining photographic grain.",
"Construct a shaded metallic hilt aligned with the original grip, with dark grooves, an emitter, and a small control button.",
"Add a narrow blue-white blade, softly feathered edges, restrained blue bloom, and subtle hilt shadow.",
"Restore the visible fingers over the hilt and preserve the person, court markings, fence, and background."
],
"preservation_constraints": [
"Keep the person's face, hair, clothing, body, and pose unchanged.",
"Preserve the hand's visible skin and its position.",
"Do not alter the fence, greenery, court markings, or unrelated background.",
"Retain the source image dimensions and photographic texture."
]
}
def edit(image):
import numpy as np
import cv2
from PIL import Image
w, h = image.size
scale = float(min(w, h))
original = np.asarray(image.convert('RGB')).copy()
rng = np.random.default_rng(37)
def point(x, y):
return (int(round(x * (w - 1))), int(round(y * (h - 1))))
head = np.zeros((h, w), np.uint8)
cv2.ellipse(head, point(0.965, 0.676),
(max(1, int(round(0.031 * w))), max(1, int(round(0.065 * h)))),
-24, 0, 360, 255, -1, cv2.LINE_AA)
cv2.line(head, point(0.917, 0.605), point(0.939, 0.637),
255, max(2, int(round(0.013 * scale))), cv2.LINE_AA)
dilation = max(1, int(round(0.006 * scale)))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
(2 * dilation + 1, 2 * dilation + 1))
head = cv2.dilate(head, kernel)
shaft = np.zeros((h, w), np.uint8)
cv2.line(shaft, point(0.879, 0.563), point(0.932, 0.628),
255, max(3, int(round(0.016 * scale))), cv2.LINE_AA)
removal = cv2.bitwise_or(head, shaft)
# Protect skin at the gripping hand before removing the equipment.
hand_roi = np.zeros((h, w), np.uint8)
hand_polygon = np.array([point(0.852, 0.520), point(0.881, 0.529),
point(0.900, 0.549), point(0.900, 0.565),
point(0.883, 0.570), point(0.862, 0.552)], np.int32)
cv2.fillPoly(hand_roi, [hand_polygon], 255)
rgb = original.astype(np.float32)
skin = ((rgb[:, :, 0] > rgb[:, :, 1] + 5) &
(rgb[:, :, 1] > rgb[:, :, 2] + 5) &
(rgb[:, :, 0] > 100) & (hand_roi > 0)).astype(np.uint8) * 255
removal[skin > 0] = 0
head[skin > 0] = 0
filled = cv2.inpaint(original, removal, max(2, int(round(0.010 * scale))),
cv2.INPAINT_TELEA).astype(np.float32)
# Transfer real asphalt grain from the clear court immediately to the left.
shift = max(1, int(round(0.095 * w)))
donor_x = np.clip(np.arange(w) - shift, 0, w - 1)
donor = rgb[:, donor_x, :].copy()
outer = cv2.dilate(head, cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, (2 * dilation + 5, 2 * dilation + 5)))
ring = (outer > 0) & (removal == 0)
if np.any(ring):
correction = np.median(rgb[ring] - donor[ring], axis=0)
donor += np.clip(correction, -12, 12)
distance_inside = cv2.distanceTransform((head > 0).astype(np.uint8),
cv2.DIST_L2, 5)
patch_alpha = np.clip(distance_inside / max(1.0, 0.007 * scale), 0, 1)
filled = filled * (1 - patch_alpha[:, :, None]) + donor * patch_alpha[:, :, None]
fine_noise = rng.normal(0, 0.65, (h, w, 1)).astype(np.float32)
filled += fine_noise * ((removal > 0)[:, :, None])
canvas = np.where((removal > 0)[:, :, None], filled, rgb).astype(np.float32)
yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)
start = np.array([0.879 * (w - 1), 0.563 * (h - 1)], np.float32)
emitter = np.array([0.910 * (w - 1), 0.614 * (h - 1)], np.float32)
axis = emitter - start
handle_length = float(np.linalg.norm(axis))
direction = axis / max(handle_length, 1e-6)
perpendicular = np.array([-direction[1], direction[0]], np.float32)
dx, dy = xx - start[0], yy - start[1]
longitudinal = dx * direction[0] + dy * direction[1]
transverse = dx * perpendicular[0] + dy * perpendicular[1]
fraction = longitudinal / max(handle_length, 1e-6)
antialias = max(0.65, 0.0015 * scale)
def blend(color, alpha):
nonlocal canvas
a = np.clip(alpha, 0, 1)[:, :, None]
c = np.asarray(color, dtype=np.float32)
canvas = canvas * (1 - a) + c * a
def capsule_distance(ax, ay, bx, by):
vx, vy = bx - ax, by - ay
t = np.clip(((xx - ax) * vx + (yy - ay) * vy) /
max(vx * vx + vy * vy, 1e-6), 0, 1)
return np.sqrt((xx - ax - t * vx) ** 2 +
(yy - ay - t * vy) ** 2), t
# Soft contact/cast shadow from the nonluminous hilt.
offset = np.array([0.006 * w, 0.008 * h], np.float32)
sd, _ = capsule_distance(*(start + offset), *(emitter + offset))
shadow = 0.09 * np.exp(-0.5 * (sd / max(1.0, 0.008 * scale)) ** 2)
shadow *= (yy > 0.565 * h)
blend([52, 57, 60], shadow)
# Daylight-visible energy blade with a bright core and localized bloom.
blade_start = emitter + direction * (0.003 * scale)
blade_end = blade_start + direction * (0.176 * scale)
blade_distance, blade_t = capsule_distance(*blade_start, *blade_end)
broad_glow = 0.19 * np.exp(-0.5 * (blade_distance / max(1.0, 0.012 * scale)) ** 2)
close_glow = 0.40 * np.exp(-0.5 * (blade_distance / max(1.0, 0.0053 * scale)) ** 2)
glow = np.clip(broad_glow + close_glow, 0, 0.68)
# Screen blending makes the halo luminous without flattening the asphalt.
glow_color = np.array([20, 145, 255], np.float32)
canvas = 255 - (255 - canvas) * (1 - glow[:, :, None] * glow_color / 255)
blade_radius = scale * (0.0041 - 0.0007 * blade_t)
edge_alpha = np.clip((blade_radius + antialias - blade_distance) /
(2 * antialias), 0, 1)
variation = rng.normal(0, 0.85, (h, w)).astype(np.float32)
blade_color = np.empty((h, w, 3), np.float32)
blade_color[:, :, 0] = 151 + 12 * (1 - blade_t) + variation
blade_color[:, :, 1] = 225 + variation
blade_color[:, :, 2] = 255
blend(blade_color, edge_alpha)
core_radius = max(0.65, 0.0019 * scale)
core_alpha = np.clip((core_radius + antialias * 0.6 - blade_distance) /
(antialias * 1.2), 0, 1)
blend([241, 252, 255], core_alpha * 0.98)
# Cylindrical metallic handle, perspective-width changes, and machined grooves.
radius = np.full((h, w), 0.0068 * scale, np.float32)
radius += (fraction > 0.80) * (0.0017 * scale)
radius += (fraction < 0.10) * (0.0006 * scale)
side_alpha = np.clip((radius + antialias - np.abs(transverse)) /
(2 * antialias), 0, 1)
end_alpha = np.minimum(np.clip((longitudinal + antialias) / (2 * antialias), 0, 1),
np.clip((handle_length - longitudinal + antialias) /
(2 * antialias), 0, 1))
handle_alpha = side_alpha * end_alpha
q = np.clip(transverse / np.maximum(radius, 1), -1, 1)
cylindrical = np.sqrt(np.maximum(0, 1 - q * q))
specular = np.exp(-((q + 0.43) / 0.19) ** 2)
metal_value = 69 + 107 * cylindrical + 65 * specular
grip_section = (fraction > 0.16) & (fraction < 0.73)
metal_value = np.where(grip_section,
36 + 49 * cylindrical + 31 * specular, metal_value)
groove = np.zeros((h, w), np.float32)
for center in [0.13, 0.23, 0.34, 0.45, 0.56, 0.67, 0.77, 0.87, 0.96]:
groove = np.maximum(groove, np.exp(-((fraction - center) / 0.018) ** 2))
metal_value *= 1 - 0.52 * groove
metal_value += rng.normal(0, 1.25, (h, w)).astype(np.float32)
handle_color = np.stack([metal_value * 0.94, metal_value * 0.98,
metal_value * 1.03], axis=2)
blend(np.clip(handle_color, 0, 255), handle_alpha)
# Small recessed activation control.
button_center = start + direction * (handle_length * 0.67) - perpendicular * (scale * 0.0015)
button_distance = np.sqrt((xx - button_center[0]) ** 2 + (yy - button_center[1]) ** 2)
button_radius = max(0.8, 0.0020 * scale)
surround = np.clip((button_radius * 1.65 + antialias - button_distance) /
(2 * antialias), 0, 1) * handle_alpha
blend([32, 37, 42], surround)
button_alpha = np.clip((button_radius + antialias * 0.5 - button_distance) /
antialias, 0, 1) * handle_alpha
blend([177, 47, 34], button_alpha)
# The emitter catches a faint blue reflection from the blade.
emitter_reflection = np.exp(-((fraction - 0.985) / 0.030) ** 2) * handle_alpha
blend([127, 210, 245], emitter_reflection * 0.63)
# Keep skin in front of the newly drawn hilt, as in the original grip.
skin_alpha = cv2.GaussianBlur(skin.astype(np.float32) / 255,
(0, 0), max(0.35, 0.0009 * scale))
skin_alpha[skin > 0] = 1
canvas = canvas * (1 - skin_alpha[:, :, None]) + rgb * skin_alpha[:, :, None]
return Image.fromarray(np.clip(np.rint(canvas), 0, 255).astype(np.uint8), 'RGB')