Qwen Pixel-Edit:当前训练进度

2026-11-27 16:55:25 北京时间 · 完整实验看板

数据构造:严格合格进度

严格合格监督(相同内容去重)

35,395 / 20,000

独立编辑任务

13,474 / 10,000

最近一小时首次严格通过:617 条;新版流水线完成候选 921。切换后的第一个小时属于不完整观测窗口,暂不据此自动推算完成日期。

所有来源统一要求:指令遵循、区域保留、画质、完整编辑范围均至少4/5,且完整目标判定为真;程序执行、复跑、依赖与英文检查通过。历史库存另列,新严格库存尚未完成全量人工审核。

存储状态:活跃数据已迁至新盘,CPU/API 数据构造已恢复;GPU 任务保持暂停。

迁移快照:SFT素材与构造文件约 69.6 GB;约59 GB的API传输归档已删除,后续不长期保存请求与回复副本。

来源严格 RFT严格 TI合计
anyedit724205929
magicbrush7994261,225
maskedit_en2,2014,1376,338
maskedit_scene1,7022,6484,350
sciforma14,6086,02520,633
ultraedit1,2736471,920

生产线心跳和实际产出

对标Qwen-Image-Edit:fine-grained数据

已从现有官方train索引 3071 个候选任务(尚未全数通过teacher)。重点:文字/数字、局部组件属性、材质、光照与几何辅助线。

2511公开卖点包含一致性、工业材质、光照和几何推理;2509还强调文字样式与多图编辑。当前主线仍是单图source-only程序,多图和新视角是待补齐能力。官方GPU基线尚未重新运行。

官方2511能力说明 · WeEdit文字编辑 · FineEdit区域编辑

单次回答的配对 prompt 测试

同一批24个训练任务,比较原版与执行约束版;相同RGB检查与图像judge。进度 384/384。排队/API错误单列,小样本结果用于选择下一轮实验。

生产线状态心跳近一小时首次严格通过错误重试预算耗尽
rft_and_rescuerunning2026-11-27T16:55:48.360279+08:001600
gpt62running2026-11-27T16:55:28.076835+08:0000
ti_balanced_en_v1running2026-11-27T16:55:39.931842+08:00932
ti_night_sol_v2waiting_for_off_peak2026-11-27T16:55:23.185723+08:0000
sol61_extra_v3running2026-11-27T16:55:42.435409+08:001340
teacher56_extra_v3running2026-11-27T16:55:39.748774+08:001040
sol62_extra_v3running2026-11-27T16:55:33.012217+08:0004
finegrained_extra_v1running2026-11-27T16:55:33.617215+08:001260
Teacher / prompt已完成严格合格通过率基础设施待处理
deepseek/baseline24520.8%0
deepseek/contract_v324520.8%0
gpt55/baseline24625.0%0
gpt55/contract_v324520.8%0
gpt56sol/baseline24729.2%0
gpt56sol/contract_v3241041.7%0
gpt61/baseline24937.5%0
gpt61/contract_v3241145.8%0
gpt62/baseline241145.8%0
gpt62/contract_v3241250.0%0
luna/baseline24833.3%0
luna/contract_v324625.0%0
seed21pro/baseline24312.5%0
seed21pro/contract_v32428.3%0
terra/baseline24729.2%0
terra/contract_v324937.5%0

新任务导入状态:downloading_training_shard;图片预算 64 GiB,当前约 45.11 GiB。

路由优先局部像素操作和图形编辑;复杂物体合成占每条生产线滚动24小时尝试预算至多约15%。RFT至多2个teacher、独立TI至多2个Sol、语义失败补救至多1次;排队中的请求持续等待,API错误达到重试预算后保留待处理记录。原始数据与旧实验保留。

历史导出库存与已完成实验

模型输入原图和英文编辑指令,一次输出计划与 Python,程序从原图计算修改后的 RGB。下表保留旧导出口径;新的严格合格库存见上方进度。

20,859 行历史英文导出

RFT 14,417 + TI 6,442 · 独立任务 14,256。这里包含不同 teacher、增强版本及已停用来源的存档,不能作为新版严格训练集规模。新的训练快照对所有来源执行同一质量门槛。

来源RFTTI合计
anyedit1,1044081,512
instructpix2pix1,0143461,360
magicbrush5,2383,6158,853
maskedit_en6856201,305
maskedit_scene6906231,313
sciforma1,8911472,038
ultraedit3,7956834,478

最新 RL smoke:同一批 64 个 MagicBrush dev 任务

SFT 起点

3/64

代码执行 44/64

RL 后

11/64

代码执行 46/64

3 次真实参数更新,初始 SFT reference 权重不变。10 个任务由失败变成功,2 个由成功变失败。

这是自建图像 judge 的语义评估,不是官方像素指标。目前只有一个训练 seed,旧 greedy 推理存在数值重复性问题。正在用固定数值设置重复评测;不能直接归因为稳定 RL 收益。

正在推进:同 teacher、同任务的 RFT/TI 配比实验

每个配置覆盖相同的 256 个真实训练任务,TI 替换 RFT 答案。0%、25%、50% 是样本行数比例,三组总回答 token 预算均为 100 万;实际 TI token 比例另行统计。

ti00 · 157 步 · 1,001,372 / 1,000,000 回答 tokens · loss 0.46102285385131836

固定 dev64:图像 judge 11/64,代码执行 36/64。

实际TI回答token占比 0.0% · 平均每行出现 1.23 次

ti25 · 153 步 · 1,001,002 / 1,000,000 回答 tokens · loss 0.6757060885429382

固定 dev64:图像 judge 8/64,代码执行 34/64。

实际TI回答token占比 28.6% · 平均每行出现 1.20 次

ti50 · 150 步 · 1,003,372 / 1,000,000 回答 tokens · loss 0.7614325284957886

固定 dev64:图像 judge 7/64,代码执行 30/64。

实际TI回答token占比 53.7% · 平均每行出现 1.17 次

此实验限于 RFT 和 TI 都成功的任务,测试监督形式;TI 救回难任务的覆盖收益需要另一组对照。官方 benchmark、独立人工审核、跨数据集与多 seed 结果仍待补齐。

当前重复验证

固定数值设置和6k输出预算,重新测同一批dev64;第二个RL seed采用相同设置。推理完成后展示全量结果。

流程已评任务执行成功图像judge通过
SFT起点64/644311
已有RL模型64/644610
Qwen3.8零样本64/64474

InstructPix2Pix 已停用

按用户要求,不再采集或构造该来源的RFT/TI。已经生成的原始记录、图片与旧实验保留;历史库存数字包含这些存档,新训练快照排除该来源。

持续构造 SFT 数据

任务池 42,812 个 · 本次合格轨迹 8,996 条 · 对应 8,996 个任务

生产心跳:2026-11-27T16:55:11.151981+08:00 · 白天并发配置。任务池会自动读取新数据,不在 128 条或 10k 行时停止。

Teacher / 方式运行或等待 API / 并发上限待处理已完成合格轨迹待重试
GPT-5.5 · RFT0 / 808,1241,82849
GPT-6.1-Sol · RFT7 / 814,54813,2615,2922216
GPT-5.6-Terra · RFT0 / 401,06624934
GPT-5.6-Luna · RFT0 / 401,04322323
GPT-6.1-Sol · TI0 / 803,970341581
GPT-6.2-Sol · RFT4 / 412,3242,0161,06333

自动扩库:读取新的官方训练分片。已接入任务:{"anyedit": 14559, "maskedit_scene": 4804, "maskedit_en": 2266, "maskedit_cn": 523, "ultraedit": 11528, "instructpix2pix": 2902, "sciforma": 7624, "magicbrush": 2491}。

已导出 4,728 条新 SFT 轨迹。原有 8,601 / 12,006 条对照训练集保持冻结,新增数据单独保存,用于后续规模实验。

合格要求:一次 teacher 输出、程序过滤、执行、完整目标区域的图像 judge 和重用执行检查通过。API 或 judge 错误会重试,不计为成功样本。任务按原图内容分组,排除官方 MagicBrush dev 与现有验证图片的内容重叠。TI 在 source-only 失败后补救,学生输入只包含原图和指令。

英文 TI:独立补充训练样本

RFT成功的任务也构造TI,保留两种方法的合格轨迹。新数据只保留英文任务和监督文本;旧实验保留用于复现,中文子集不进入新训练快照。北京时间工作日18:00–09:00、周末全天提高Sol并发。1:1为训练配比对照目标,当前通过率不被人为调整。

独立TI合格样本 2,158 · 生产心跳 2026-11-27T16:55:04.625965+08:00

TI teacher运行/并发上限待处理已完成合格待重试
GPT-6.1-Sol7/816,75311,1443,5272171
GPT-6.2-Sol4/416,4536,3482,197180

非工作时间补充:61/62-Sol TI

状态 waiting_for_off_peak · 心跳 2026-11-27T16:54:59.002605+08:00。仅北京时间工作日18:00–09:00或周末发起新请求;白天保留排队中的调用。优先科研图、文字布局、区域编辑和MagicBrush。

Teacher运行或等待 / 上限待构造合格轨迹
GPT-6.1-Sol0/019,7112,961
GPT-6.2-Sol0/016,6531,924

每个teacher增加至多16个夜间请求槽;额外候选使用独立ID,不改写旧轨迹。并发上限包含可能仍在API队列中的请求,不代表完成速度。

当前英文SFT库存:按样本行数统计,可相加

数据来源RFT行数TI行数合计
anyedit1,1044081,512
instructpix2pix1,0143461,360
magicbrush5,2383,6158,853
maskedit_en6856201,305
maskedit_scene6906231,313
sciforma1,8911472,038
ultraedit3,7956834,478

英文SFT行数 20,859 = RFT 14,417 + TI 6,442;独立任务覆盖 14,256。行数包含不同teacher轨迹和增强版本,后续冻结训练快照另做完全相同监督内容的去重。

展示样本的质量门槛

下方是历史候选的图像展示:自建图像judge通过、画质与保留评分至少4/5。页面顶部的新版严格库存还要求指令遵循、完整编辑范围至少4/5和完整目标判定。InstructPix2Pix已停用,旧记录存档保留,新库存排除。

质量筛选后的候选:In the main ECML diagram’s top-left area (View 1 input), completely remove the white rectangle with a thin light-green solid border that contained the 7-line black paragraph beginning “I'm not sure if they're …” with bolded phrases “thousand island” and “thick pink mayo,” leaving a blank background in its place.

sciforma · GPT-6.1-Sol · RFT · 自建图像judge通过 · 画质5/5 · 保留5/5

原图
单次 teacher 代码执行
数据集目标
完整 teacher 计划与代码
{
  "visual_evidence": [
    "The top-left View 1 input is a white text rectangle outlined by a thin light-green border.",
    "Its black paragraph begins “I'm not sure if they're” and includes bold “thousand island” and “thick pink mayo.”",
    "The surrounding diagram background is white.",
    "The View 1 label is below the rectangle, and a green arrow begins immediately to its right."
  ],
  "target_object": "The entire bordered paragraph rectangle in the View 1 input.",
  "target_region": "Approximately 3.3%–19.0% of the image width and 0%–34.5% of its height, at the top-left.",
  "editing_intent": "Remove all paragraph text and the complete light-green rectangular border, replacing them with matching blank background.",
  "editing_plan": [
    "Mask the rectangle, including its border and a narrow white margin.",
    "Sample an unobstructed white background patch above the adjacent network block.",
    "Copy that background texture across the masked region, retaining its native color and pixel variation.",
    "Feather the mask boundary inside the narrow margin and preserve every pixel outside the target region."
  ],
  "preservation_constraints": [
    "Keep the View 1 label beneath the removed rectangle unchanged.",
    "Keep the adjacent green arrow and network block unchanged.",
    "Preserve the burger photograph, all other text, arrows, evidence bars, opinion diagrams, and aggregation elements.",
    "Preserve the image dimensions."
  ]
}

def edit(image):
    import numpy as np
    import cv2
    from PIL import Image

    w, h = image.size
    src = np.asarray(image.convert('RGB')).copy()

    # This mask encompasses the paragraph and its complete green border,
    # including the border where it meets the top edge of the image.
    x0 = max(0, int(np.floor(0.0325 * w)))
    x1 = min(w, int(np.ceil(0.190625 * w)))
    y0 = 0
    y1 = min(h, int(np.ceil(0.3500 * h)))

    # Unmarked background between the removed input and the network block.
    sx0 = min(w - 1, max(0, int(round(0.200 * w))))
    sx1 = min(w, max(sx0 + 1, int(round(0.235 * w))))
    sy0 = min(h - 1, max(0, int(round(0.008 * h))))
    sy1 = min(h, max(sy0 + 1, int(round(0.085 * h))))
    patch = src[sy0:sy1, sx0:sx1]

    # Patch copying reproduces the actual blank diagram background,
    # rather than substituting an arbitrary gray or adding artificial grain.
    rh, rw = y1 - y0, x1 - x0
    ph, pw = patch.shape[:2]
    tiled = np.tile(patch, ((rh + ph - 1) // ph,
                            (rw + pw - 1) // pw, 1))[:rh, :rw]
    replacement = src.copy()
    replacement[y0:y1, x0:x1] = tiled

    mask = np.zeros((h, w), dtype=np.float32)
    mask[y0:y1, x0:x1] = 1.0
    sigma = max(0.35, 0.001 * min(w, h))
    alpha = cv2.GaussianBlur(mask, (0, 0), sigmaX=sigma,
                             sigmaY=sigma, borderType=cv2.BORDER_REPLICATE)
    # Restrict blending to the target; the surrounding arrow and label
    # are never modified. The border lies inside the full-opacity area.
    alpha *= mask
    alpha = alpha[:, :, None]
    result = np.rint(src.astype(np.float32) * (1.0 - alpha) +
                     replacement.astype(np.float32) * alpha)
    result = np.clip(result, 0, 255).astype(np.uint8)

    if image.mode == 'RGBA':
        original_alpha = np.asarray(image.getchannel('A'))
        return Image.fromarray(np.dstack((result, original_alpha)), 'RGBA')
    return Image.fromarray(result, 'RGB')

质量筛选后的候选:1. Inside the main dashed ‘For each frame’ block (centre-left of the single-panel diagram), completely remove the light-peach rectangular module with a solid black border and the centred black text “D-MAD”, which previously sat immediately above the light-blue “Quality” rectangle. 2. On the single-panel diagram’s left-centre area, delete the entire thick black compound connector that consisted of an upper right-pointing arrow feeding the peach “D-MAD” box, a vertical downward stroke, and a lower right-pointing arrow feeding the blue “Quality” box; after removal, the dashed ‘For each frame’ outline must remain with no incoming bracket-shaped arrow.

sciforma · GPT-6.1-Sol · RFT · 自建图像judge通过 · 画质5/5 · 保留5/5

原图
单次 teacher 代码执行
数据集目标
完整 teacher 计划与代码
{
  "visual_evidence": [
    "A peach rectangle labeled “D-MAD” sits above the blue “Quality” rectangle inside the dashed “For each frame” outline.",
    "A thick black connector to the left branches into two right-pointing arrows entering those rectangles.",
    "The surrounding diagram background is white."
  ],
  "target_object": "The peach D-MAD module and the entire incoming bracket-shaped connector",
  "target_region": "Centre-left: the upper module inside the dashed block and the branching connector immediately outside its left edge",
  "editing_intent": "Remove the D-MAD rectangle, its border and text, and both incoming arrows with their connecting vertical stroke, leaving clean background.",
  "editing_plan": [
    "Mask the complete peach module, including its black border and label.",
    "Detect and mask the black compound connector within a tightly bounded region, including antialiased edges.",
    "Replace masked pixels with clean white-background patches sampled from the same image.",
    "Feather the replacement boundaries while preserving the dashed outline and all neighboring diagram content."
  ],
  "preservation_constraints": [
    "Keep the entire dashed “For each frame” outline and its caption unchanged.",
    "Keep the blue Quality rectangle, border, and text unchanged.",
    "Keep the document image, input sequence, and document-to-frame arrow unchanged.",
    "Keep both outgoing score arrows, score labels, fusion block, optional connector, and final classification labels unchanged."
  ]
}

def edit(image):
    import numpy as np
    import cv2
    from PIL import Image

    w, h = image.size
    original = np.asarray(image.convert('RGB')).copy()
    mask = np.zeros((h, w), dtype=np.uint8)

    def bounds(rect):
        x0, y0, x1, y1 = rect
        return (max(0, int(np.floor(x0 * w))),
                max(0, int(np.floor(y0 * h))),
                min(w, int(np.ceil(x1 * w))),
                min(h, int(np.ceil(y1 * h))))

    # This rectangle includes the peach fill, black text, border,
    # and a small white margin, but excludes the dashed outline.
    module = bounds((0.335, 0.5203125, 0.464375, 0.675))
    mx0, my0, mx1, my1 = module
    mask[my0:my1, mx0:mx1] = 255

    # Only the incoming compound connector lies in this region.
    connector = bounds((0.2325, 0.5875, 0.321875, 0.8359375))
    cx0, cy0, cx1, cy1 = connector
    roi = original[cy0:cy1, cx0:cx1]
    dark = (np.min(roi, axis=2) < 250).astype(np.uint8) * 255
    radius = max(1, int(round(0.003125 * h)))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                      (2 * radius + 1, 2 * radius + 1))
    dark = cv2.dilate(dark, kernel)
    mask[cy0:cy1, cx0:cx1] = np.maximum(
        mask[cy0:cy1, cx0:cx1], dark)

    # Copy actual clean background pixels rather than imposing a
    # synthetic solid color or blurring adjacent diagram features.
    replacement = original.copy()
    for x0, y0, x1, y1 in (module, connector):
        bw, bh = x1 - x0, y1 - y0
        sx = int(round(0.75 * w))
        sy = int(round(0.08 * h))
        patch = original[sy:sy + bh, sx:sx + bw]
        if patch.shape[:2] != (bh, bw):
            clean = original[int(0.06 * h):int(0.25 * h),
                             int(0.72 * w):int(0.92 * w)]
            patch = cv2.resize(clean, (bw, bh), interpolation=cv2.INTER_LINEAR)
        replacement[y0:y1, x0:x1] = patch

    # Feather only inside the mask. The mask's margins ensure all
    # original ink and peach pixels receive full replacement opacity.
    distance = cv2.distanceTransform(mask, cv2.DIST_L2, 5)
    feather_width = max(0.75, 0.0015625 * h)
    alpha = np.clip(distance / feather_width, 0.0, 1.0)[..., None]
    result = np.rint(original.astype(np.float32) * (1.0 - alpha) +
                     replacement.astype(np.float32) * alpha)
    result = np.clip(result, 0, 255).astype(np.uint8)
    output = Image.fromarray(result, 'RGB')
    if image.mode == 'RGBA':
        output = output.convert('RGBA')
        output.putalpha(image.getchannel('A'))
    return output

质量筛选后的候选:Replace the tennis racket with a lightsaber

ultraedit · GPT-6.1-Sol · RFT · 自建图像judge通过 · 画质4/5 · 保留5/5

原图
单次 teacher 代码执行
数据集目标
完整 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')