From b4b6c110cc6cf1b58def753746deb8a6fa920568 Mon Sep 17 00:00:00 2001 From: dj-oyu <68707227+dj-oyu@users.noreply.github.com> Date: Thu, 19 Mar 2026 23:09:53 +0900 Subject: [PATCH] feat: clean up yomitoku page images (_pN.jpg) after OCR yomitoku always generates per-page JPEG files that cannot be suppressed via CLI options. Remove these after successful OCR while preserving the markdown output and figures/ directory (referenced by markdown). Co-Authored-By: Claude Opus 4.6 (1M context) --- pkg/agent/loop_media.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/agent/loop_media.go b/pkg/agent/loop_media.go index 28f512a17..6474df3d0 100644 --- a/pkg/agent/loop_media.go +++ b/pkg/agent/loop_media.go @@ -568,6 +568,11 @@ func (al *AgentLoop) ocrPDF( return fmt.Sprintf("[file:%s]", pdfPath) } + // Clean up page images (_pN.jpg) generated by yomitoku. + // These are always created and cannot be suppressed via CLI options. + // Keep: .md files, figures/ directory (referenced by markdown output). + cleanupOCRPageImages(outputDir, pdfPath) + // Find the output markdown file mdPath := findOCROutput(outputDir, pdfPath) if mdPath == "" { @@ -669,3 +674,39 @@ func ocrEnvSlice(env map[string]string) []string { } return result } + +// cleanupOCRPageImages removes _pN.jpg files generated by yomitoku. +// These per-page images are always created by the CLI and cannot be suppressed. +// Only top-level _pN.jpg files matching the PDF basename are removed; +// the figures/ subdirectory and .md files are preserved. +func cleanupOCRPageImages(outputDir, pdfPath string) { + base := strings.TrimSuffix(filepath.Base(pdfPath), filepath.Ext(pdfPath)) + entries, err := os.ReadDir(outputDir) + if err != nil { + return + } + + removed := 0 + for _, e := range entries { + if e.IsDir() { + continue + } + name := e.Name() + // Match pattern: __pN.jpg + if !strings.HasSuffix(strings.ToLower(name), ".jpg") { + continue + } + if !strings.Contains(name, base+"_p") { + continue + } + if rmErr := os.Remove(filepath.Join(outputDir, name)); rmErr == nil { + removed++ + } + } + if removed > 0 { + logger.DebugCF("agent", "Cleaned up OCR page images", map[string]any{ + "dir": outputDir, + "removed": removed, + }) + } +}