validate / validate (push) Canceled after 0s
本仓只服务桌面宿主 image-workbench-desktop,与网页端无限画布工具无关。 14 个测试/工具脚本里共 15 处仍把 image-workbench-web 当 playwright/esbuild 的依赖后备目录(含 path.resolve、报错文案、album viewer-e2e 的多余候选), 全部改为 image-workbench-desktop;album 里重复的 web 仓候选直接删掉。 这些常量只用于找 node_modules,不参与任何插件逻辑,行为零变化。 桌面仓已确认有 playwright 1.64.1 / esbuild 0.28.2,改后依赖解析有效。 本轮不动 6 处 test.image-workbench-web.pages.dev 网址(属于网页端 e2e 脚本的废弃处理,另案);插件源码与版本未动,不需要发市场。
143 lines
5.5 KiB
JavaScript
143 lines
5.5 KiB
JavaScript
// 单色/灰度描摹 CLI(开发用,不进 CI):把真实素材丢进去,直接看参数效果。
|
||
//
|
||
// node image-vectorize/tools/trace-cli.mjs <图片> [--mode mono|gray] [--levels 3]
|
||
// [--turd 3] [--alpha 1] [--tol 0.1] [--invert] [--color #1a1a1a] [--threshold -1]
|
||
// [--no-split] [--out 目录]
|
||
//
|
||
// --no-split:不按孤岛拆分(所有轮廓塞在一个 path 里,粘到 Figma 是整体)。默认拆分。
|
||
//
|
||
// 产物:<out>/<name>-<mode><levels>.svg + 一张对比图 <out>/<name>-cmp.png(原图 / 结果 / 局部放大)。
|
||
// 为什么走浏览器:PNG/JPEG 解码和 ImageData 语义都与线上一致,Node 里没有等价的零依赖解码器。
|
||
import { createRequire } from 'node:module'
|
||
import fs from 'node:fs'
|
||
import path from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { bundleEngine } from './bundle-engine.mjs'
|
||
|
||
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
||
const REPO = path.resolve(HERE, '../..')
|
||
const HOST_REPO = '/Users/qiqi/open/github/image-workbench-desktop'
|
||
|
||
let chromium = null
|
||
for (const base of [HOST_REPO, REPO, process.cwd()]) {
|
||
try {
|
||
chromium = createRequire(path.join(base, 'package.json'))('playwright').chromium
|
||
break
|
||
} catch {
|
||
/* 试下一个 */
|
||
}
|
||
}
|
||
if (!chromium) {
|
||
console.error('✗ 找不到 playwright')
|
||
process.exit(2)
|
||
}
|
||
|
||
const argv = process.argv.slice(2)
|
||
const files = []
|
||
const opt = { mode: 'gray', levels: 3, turd: 3, alpha: 1, tol: 0.1, invert: false, color: '#1a1a1a', threshold: -1, split: true, out: '/tmp/vz-cli' }
|
||
for (let i = 0; i < argv.length; i++) {
|
||
const a = argv[i]
|
||
if (a === '--mode') opt.mode = argv[++i]
|
||
else if (a === '--levels') opt.levels = Number(argv[++i])
|
||
else if (a === '--turd') opt.turd = Number(argv[++i])
|
||
else if (a === '--alpha') opt.alpha = Number(argv[++i])
|
||
else if (a === '--tol') opt.tol = Number(argv[++i])
|
||
else if (a === '--invert') opt.invert = true
|
||
else if (a === '--color') opt.color = argv[++i]
|
||
else if (a === '--threshold') opt.threshold = Number(argv[++i])
|
||
else if (a === '--no-split') opt.split = false
|
||
else if (a === '--split') opt.split = true
|
||
else if (a === '--out') opt.out = argv[++i]
|
||
else if (a.startsWith('--')) throw new Error('未知参数 ' + a)
|
||
else files.push(a)
|
||
}
|
||
if (!files.length) {
|
||
console.error('用法:node image-vectorize/tools/trace-cli.mjs <图片...> [--mode mono|gray] [--levels 3] ...')
|
||
process.exit(2)
|
||
}
|
||
fs.mkdirSync(opt.out, { recursive: true })
|
||
|
||
const bundle = bundleEngine()
|
||
const bundleCode = fs.readFileSync(bundle, 'utf8')
|
||
const browser = await chromium.launch()
|
||
const page = await browser.newPage()
|
||
await page.setContent('<canvas id="c"></canvas>')
|
||
await page.addScriptTag({ content: bundleCode })
|
||
|
||
const params = {
|
||
mode: opt.mode,
|
||
levels: opt.levels,
|
||
turdSize: opt.turd,
|
||
alphaMax: opt.alpha,
|
||
optTolerance: opt.tol,
|
||
invert: opt.invert,
|
||
color: opt.color,
|
||
threshold: opt.threshold,
|
||
outScale: 1,
|
||
splitIslands: opt.split,
|
||
}
|
||
|
||
for (const file of files) {
|
||
const b64 = fs.readFileSync(file).toString('base64')
|
||
const mime = /\.jpe?g$/i.test(file) ? 'image/jpeg' : 'image/png'
|
||
const res = await page.evaluate(
|
||
async ({ dataUrl, params }) => {
|
||
const img = new Image()
|
||
img.src = dataUrl
|
||
await img.decode()
|
||
const c = document.createElement('canvas')
|
||
c.width = img.naturalWidth
|
||
c.height = img.naturalHeight
|
||
const g = c.getContext('2d', { willReadFrequently: true })
|
||
g.drawImage(img, 0, 0)
|
||
const imageData = g.getImageData(0, 0, c.width, c.height)
|
||
const out = window.VZEngine.traceImageData(imageData, params)
|
||
return { svg: out.svg, ms: out.ms, paths: out.paths, elements: out.elements, islands: out.islands, levels: out.levels, w: c.width, h: c.height }
|
||
},
|
||
{ dataUrl: `data:${mime};base64,${b64}`, params }
|
||
)
|
||
const base = path.basename(file).replace(/\.[^.]+$/, '')
|
||
const tag = (opt.mode === 'mono' ? 'mono' : 'gray' + res.levels) + '-t' + opt.tol + (opt.split ? '-split' : '-whole')
|
||
const svgPath = path.join(opt.out, `${base}-${tag}.svg`)
|
||
fs.writeFileSync(svgPath, res.svg)
|
||
console.log(
|
||
`${base} ${tag.padEnd(14)} ${String(res.elements).padStart(4)} 元素 / ${String(res.islands).padStart(4)} 孤岛 ${String(res.ms).padStart(4)}ms ` +
|
||
`${(res.svg.length / 1024).toFixed(0)}KB ${res.w}×${res.h} → ${svgPath}`
|
||
)
|
||
|
||
// 对比图:原图 | 描摹结果
|
||
const cmp = await page.evaluate(
|
||
async ({ orig, svg, w, h }) => {
|
||
const load = (src) => new Promise((res, rej) => { const i = new Image(); i.onload = () => res(i); i.onerror = rej; i.src = src })
|
||
const a = await load(orig)
|
||
const b = await load('data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svg))))
|
||
const cw = 520
|
||
const ch = Math.round((h / w) * cw)
|
||
const cv = document.createElement('canvas')
|
||
cv.width = cw * 2 + 16
|
||
cv.height = ch + 24
|
||
const g = cv.getContext('2d')
|
||
g.fillStyle = '#fff'
|
||
g.fillRect(0, 0, cv.width, cv.height)
|
||
g.drawImage(a, 0, 20, cw, ch)
|
||
g.drawImage(b, cw + 16, 20, cw, ch)
|
||
g.fillStyle = '#333'
|
||
g.font = '12px system-ui'
|
||
g.fillText('原图', 4, 14)
|
||
g.fillText('描摹结果', cw + 20, 14)
|
||
return cv.toDataURL('image/png')
|
||
},
|
||
{
|
||
orig: `data:${mime};base64,${b64}`,
|
||
svg: res.svg.replace(/fill-opacity="0"/g, 'fill-opacity="0.001"'),
|
||
w: res.w,
|
||
h: res.h,
|
||
}
|
||
)
|
||
const cmpPath = path.join(opt.out, `${base}-cmp.png`)
|
||
fs.writeFileSync(cmpPath, Buffer.from(cmp.split(',')[1], 'base64'))
|
||
console.log(' 对比图 → ' + cmpPath)
|
||
}
|
||
|
||
await browser.close()
|