Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: QRCode-Server

on:
push:
branches: [ main ]
tags: [ 'v*.*.*' ] # 匹配语义化版本 Tag(如 v1.0.0)
pull_request:
branches: [ main ]

permissions:
contents: write

jobs:
code-check:
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4

- name: 设置 Go 环境
uses: actions/setup-go@v4
with:
go-version: '1.24'
cache: true

- name: 安装依赖
run: go mod init read-image-color && go mod tidy && go mod verify

- name: 代码 lint 检查
uses: golangci/golangci-lint-action@v6
with:
version: latest
args: --timeout=5m --enable=goimports,revive

- name: 编译验证
run: go build -o /dev/null main.go


multi-platform-build:
needs: code-check
runs-on: ubuntu-latest
strategy:
fail-fast: false # 某个平台失败不影响其他
matrix:
# 使用对象数组的方式定义平台配置,确保各维度正确对应
platform:
- { goos: linux, goarch: amd64, output: "read-image-color-linux-amd64", zip: "linux-amd64.zip" }
- { goos: linux, goarch: arm64, output: "read-image-color-linux-arm64", zip: "linux-arm64.zip" }
- { goos: windows, goarch: amd64, output: "read-image-color-windows-amd64.exe", zip: "windows-amd64.zip" }
- { goos: darwin, goarch: amd64, output: "read-image-color-darwin-amd64", zip: "darwin-amd64.zip" }
- { goos: darwin, goarch: arm64, output: "read-image-color-darwin-arm64", zip: "darwin-arm64.zip" }
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: 设置 Go 环境
uses: actions/setup-go@v4
with:
go-version: '1.24'
cache: true

- name: 安装依赖
run: go mod init read-image-color && go mod tidy

- name: 生成版本信息
id: version
run: |
if [[ "${{ github.ref_type }}" == "tag" ]]; then
VERSION="${{ github.ref_name }}"
else
VERSION="main-$(git rev-parse --short HEAD)"
fi
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "version=$VERSION" >> $GITHUB_OUTPUT

- name: 编译 ${{ matrix.platform.goos }}/${{ matrix.platform.goarch }} 平台
run: |
# 使用 matrix 中的平台对象变量
GOOS=${{ matrix.platform.goos }} \
GOARCH=${{ matrix.platform.goarch }} \
go build -ldflags "-s -w -X main.version=${{ env.VERSION }}" \
-o "bin/${{ matrix.platform.output }}" main.go
# 压缩产物
cd bin && zip ${{ matrix.platform.zip }} ${{ matrix.platform.output }}

- name: 暂存编译产物
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.platform.zip }}
path: bin/${{ matrix.platform.zip }}
retention-days: 14
if-no-files-found: error


release:
needs: multi-platform-build
if: github.ref_type == 'tag'
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4

- name: 下载所有编译产物
uses: actions/download-artifact@v4
with:
path: release-artifacts

- name: 整理产物
run: |
mkdir -p final-release
find release-artifacts -type f -name "*.zip" -exec mv {} final-release/ \;

- name: 创建 GitHub Release
uses: softprops/action-gh-release@v2
with:
name: ${{ github.ref_name }}
body_path: README.md
files: final-release/*.zip
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,136 @@
# read-image-color
基于豆包变成实现的go语言的图片大致颜色取色


# 图片颜色数值化统计工具

一个基于 Go 语言的图片颜色分析工具,支持提取图片的 RGB/HEX/HSV 量化数据、核心颜色占比统计,无任何语义化颜色名称,仅输出数值化结果,适合数据统计、机器视觉分析等场景。


## 功能特性
- **数值化输出**:仅保留 RGB、HEX、HSV 量化数据,无颜色名称,聚焦数据本身
- **核心颜色统计**:按 HSV 色调聚类,自动过滤低占比颜色(≥3%),最多保留 8 种核心颜色
- **多格式支持**:提供「友好阅读模式」和「JSON 模式」,满足人工查看和程序解析需求
- **智能缩放**:自动适配图片尺寸(默认 20x20),平衡分析精度与处理速度
- **代码规范**:通过 `golangci-lint` 全量检查,符合 Go 官方编码规范


## 环境依赖
- **Go 版本**:1.18+(推荐 1.20+)
- **依赖包**:仅需一个第三方图片缩放库
```bash
go get github.com/nfnt/resize@latest
```


## 快速开始

### 1. 安装工具
```bash
# 克隆代码(或直接下载 main.go)
git clone https://github.com/OIAPI/read-image-color.git
cd read-image-color

# 安装依赖
go mod init read-image-color
go get github.com/nfnt/resize@latest
```

### 2. 基本使用
#### (1)友好阅读模式(默认)
适合人工查看,输出格式化的 RGB/HEX/HSV 数据:
```bash
go run main.go -path ./test.jpg
```
**示例输出**:
```
======================================
📷 图片路径:./test.jpg
🖼️ 处理尺寸:20x20 像素(总像素:400)
🎯 整体平均数据:
- 平均RGB:[242, 128, 65]
- 平均HEX:#F28041
- 平均HSV:[H:22.5°, S:73.2%, V:94.9%]

🏆 核心颜色统计(共 2 种,占比≥3%):
1. 占比:68.5% | 像素数:274 | RGB:[245,125,60] | HEX:#F57D3C | HSV:[H:21.8°, S:75.5%, V:96.1%]
2. 占比:22.3% | 像素数:89 | RGB:[250,180,120] | HEX:#FAB478 | HSV:[H:25.2°, S:52.0%, V:98.0%]
======================================
```

#### (2)JSON 模式(程序解析)
适合后端/前端调用,输出标准 JSON 格式:
```bash
go run main.go -path ./test.jpg -format json -scale 25
```
**示例输出**:
```json
{
"image_path": "./test.jpg",
"process_size": 25,
"total_pixel": 625,
"avg_rgb": [242, 128, 65],
"avg_hex": "#F28041",
"avg_hsv": {
"H": 22.5,
"S": 73.2,
"V": 94.9
},
"main_stats": [
{
"count": 428,
"ratio": 68.5,
"represent_rgb": [245, 125, 60],
"represent_hex": "#F57D3C",
"represent_hsv": {
"H": 21.8,
"S": 75.5,
"V": 96.1
}
}
]
}
```


## 命令行参数说明
| 参数名 | 类型 | 必选 | 默认值 | 说明 |
|--------------|--------|------|---------|----------------------------------------------------------------------|
| `-path` | string | 是 | 无 | 图片路径(支持 PNG/JPG 格式,相对路径或绝对路径均可,如 `./sunset.jpg`) |
| `-format` | string | 否 | `human` | 输出格式:`human`(友好阅读)、`json`(标准 JSON) |
| `-scale` | uint | 否 | 0 | 手动缩放尺寸(如 `25` 表示 25x25 像素,建议 15-50,0 表示智能缩放) |


## 代码质量检查
本项目使用 `golangci-lint` 保障代码规范,执行以下命令可自行检查:
```bash
# 1. 安装 golangci-lint(仅首次需要)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2

# 2. 执行全量检查
golangci-lint run ./...
```


## 核心数据说明
| 字段名 | 含义 |
|------------------|----------------------------------------------------------------------|
| `process_size` | 图片处理时的缩放尺寸(N x N),尺寸越大分析越精细,但速度越慢 |
| `total_pixel` | 处理后的总像素数(= process_size × process_size) |
| `avg_rgb` | 全图像素的 RGB 平均值(每个通道 0-255) |
| `avg_hex` | 平均 RGB 对应的 16 进制颜色码(如 `#FF5733`,可直接用于设计工具) |
| `avg_hsv` | 平均 HSV 值(H: 0-360° 色调,S: 0-100% 饱和度,V: 0-100% 亮度) |
| `main_stats` | 核心颜色列表(占比≥3%),按占比降序排列,包含像素数、代表 RGB/HEX/HSV |


## 常见问题
1. **Q:提示“找不到 goimports/golangci-lint 命令”?**
A:需将 Go 的 `bin` 目录(`$(go env GOPATH)/bin`)添加到系统环境变量,参考 [Go 环境变量配置](https://go.dev/doc/install#install)。

2. **Q:支持哪些图片格式?**
A:默认支持 PNG 和 JPG,如需支持其他格式(如 WebP),可在代码中添加对应导入(如 `_ "image/webp"`)。

3. **Q:如何调整核心颜色的占比阈值(默认≥3%)?**
A:修改 `GetMainStats` 函数中的 `if ratio < 3.0 { continue }`,将 `3.0` 改为目标阈值(如 `5.0`)。

内容由 AI 生成
Loading
Loading