diff --git a/CHANGELOG.md b/CHANGELOG.md index cd5f2c4..60c9f77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. See [standa ## Version 2026.10.16-it-tools-pro +### ✨ 图片编辑:智能消除(无痕删除图中文字/水印) +- **OCR 路径**:识别文字后点框 →「🧹 消除文字」自动识别文字笔画像素,从周边背景扩散重建(inpainting),渐变/纹理背景自然补全,不再是纯色覆盖 +- **选框路径**:框选任意区域(文字/水印/杂物)→「🧹 消除选区内容」一键无痕消除,无需 OCR +- 消除结果写入独立「智能消除」画笔图层:非破坏性、可撤销(Ctrl+Z)、可调不透明度、可再编辑 +- 修复:mask 检测覆盖选区全部像素(原实现跳过边缘 2px,选区边缘的字符会残留);视口尺寸变化时按中心补偿平移(原实现布局稳定后图片偏移导致交互坐标错位) + ### ✨ 图片编辑升级为 PS 风格图层编辑器 - **9 工具**:移动/选框/画笔/橡皮/文字/油漆桶(容差可调)/吸管取色/裁剪/抓手,快捷键 V M B E T G I C H,`[ ]` 调笔刷、`0` 适应、`1` 原尺寸 - **图层面板**:背景/画笔/文字/色块四类图层,显示隐藏、不透明度、上移下移、删除、新建画笔层;文字层改内容/字号/颜色/加粗,色块改底色 diff --git a/locales/en.yml b/locales/en.yml index 7b6c7e4..c08335c 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -310,7 +310,7 @@ tools: image-tools: title: Image tools - description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, Photoshop-style editor (layers, brush, text, selection, OCR text replacement), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device." + description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, Photoshop-style editor (layers, brush, text, selection, OCR text replacement, content-aware erase), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device." pdf-tools: title: PDF tools diff --git a/locales/zh.yml b/locales/zh.yml index fa8c174..34557ea 100644 --- a/locales/zh.yml +++ b/locales/zh.yml @@ -307,7 +307,7 @@ tools: image-tools: title: 图片工具 - description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 + description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字/智能消除无痕去文字水印)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 pdf-tools: title: PDF工具 diff --git a/src/pages/About.vue b/src/pages/About.vue index cd8c087..b994530 100644 --- a/src/pages/About.vue +++ b/src/pages/About.vue @@ -53,7 +53,7 @@ const groups: { title: string; items: Item[] }[] = [ { name: '多图拼接', icon: '拼', desc: '横向/纵向长图/九宫格/瀑布流/交错双列/拍立得 6 种方式,网格类 2-4 列可调,间距可调', tag: '改造' }, { name: 'JSON 美化和格式化', icon: 'J', desc: '双栏专业工作台:左侧行号编辑,右侧树视图/语法高亮切换,含 Dict/Get参数/XML/CSV 等 7 种转换', tag: '改造' }, { name: 'PDF工具', icon: 'P', desc: '合并/拆分/旋转/重排/压缩/加密/解密/水印/提取文本/页码/转图片,11 功能全程浏览器本地处理,需授权使用', tag: '授权' }, - { name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/PS 风格编辑器(图层+画笔+文字+选区+历史记录+OCR 改字)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' }, + { name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/PS 风格编辑器(图层+画笔+文字+选区+历史记录+OCR 改字/智能消除无痕去文字水印)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' }, ], }, { diff --git a/src/tools/image-tools/PsEditor.vue b/src/tools/image-tools/PsEditor.vue index 8505ff4..bd321f7 100644 --- a/src/tools/image-tools/PsEditor.vue +++ b/src/tools/image-tools/PsEditor.vue @@ -73,7 +73,7 @@ const statusHint = computed(() => { return '拖动文字/色块;选中画笔层后可整体移动'; } if (tool.value === 'marquee') { - return '拖拽框选,松手后可填充/清除选区'; + return '拖拽框选,松手后可「消除选区内容」(无痕去文字/水印)或填充/清除'; } if (tool.value === 'text') { return '点击画布放置文字层'; @@ -243,6 +243,7 @@ function scheduleRender(): void { }); } +const lastView = { w: 0, h: 0 }; let checkerPattern: CanvasPattern | null = null; function getChecker(ctx: CanvasRenderingContext2D): CanvasPattern { if (!checkerPattern) { @@ -277,6 +278,13 @@ function render(): void { const vw = host.clientWidth; const vh = host.clientHeight; if (c.width !== Math.round(vw * dpr) || c.height !== Math.round(vh * dpr)) { + // 视口尺寸变化:按中心补偿平移,保持图像居中(否则布局稳定后图片会偏移) + if (lastView.w) { + panX.value += (vw - lastView.w) / 2; + panY.value += (vh - lastView.h) / 2; + } + lastView.w = vw; + lastView.h = vh; c.width = Math.round(vw * dpr); c.height = Math.round(vh * dpr); c.style.width = `${vw}px`; @@ -430,26 +438,29 @@ function normRect(a: { x: number; y: number }, b: { x: number; y: number }): { x return { x: Math.round(Math.min(a.x, b.x)), y: Math.round(Math.min(a.y, b.y)), w: Math.round(Math.abs(b.x - a.x)), h: Math.round(Math.abs(b.y - a.y)) }; } -// ---------- 图层操作 ---------- -function ensurePaintLayer(name?: string): PaintLayer { - let l = layers.value.find(v => v.id === activeLayerId.value); - if (l && l.type === 'paint') { - return l; - } +// ---------- 选区操作 ---------- +function createPaintLayer(name: string): PaintLayer { const c = document.createElement('canvas'); c.width = docW.value; c.height = docH.value; - const pl: PaintLayer = { id: uid(), type: 'paint', name: name ?? '画笔图层', visible: true, opacity: 100, ox: 0, oy: 0, canvas: c }; - // 插到文字/色块层下方(底图之上) + const pl: PaintLayer = { id: uid(), type: 'paint', name, visible: true, opacity: 100, ox: 0, oy: 0, canvas: c }; let idx = layers.value.findIndex(v => v.type === 'image'); if (idx < 0) { - idx = 0; + idx = layers.value.length - 1; } layers.value.splice(idx + 1, 0, pl); activeLayerId.value = pl.id; return pl; } +function ensurePaintLayer(name?: string): PaintLayer { + const l = layers.value.find(v => v.id === activeLayerId.value); + if (l && l.type === 'paint') { + return l; + } + return createPaintLayer(name ?? '画笔图层'); +} + function addPaintLayer(): void { const c = document.createElement('canvas'); c.width = docW.value; @@ -534,6 +545,25 @@ function fillSelection(): void { scheduleRender(); } +// 智能消除选区:框住文字/水印/杂物 → 从周边背景扩散重建,无需 OCR +function inpaintSelection(): void { + const s = selection.value; + if (!s || s.w < 4 || s.h < 4) { + emit('error', '请先框选要消除的区域(不小于 4×4)'); + return; + } + const ok = inpaintRect(s); + if (ok) { + selection.value = null; + commit('智能消除选区'); + composite(); + scheduleRender(); + } + else { + emit('error', '未检出可消除的内容(区域与背景一致),可改用填充/画笔'); + } +} + function clearSelection(): void { const s = selection.value; if (!s || s.w < 1 || s.h < 1) { @@ -822,18 +852,182 @@ function applyHitErase(): void { if (!hit || hit.used) { return; } - 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); + // 智能消除:识别笔画像素,从周边背景扩散重建(inpainting) + const padX = Math.max(2, Math.round(hit.w * 0.04)); + const padY = Math.max(2, Math.round(hit.h * 0.1)); + const rect = { + x: Math.max(0, hit.x - padX), + y: Math.max(0, hit.y - padY), + w: Math.min(docW.value, hit.w + padX * 2), + h: Math.min(docH.value, hit.h + padY * 2), + }; + rect.w = Math.min(rect.w, docW.value - rect.x); + rect.h = Math.min(rect.h, docH.value - rect.y); + const ok = inpaintRect(rect); + if (!ok) { + return; + } hit.used = true; selHitId.value = null; - commit('OCR 擦除文字'); + commit('OCR 消除文字'); composite(); scheduleRender(); } +// ---------- 智能消除(inpainting) ---------- +// 1) 取矩形区域(含外扩边距)像素;2) 与周边背景色差异大的判为文字笔画; +// 3) 笔画像素由四周已知像素迭代扩散重建(Gauss-Seidel),背景连续纹理/渐变都能自然补上; +// 4) 只把重建像素写入一个新的「智能消除」画笔层,非破坏性、可撤销、可再编辑。 +function inpaintRect(rect: { x: number; y: number; w: number; h: number }): boolean { + if (!compCanvas || rect.w < 2 || rect.h < 2) { + return false; + } + const rw = Math.round(rect.w); + const rh = Math.round(rect.h); + const rx = Math.round(rect.x); + const ry = Math.round(rect.y); + const src = compCanvas.getContext('2d', { willReadFrequently: true })!.getImageData(rx, ry, rw, rh); + const d = src.data; + // 1) 背景色:区域外圈 2px 环带的中位色 + const rs: number[] = []; + const gs: number[] = []; + const bs: number[] = []; + for (let y = 0; y < rh; y++) { + for (let x = 0; x < rw; x++) { + const onRing = x < 2 || y < 2 || x >= rw - 2 || y >= rh - 2; + if (!onRing) { + continue; + } + const i = (y * rw + x) * 4; + rs.push(d[i]); + gs.push(d[i + 1]); + bs.push(d[i + 2]); + } + } + if (!rs.length) { + return false; + } + const med = (a: number[]) => { + a.sort((p, q) => p - q); + return a[a.length >> 1]; + }; + const bgR = med(rs); + const bgG = med(gs); + const bgB = med(bs); + // 2) 笔画掩码:与背景色差异大的像素(全区域检测,含边缘字符) + const mask = new Uint8Array(rw * rh); + let unknown = 0; + const thr = 42; + for (let y = 0; y < rh; y++) { + for (let x = 0; x < rw; x++) { + const p = y * rw + x; + const i = p * 4; + const dist = Math.abs(d[i] - bgR) + Math.abs(d[i + 1] - bgG) + Math.abs(d[i + 2] - bgB); + if (dist > thr) { + mask[p] = 1; + unknown++; + } + } + } + if (!unknown) { + return false; // 没检出文字,无需处理 + } + // 掩码向外扩 1px,把抗锯齿的半透明边缘也盖住(带边界检查) + const grown = mask.slice(); + for (let y = 0; y < rh; y++) { + for (let x = 0; x < rw; x++) { + const p = y * rw + x; + if (mask[p]) { + continue; + } + if ((x > 0 && mask[p - 1]) || (x < rw - 1 && mask[p + 1]) || (y > 0 && mask[p - rw]) || (y < rh - 1 && mask[p + rw])) { + grown[p] = 1; + unknown++; + } + } + } + // 3) 扩散重建:未知像素反复取四邻均值(迭代次数随区域尺寸自适应) + const buf = new Float32Array(rw * rh * 3); + for (let p = 0; p < rw * rh; p++) { + const i = p * 4; + buf[p * 3] = d[i]; + buf[p * 3 + 1] = d[i + 1]; + buf[p * 3 + 2] = d[i + 2]; + } + const iters = Math.min(500, Math.max(rw, rh) * 2); + for (let it = 0; it < iters; it++) { + // 交替扫描方向,消除顺序偏置 + const rev = it % 2 === 1; + for (let yy = 0; yy < rh; yy++) { + const y = rev ? rh - 1 - yy : yy; + for (let xx = 0; xx < rw; xx++) { + const x = rev ? rw - 1 - xx : xx; + const p = y * rw + x; + if (!grown[p]) { + continue; + } + let sr = 0; + let sg = 0; + let sb = 0; + let c = 0; + if (x > 0) { + const q = (p - 1) * 3; + sr += buf[q]; + sg += buf[q + 1]; + sb += buf[q + 2]; + c++; + } + if (x < rw - 1) { + const q = (p + 1) * 3; + sr += buf[q]; + sg += buf[q + 1]; + sb += buf[q + 2]; + c++; + } + if (y > 0) { + const q = (p - rw) * 3; + sr += buf[q]; + sg += buf[q + 1]; + sb += buf[q + 2]; + c++; + } + if (y < rh - 1) { + const q = (p + rw) * 3; + sr += buf[q]; + sg += buf[q + 1]; + sb += buf[q + 2]; + c++; + } + if (c) { + buf[p * 3] = sr / c; + buf[p * 3 + 1] = sg / c; + buf[p * 3 + 2] = sb / c; + } + } + } + } + // 4) 写入独立画笔层(只含重建区域,其余透明) + const out = new ImageData(rw, rh); + const od = out.data; + for (let p = 0; p < rw * rh; p++) { + if (!grown[p]) { + continue; + } + const i = p * 4; + od[i] = Math.max(0, Math.min(255, Math.round(buf[p * 3]))); + od[i + 1] = Math.max(0, Math.min(255, Math.round(buf[p * 3 + 1]))); + od[i + 2] = Math.max(0, Math.min(255, Math.round(buf[p * 3 + 2]))); + od[i + 3] = 255; + } + const tmp = document.createElement('canvas'); + tmp.width = rw; + tmp.height = rh; + tmp.getContext('2d')!.putImageData(out, 0, 0); + const pl = createPaintLayer('智能消除'); + pl.canvas.getContext('2d')!.drawImage(tmp, rx, ry); + return true; +} + function ocrHitTest(dx: number, dy: number): OcrHit | null { for (const h of ocrHits.value) { if (!h.used && dx >= h.x - 3 && dx <= h.x + h.w + 3 && dy >= h.y - 3 && dy <= h.y + h.h + 3) { @@ -1239,6 +1433,10 @@ onBeforeUnmount(() => {