diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b5de22..cf0d6c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. See [standa ## Version 2026.10.16-it-tools-pro +### ✨ OCR 无痕改字:自动匹配原字样式,输入新字即改 +- **点字框自动检测原文字样式**:角度(OCR 框顶边斜率)、字号(行高×0.82)、颜色(框内墨迹中位色,白/黑/灰/彩色识别)、粗细(中心行带墨迹横向行程中位数判粗体) +- **实时预览**:输入新文字时画布按检测样式原位重绘(所见即所得),新字过原框宽 1.15 倍自动缩号 +- **无缝替换**:inpainting 背景扩散重建擦掉原字(非色块覆盖)→ 按检测样式重绘新字 → 一步历史「无痕改字」整体撤销 +- 选项栏显示「检出 白色·粗·109px·-10°」检测徽标;「🧹 只消除」保留纯删除路径 +- 实测(渐变背景+白色粗体"人数8"倾斜-10°):检出 白色·109px·5.2°;输入"31"实时预览;回车后图层=文字「31」+智能消除+背景;导出像素验证原区域无白字残影、背景平滑无痕 + ### ✨ 文字模板扩到 47 套 · 新增 7 大平台分类 - **新增 6 平台各 5 套共 30 套**(原 17 套经典款归入「通用」): - **小红书**:品牌红 #FF2442 种草/探店/避坑/好物/vlog(「今日份宝藏」「魔都探店」) diff --git a/locales/zh.yml b/locales/zh.yml index 87a8e7b..b13fc44 100644 --- a/locales/zh.yml +++ b/locales/zh.yml @@ -307,7 +307,7 @@ tools: image-tools: title: 图片工具 - description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字/47套成套文案花字模板(通用/小红书/抖音/B站/公众号/视频号/封面图7类)+画布旋转拖角缩放/智能消除笔实时预览无痕去文字水印)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 + description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字/47套成套文案花字模板(通用/小红书/抖音/B站/公众号/视频号/封面图7类)+画布旋转拖角缩放/智能消除笔实时预览无痕去文字水印、OCR无痕改字(自动匹配原字字号/颜色/角度))、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 pdf-tools: title: PDF工具 diff --git a/src/tools/image-tools/PsEditor.vue b/src/tools/image-tools/PsEditor.vue index 6b27689..8712bdf 100644 --- a/src/tools/image-tools/PsEditor.vue +++ b/src/tools/image-tools/PsEditor.vue @@ -79,6 +79,11 @@ const hover = ref<{ x: number; y: number } | null>(null); let resizeObs: ResizeObserver | null = null; const statusHint = computed(() => { + if (selHit.value) { + return hitStyle.value + ? `无痕改字:已检出原字「${hitStyle.value.colorName}${hitStyle.value.bold ? '·粗体' : ''}·${hitStyle.value.size}px${hitStyle.value.angle ? `·${hitStyle.value.angle}°` : ''}」→ 输入新文字实时预览,回车无缝替换` + : `原字「${selHit.value.text}」→ 输入新文字实时预览,回车无缝替换(或「🧹 只消除」)`; + } if (tool.value === 'eraser') { return activeLayer.value?.type === 'paint' ? `画笔层擦除:大小 ${brushSize.value}px,按 [ ] 调整` @@ -452,6 +457,13 @@ function render(): void { if (preview.value) { ctx.drawImage(preview.value.canvas, preview.value.x, preview.value.y); } + // 无痕改字实时预览:输入新字 → 按检测样式原位重绘(所见即所得) + if (selHit.value && hitText.value.trim()) { + const hpl = hitPreviewLayer(hitText.value.trim()); + if (hpl) { + drawTextLayer(ctx, hpl); + } + } ctx.strokeStyle = 'rgba(0,0,0,0.45)'; ctx.lineWidth = 1 / zoom.value; ctx.strokeRect(0, 0, docW.value, docH.value); @@ -1034,6 +1046,160 @@ const selHitId = ref(null); const hitText = ref(''); const selHit = computed(() => ocrHits.value.find(h => h.id === selHitId.value) ?? null); +// ---------- 无痕改字:原文字样式自动检测 ---------- +interface HitStyle { size: number; color: string; bold: boolean; angle: number; cx: number; cy: number; bw: number; colorName: string } +const hitStyle = ref(null); + +// 从 OCR 框像素推断:角度(框顶边)、字号(垂直行高×0.82)、颜色(框内墨迹中位色)、粗细(笔画横向行程) +function detectTextStyle(hit: OcrHit): HitStyle | null { + if (!baseCanvas || !hit.box || hit.box.length < 4) { + return null; + } + try { + const box = hit.box.map((p: any) => [Number(p[0]), Number(p[1])]); + const [p0, p1, p2, p3] = box; + // 顶边角度(框点序:左上→右上→右下→左下),归一到 (-90, 90] 防倒置 + let angle = (Math.atan2(p1[1] - p0[1], p1[0] - p0[0]) * 180) / Math.PI; + if (angle > 90) { + angle -= 180; + } + if (angle <= -90) { + angle += 180; + } + // 垂直行高(两横边中点距)与平均行宽 + const midT = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2]; + const midB = [(p3[0] + p2[0]) / 2, (p3[1] + p2[1]) / 2]; + const bh = Math.hypot(midB[0] - midT[0], midB[1] - midT[1]); + const bw = (Math.hypot(p1[0] - p0[0], p1[1] - p0[1]) + Math.hypot(p2[0] - p3[0], p2[1] - p3[1])) / 2; + const cx = (p0[0] + p1[0] + p2[0] + p3[0]) / 4; + const cy = (p0[1] + p1[1] + p2[1] + p3[1]) / 4; + const size = Math.max(10, Math.round(bh * 0.82)); + // 采样包围盒 → 局部坐标判内外圈 + const xs = box.map((p: any) => p[0]); + const ys = box.map((p: any) => p[1]); + const x0 = Math.max(0, Math.floor(Math.min(...xs))); + const y0 = Math.max(0, Math.floor(Math.min(...ys))); + const x1 = Math.min(baseCanvas.width, Math.ceil(Math.max(...xs))); + const y1 = Math.min(baseCanvas.height, Math.ceil(Math.max(...ys))); + if (x1 - x0 < 4 || y1 - y0 < 4) { + return null; + } + const ctx = baseCanvas.getContext('2d', { willReadFrequently: true })!; + const d = ctx.getImageData(x0, y0, x1 - x0, y1 - y0).data; + const W = x1 - x0; + const a = (angle * Math.PI) / 180; + const cos = Math.cos(a); + const sin = Math.sin(a); + const ringR = Math.max(bw / 2, bh / 2) + bh * 0.5; + const ring: number[][] = []; + const ink: number[][] = []; + const inkGrid: { x: number; y: number; lx: number; ly: number; dist?: number }[] = []; + for (let py = 0; py < y1 - y0; py++) { + for (let px = 0; px < x1 - x0; px++) { + const ox = px + x0 + 0.5 - cx; + const oy = py + y0 + 0.5 - cy; + const lx = ox * cos + oy * sin; + const ly = -ox * sin + oy * cos; + const i = (py * W + px) * 4; + const c = [d[i], d[i + 1], d[i + 2]]; + if (Math.abs(lx) <= bw / 2 && Math.abs(ly) <= bh / 2) { + ink.push(c); + inkGrid.push({ x: px, y: py, lx, ly }); + } + else if (Math.abs(lx) <= ringR && Math.abs(ly) <= ringR) { + ring.push(c); + } + } + } + const med = (arr: number[], k: number) => { + const v = arr.map(c => c[k]).sort((p, q) => p - q); + return v[v.length >> 1]; + }; + const bg = ring.length > 8 ? [med(ring, 0), med(ring, 1), med(ring, 2)] : [128, 128, 128]; + const dist = (c: number[]) => Math.hypot(c[0] - bg[0], c[1] - bg[1], c[2] - bg[2]); + const thr = 60; + // 网格补距离(此前只存坐标) + for (let gi = 0; gi < inkGrid.length; gi++) { + const i4 = (inkGrid[gi].y) * W + (inkGrid[gi].x); + const off = i4 * 4; + inkGrid[gi].dist = Math.hypot(d[off] - bg[0], d[off + 1] - bg[1], d[off + 2] - bg[2]); + } + const inks = ink.filter(c => dist(c) > thr); + let color = '#000000'; + let bold = false; + let colorName = '深色'; + if (inks.length >= 6 && inks.length / Math.max(1, ink.length) > 0.008) { + const tr = Math.round(med(inks, 0)); + const tg = Math.round(med(inks, 1)); + const tb = Math.round(med(inks, 2)); + color = `#${[tr, tg, tb].map(v => v.toString(16).padStart(2, '0')).join('')}`; + const lum = 0.299 * tr + 0.587 * tg + 0.114 * tb; + const sat = Math.max(tr, tg, tb) - Math.min(tr, tg, tb); + colorName = sat < 45 && lum > 215 ? '白色' : lum < 70 ? '黑色' : sat < 45 ? '灰色' : '彩色'; + // 粗细:中心行带内墨迹横向行程中位数 / 字号 + const band = inkGrid.filter(g => Math.abs(g.ly) < bh * 0.14 && (g.dist ?? 0) > thr); + if (band.length > 10) { + const rows = new Map(); + for (const g of band) { + const k = Math.round(g.ly * 2); + if (!rows.has(k)) { + rows.set(k, []); + } + rows.get(k)!.push(g.x); + } + const runs: number[] = []; + for (const [, xs2] of rows) { + xs2.sort((m, n) => m - n); + let len = 1; + for (let i2 = 1; i2 < xs2.length; i2++) { + if (xs2[i2] === xs2[i2 - 1] + 1) { + len += 1; + } + else { + runs.push(len); + len = 1; + } + } + runs.push(len); + } + if (runs.length) { + runs.sort((m, n) => m - n); + const stroke = runs[runs.length >> 1]; + bold = stroke / size > 0.11; + } + } + } + else { + // 检不出墨迹:按背景明暗给默认字色 + const bl = 0.299 * bg[0] + 0.587 * bg[1] + 0.114 * bg[2]; + color = bl < 110 ? '#ffffff' : '#000000'; + colorName = bl < 110 ? '白色(推测)' : '黑色(推测)'; + } + return { size, color, bold, angle: Math.round(angle * 10) / 10, cx, cy, bw, colorName }; + } + catch { + return null; + } +} + +// 无痕改字的实时预览文字层(输入即所见) +function hitPreviewLayer(text: string): TextLayer | null { + const st = hitStyle.value; + if (!st) { + return null; + } + const tmp: TextLayer = { id: '__hitpv', type: 'text', name: '', visible: true, opacity: 100, text, x: 0, y: 0, size: st.size, color: st.color, bold: st.bold, rot: st.angle }; + let m = textMetrics(tmp); + // 新字比原框宽太多 → 自动缩到框宽 1.15 倍内 + if (m.w > st.bw * 1.15 && m.w > 0) { + tmp.size = Math.max(8, Math.round((st.bw * 1.15 * st.size) / m.w)); + m = textMetrics(tmp); + } + tmp.x = Math.round(st.cx - m.w / 2); + tmp.y = Math.round(st.cy - m.h / 2); + return tmp; +} + async function runOcr(): Promise { if (!props.file) { emit('error', '请先选择图片'); @@ -1124,25 +1290,50 @@ function sampleBoxColor(box: { x: number; y: number; w: number; h: number }): st } } +// 无痕改字:先用 inpaint 无缝重建背景(擦掉原字),再按检测样式重绘新字;一步历史可整体撤销 function applyHitReplace(): void { const hit = selHit.value; if (!hit || hit.used) { return; } - cancelPreview(); // 替换是另一条路径,先撤掉消除预览 - const color = sampleBoxColor(hit); - const padX = Math.max(1, Math.round(hit.w * 0.02)); - const padY = Math.max(1, Math.round(hit.h * 0.08)); - const fl: FillLayer = { id: uid(), type: 'fill', name: '擦除色块', visible: true, opacity: 100, x: Math.max(0, hit.x - padX), y: Math.max(0, hit.y - padY), w: hit.w + padX * 2, h: hit.h + padY * 2, color }; - layers.value.push(fl); const content = hitText.value.trim() || hit.text; - const tl: TextLayer = { id: uid(), type: 'text', name: `文字「${content.slice(0, 8)}」`, visible: true, opacity: 100, text: content, x: hit.x, y: hit.y + hit.h * 0.06, size: Math.max(10, Math.round(hit.h * 0.82)), color: '#000000', bold: false, rot: 0 }; + const st = hitStyle.value ?? detectTextStyle(hit); + cancelPreview(); // 替换是另一条路径,先撤掉消除预览 + // 1) 无缝擦除原字(背景扩散重建,非色块覆盖) + const res = computeInpaint(hitInpaintRect(hit)); + if (res) { + const pl = createPaintLayer('智能消除', true); + pl.canvas.getContext('2d')!.drawImage(res.canvas, res.x, res.y); + } + // 2) 按检测样式重绘新字(字号/颜色/粗细/角度与原字一致,居中对齐原框) + let tl: TextLayer; + if (st) { + const tmp: TextLayer = { id: uid(), type: 'text', name: `文字「${content.slice(0, 8)}」`, visible: true, opacity: 100, text: content, x: 0, y: 0, size: st.size, color: st.color, bold: st.bold, rot: st.angle }; + let m = textMetrics(tmp); + if (m.w > st.bw * 1.15 && m.w > 0) { + tmp.size = Math.max(8, Math.round((st.bw * 1.15 * st.size) / m.w)); // 新字过长自动缩号 + m = textMetrics(tmp); + } + tmp.x = Math.round(st.cx - m.w / 2); + tmp.y = Math.round(st.cy - m.h / 2); + tl = tmp; + } + else { + // 检测失败退回旧路径:色块覆盖 + 黑字 + const color = sampleBoxColor(hit); + const padX = Math.max(1, Math.round(hit.w * 0.02)); + const padY = Math.max(1, Math.round(hit.h * 0.08)); + const fl: FillLayer = { id: uid(), type: 'fill', name: '擦除色块', visible: true, opacity: 100, x: Math.max(0, hit.x - padX), y: Math.max(0, hit.y - padY), w: hit.w + padX * 2, h: hit.h + padY * 2, color }; + layers.value.push(fl); + tl = { id: uid(), type: 'text', name: `文字「${content.slice(0, 8)}」`, visible: true, opacity: 100, text: content, x: hit.x, y: hit.y + hit.h * 0.06, size: Math.max(10, Math.round(hit.h * 0.82)), color: '#000000', bold: false, rot: 0 }; + } layers.value.push(tl); activeLayerId.value = tl.id; hit.used = true; selHitId.value = null; hitText.value = ''; - commit('OCR 替换文字'); + hitStyle.value = null; + commit(`无痕改字「${content}」`); composite(); scheduleRender(); } @@ -1375,6 +1566,7 @@ function applyPreview(): void { if (selHitId.value === hitId) { selHitId.value = null; hitText.value = ''; + hitStyle.value = null; } } preview.value = null; @@ -1432,6 +1624,7 @@ function onDown(e: PointerEvent): void { if (oh) { selHitId.value = oh.id; hitText.value = ''; + hitStyle.value = detectTextStyle(oh); // 自动检测原文字样式(角度/字号/颜色/粗细) // 点框即预览:文字立刻“消失”,满意点✓应用,不满意可换字或取消 cancelPreview(); const res = computeInpaint(hitInpaintRect(oh)); @@ -1784,6 +1977,7 @@ function onKeyDown(e: KeyboardEvent): void { selection.value = null; dragRect.value = null; selHitId.value = null; + hitStyle.value = null; cancelPreview(); scheduleRender(); } @@ -1854,6 +2048,7 @@ async function init(): Promise { activeLayerId.value = il.id; ocrHits.value = []; selHitId.value = null; + hitStyle.value = null; selection.value = null; dragRect.value = null; ensureComp(); @@ -1960,10 +2155,21 @@ onBeforeUnmount(() => { {{ ocrBusy ? '识别中…' : '🔍 识别图中文字' }} @@ -2226,6 +2432,17 @@ onBeforeUnmount(() => { cursor: pointer; } + &-hitbadge { + background: rgba(37, 99, 235, 0.12); + border: 1px solid rgba(37, 99, 235, 0.4); + color: #2563eb; + border-radius: 6px; + font-size: 11px; + line-height: 1; + padding: 5px 8px; + white-space: nowrap; + } + &-textin { flex: 0 1 220px; min-width: 120px; diff --git a/src/tools/image-tools/image-tools.vue b/src/tools/image-tools/image-tools.vue index bbb9d99..d710138 100644 --- a/src/tools/image-tools/image-tools.vue +++ b/src/tools/image-tools/image-tools.vue @@ -24,7 +24,7 @@ const TABS: TabDef[] = [ { key: 'rotate', label: '旋转', desc: '旋转或翻转图片' }, { key: 'filter', label: '滤镜', desc: '为图片应用滤镜效果(灰度/复古/模糊等)' }, { key: 'mosaic', label: '打码', desc: '在图片上拖拽框选区域,马赛克或涂黑打码' }, - { key: 'edit', label: '图片编辑', desc: 'PS 风格图层编辑器:画笔/智能消除笔/文字/选区/油漆桶/吸管/裁剪,文字支持画布拖拽旋转+47套成套标题文案模板(通用/小红书/抖音/B站/公众号/视频号/封面图7类切换)点击即插(主副标题整组),消除实时预览,OCR 一键替换/消除图中文字' }, + { key: 'edit', label: '图片编辑', desc: 'PS 风格图层编辑器:画笔/智能消除笔/文字/选区/油漆桶/吸管/裁剪,文字支持画布拖拽旋转+47套成套标题文案模板(通用/小红书/抖音/B站/公众号/视频号/封面图7类切换)点击即插(主副标题整组),消除实时预览,OCR 无痕改字(自动检出原字字号/颜色/粗细/角度,输入新字实时预览无缝替换)' }, { key: 'exif', label: 'EXIF 查看', desc: '查看拍摄设备、时间、参数等 EXIF 信息' }, { key: 'qrgen', label: '二维码生成', desc: '把文字/链接生成为二维码图片', noFile: true }, { key: 'qrread', label: '二维码识别', desc: '识别图片中的二维码/条码内容' },