Skip to content

vision_extract_foreground fails on Windows (zh-CN locale): "unexpected output" — upstream Chinese stdout arrives as GBK, parser regexes are UTF-8 #9

Description

@che269313-coder

Plugin: @dsh-external/dsh-vision-toolkit v0.1.2 (local link install)
OS: Windows 11 (zh-CN) | Node: 24.13.0 | Python: 3.13.12 (managed runtime, uv)

Repro

Call vision_extract_foreground on any image (tested on a 1628x896 PNG screenshot, auto and color modes).

Error: extract_foreground: unexpected output: bbox (ԭͼ����): x1: 22, y1: 850, x2: 49, y2: 877 | ǰ������: 593  ... ������ռ��: 48%

(the Chinese in the error is mojibake)

Root cause

The pinned skills/vision-tools/scripts/extract_fg.py prints Chinese diagnostics:

print(f"bbox (原图像素): x1: {bx1}, y1: {by1}, x2: {bx2}, y2: {by2}")
print(f"前景像素: {len(best)}  保留分量: {len(kept)}/{len(comps)}  最大分量占比: ...%")
print(f"wrote {dest} ({out.width}x{out.height})")

On Windows, Python encodes pipe stdout with the ANSI code page (cp936 on zh-CN) because the plugin's subprocess env in lib/upstream.js run() does not set PYTHONIOENCODING/PYTHONUTF8/LANG. The plugin parses stdout as UTF-8 (lib/upstream.js parseExtractForegroundOutput, regexes like /^bbox \(原图像素\):/), so no line ever matches and the call fails with unexpected output. I verified by running the vendored script directly — its stdout is GBK and shows as mojibake when interpreted as UTF-8.

The error message itself is also hard to read because the raw misdecoded bytes are embedded in it.

Suggested fix

Set PYTHONIOENCODING=utf-8 (or PYTHONUTF8=1) in the plugin subprocess env for local upstream tools, and/or have the upstream scripts reconfigure stdout (sys.stdout.reconfigure(encoding='utf-8')). Consider making the error path decode-safe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions