fix: 重复文件归类、图片探测与明暗分析健壮性
- organize/store: 按 MD5 去重,重复图片/视频/音频统一移入「重复文件」目录,不再覆盖已有记录 - probe/image: webp/bmp/psd/psb 专用解码失败后走 ffprobe 兜底,与通用路径一致 - probe/brightness: 大图内存缩放为缩略图再分析明暗,超4000万像素跳过分析防OOM - README: 补充重复文件目录说明,修正 Go 版本为 1.25.0
This commit is contained in:
@@ -2,7 +2,6 @@ package probe
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
_ "image/gif"
|
||||
@@ -10,27 +9,74 @@ import (
|
||||
_ "image/png"
|
||||
|
||||
_ "golang.org/x/image/bmp"
|
||||
"golang.org/x/image/draw"
|
||||
_ "golang.org/x/image/tiff"
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
const (
|
||||
// brightnessDecodeLimit 明暗分析允许解码的最大像素数,超出时跳过分析(按 mid 归类),避免超大图 OOM
|
||||
brightnessDecodeLimit = 40_000_000
|
||||
// thumbnailMaxEdge 明暗分析时大图缩放后的最长边像素,缩小到该尺寸内再统计
|
||||
thumbnailMaxEdge = 512
|
||||
// thumbnailScaleThreshold 像素数超过该值才缩放为缩略图再统计
|
||||
thumbnailScaleThreshold = 1_000_000
|
||||
)
|
||||
|
||||
// AnalyzeBrightness 打开并解码图片,分析明暗程度。
|
||||
// darkScore: 暗色像素(亮度 < 80)占比;lightScore: 亮色像素(亮度 > 175)占比。
|
||||
// 超大图(超过 brightnessDecodeLimit 像素)直接跳过分析返回 mid;大图先缩放为缩略图再统计,降低内存与耗时。
|
||||
func AnalyzeBrightness(filePath string) (darkScore float64, lightScore float64, err error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
cfg, _, err := image.DecodeConfig(f)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if cfg.Width <= 0 || cfg.Height <= 0 || cfg.Width*cfg.Height > brightnessDecodeLimit {
|
||||
return 0, 0, nil // 超大图跳过,由调用方按 mid 归类
|
||||
}
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
img, _, err := image.Decode(f)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
if img.Bounds().Dx()*img.Bounds().Dy() > thumbnailScaleThreshold {
|
||||
img = scaleToThumbnail(img, thumbnailMaxEdge)
|
||||
}
|
||||
dark, light := analyzeBrightness(img)
|
||||
return dark, light, nil
|
||||
}
|
||||
|
||||
// analyzeBrightness 统计暗色/亮色像素占比(ITU-R BT.601 亮度公式),大图自动采样加速。
|
||||
// scaleToThumbnail 将大图等比缩放到最长边不超过 maxEdge 的缩略图(纯内存,不落盘)。
|
||||
// 缩放是像素平均,整体亮度分布基本保留,用于粗粒度明暗分类足够准确。
|
||||
func scaleToThumbnail(img image.Image, maxEdge int) image.Image {
|
||||
srcBounds := img.Bounds()
|
||||
srcW, srcH := srcBounds.Dx(), srcBounds.Dy()
|
||||
if srcW <= 0 || srcH <= 0 {
|
||||
return img
|
||||
}
|
||||
ratio := float64(maxEdge) / float64(max(srcW, srcH))
|
||||
dstW, dstH := int(float64(srcW)*ratio), int(float64(srcH)*ratio)
|
||||
if dstW < 1 {
|
||||
dstW = 1
|
||||
}
|
||||
if dstH < 1 {
|
||||
dstH = 1
|
||||
}
|
||||
dst := image.NewRGBA(image.Rect(0, 0, dstW, dstH))
|
||||
draw.BiLinear.Scale(dst, dst.Bounds(), img, srcBounds, draw.Over, nil)
|
||||
return dst
|
||||
}
|
||||
|
||||
// analyzeBrightness 统计暗色/亮色像素占比(ITU-R BT.601 亮度公式)。
|
||||
// 完全透明的像素(alpha=0)不计入统计。
|
||||
func analyzeBrightness(img image.Image) (darkScore float64, lightScore float64) {
|
||||
bounds := img.Bounds()
|
||||
@@ -40,13 +86,8 @@ func analyzeBrightness(img image.Image) (darkScore float64, lightScore float64)
|
||||
}
|
||||
|
||||
darkCount, lightCount, sampleCount := 0, 0, 0
|
||||
sampleStep := 1
|
||||
if totalPixels > 1_000_000 {
|
||||
sampleStep = int(math.Sqrt(float64(totalPixels) / 100_000))
|
||||
}
|
||||
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y += sampleStep {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x += sampleStep {
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
r, g, b, a := img.At(x, y).RGBA()
|
||||
if a == 0 {
|
||||
continue
|
||||
|
||||
+29
-10
@@ -32,35 +32,54 @@ func IsSupportImage(ext string) bool {
|
||||
}
|
||||
|
||||
// ProbeImage 统一探测图片尺寸:
|
||||
// 1. 特殊格式(webp/bmp/psd/psb)走专用解码器
|
||||
// 2. 其余支持格式走 Go 标准库 image.DecodeConfig
|
||||
// 1. 特殊格式(webp/bmp/psd/psb)走专用解码器,失败后用 ffprobe 兜底
|
||||
// 2. 其余白名单格式走 Go 标准库 image.DecodeConfig
|
||||
// 3. 标准库失败时用 ffprobe 兜底(ffmpeg 支持的图片格式更广,如 tiff/svg 等)
|
||||
//
|
||||
// ffprobePath 为空表示不启用 ffprobe 兜底;扩展名不属于支持格式时返回错误。
|
||||
// ffprobePath 为空表示不启用 ffprobe 兜底;非白名单扩展名(如 tiff)在 ffprobe 可用时兜底探测。
|
||||
func ProbeImage(filePath, ffprobePath string) (*ProbeResult, error) {
|
||||
ext := strings.ToLower(filepath.Ext(filePath))
|
||||
switch ext {
|
||||
case ".webp":
|
||||
return probeWebp(filePath)
|
||||
if res, err := probeWebp(filePath); err == nil {
|
||||
return res, nil
|
||||
}
|
||||
case ".bmp":
|
||||
return probeBmp(filePath)
|
||||
if res, err := probeBmp(filePath); err == nil {
|
||||
return res, nil
|
||||
}
|
||||
case ".psd", ".psb":
|
||||
return probePsd(filePath)
|
||||
if res, err := probePsd(filePath); err == nil {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
if !IsSupportImage(ext) {
|
||||
// 非白名单扩展名(tiff/svg 等):ffprobe 可用时兜底探测,否则视为不支持
|
||||
if res, err := probeByFFprobe(ffprobePath, filePath); err == nil {
|
||||
return res, nil
|
||||
}
|
||||
return nil, fmt.Errorf("不支持的图片格式: %s", ext)
|
||||
}
|
||||
if w, h, err := decodeConfig(filePath); err == nil {
|
||||
return &ProbeResult{Width: w, Height: h}, nil
|
||||
}
|
||||
if ffprobePath != "" {
|
||||
if res, err := ProbeMediaFile(ffprobePath, filePath); err == nil && res.HasVideo {
|
||||
return res, nil
|
||||
}
|
||||
if res, err := probeByFFprobe(ffprobePath, filePath); err == nil {
|
||||
return res, nil
|
||||
}
|
||||
return nil, fmt.Errorf("无法读取图片尺寸: %s", filePath)
|
||||
}
|
||||
|
||||
// probeByFFprobe 用 ffprobe 兜底探测图片尺寸;ffprobePath 为空或探测失败时返回错误
|
||||
func probeByFFprobe(ffprobePath, filePath string) (*ProbeResult, error) {
|
||||
if ffprobePath == "" {
|
||||
return nil, fmt.Errorf("未启用 ffprobe 兜底")
|
||||
}
|
||||
if res, err := ProbeMediaFile(ffprobePath, filePath); err == nil && res.HasVideo {
|
||||
return res, nil
|
||||
}
|
||||
return nil, fmt.Errorf("ffprobe 兜底失败: %s", filePath)
|
||||
}
|
||||
|
||||
// decodeConfig 用 Go 标准库读取图片宽高
|
||||
func decodeConfig(filePath string) (int, int, error) {
|
||||
f, err := os.Open(filePath)
|
||||
|
||||
Reference in New Issue
Block a user