Files
kenaito-media-picker/internal/classify/classify.go
T
odboy 99b9460bcd feat(classify): 添加图片明暗分类功能
- 修改 ImageDir 函数增加 darkScore 和 lightScore 参数
- 实现 BrightnessName 函数计算明暗等级
- 定义暗色和亮色阈值等级数组
- 在 organize.go 中集成图片明暗分析逻辑
- 使用 ITU-R BT.601 公式计算像素亮度
- 为大图实现自动采样加速分析
- 更新 README.md 文档说明明暗分类算法
- 在目录结构中体现明暗等级命名规则
2025-03-29 21:11:14 +08:00

75 lines
1.8 KiB
Go

// Package classify 提供纯函数的分辨率归类规则,不依赖任何状态。
package classify
import "strings"
// Orientation 画面方向
type Orientation int
const (
OrientationSquare Orientation = iota
OrientationHorizontal
OrientationVertical
)
// OrientationOf 由宽高判定画面方向
func OrientationOf(width, height int) Orientation {
switch {
case width > height:
return OrientationHorizontal
case width < height:
return OrientationVertical
default:
return OrientationSquare
}
}
func (o Orientation) String() string {
switch o {
case OrientationHorizontal:
return "横屏"
case OrientationVertical:
return "竖屏"
default:
return "等比"
}
}
// ImageDir 返回图片归类子目录名,如 "图片_横屏_1080P-dark_100" / "图片_GIF"。
// darkScore/lightScore 用于明暗分类(GIF 不做明暗分类,忽略这两项)。
func ImageDir(ext string, width, height int, darkScore, lightScore float64) string {
if strings.EqualFold(ext, ".gif") {
return "图片_GIF"
}
dir := resolutionDir("图片", ext, width, height)
return dir + "-" + BrightnessName(darkScore, lightScore)
}
// VideoDir 返回视频归类子目录名,如 "视频_竖屏_4k" / "视频_横屏_GIF"
func VideoDir(ext string, width, height int) string {
return resolutionDir("视频", ext, width, height)
}
func resolutionDir(kind, ext string, width, height int) string {
ori := OrientationOf(width, height).String()
if strings.EqualFold(ext, ".gif") {
return kind + "_" + ori + "_GIF"
}
if suffix := matchStandard(width, height); suffix != "" {
return kind + "_" + ori + "_" + suffix
}
overflow := "原图"
if kind == "视频" {
overflow = "原画质"
}
return kind + "_" + ori + "_" + levelName(longEdge(width, height), overflow)
}
// longEdge 返回长边像素
func longEdge(w, h int) int {
if w > h {
return w
}
return h
}