feat: add image icon detection and filtering functionality

- Introduced a new function `isIconImage` to identify small images and decorative elements based on pixel dimensions and file size.
- Updated the image resolution logic to filter out identified icon images from markdown content, enhancing the quality of stored images.
- Added unit tests for icon image detection to ensure accurate functionality across various image sizes.

These changes improve image handling by effectively filtering out non-essential images, leading to cleaner document processing.
This commit is contained in:
wizardchen
2026-03-04 20:35:38 +08:00
committed by lyingbug
parent 1b8d7bd4de
commit 45eba325e2
2 changed files with 105 additions and 0 deletions
@@ -1,7 +1,12 @@
package docparser
import (
"bytes"
"context"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"path/filepath"
"regexp"
@@ -12,6 +17,30 @@ import (
"github.com/google/uuid"
)
const (
// minImageDimension is the minimum width/height in pixels; images smaller
// than this on either axis are treated as icons and filtered out.
minImageDimension = 128
// minImageBytes is the minimum file size in bytes; very small images are
// almost certainly icons or decorative elements.
minImageBytes = 512 // 512 bytes
)
// isIconImage returns true if the image data looks like a small icon or
// decorative element that should be filtered out. It checks pixel dimensions
// when decodable, and falls back to raw byte size otherwise.
func isIconImage(data []byte) bool {
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {
// Cannot decode dimensions — fall back to size-only heuristic.
return len(data) < minImageBytes
}
if cfg.Width < minImageDimension || cfg.Height < minImageDimension {
return true
}
return false
}
// StoredImage describes an image that has been saved to storage.
type StoredImage struct {
OriginalRef string // reference in the original markdown
@@ -72,6 +101,13 @@ func (r *ImageResolver) ResolveAndStore(
continue
}
// Filter out small icons and decorative images
if isIconImage(ref.ImageData) {
// Remove the image reference from markdown entirely
markdown = markdown[:m[0]] + markdown[m[1]:]
continue
}
// Determine extension
ext := extFromMime(ref.MimeType)
if ext == "" {