747ab25325
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 26s
CI / admin (push) Successful in 17s
CI / analysis (push) Successful in 24s
CI / bff (push) Successful in 16s
CI / chore (push) Successful in 14s
CI / equipment (push) Successful in 15s
CI / express (push) Successful in 13s
CI / factory (push) Successful in 13s
CI / message (push) Successful in 10s
CI / order (push) Successful in 10s
CI / payment (push) Successful in 11s
CI / percutaneous (push) Successful in 11s
CI / product (push) Successful in 12s
CI / record (push) Successful in 12s
CI / sale (push) Successful in 11s
CI / store (push) Successful in 11s
CI / support (push) Successful in 11s
CI / task (push) Successful in 10s
CI / user (push) Successful in 18s
CI / water (push) Successful in 13s
CI / wecom (push) Successful in 13s
CI / ws (push) Successful in 6s
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package facepp
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/jpeg"
|
|
"os"
|
|
|
|
"github.com/disintegration/imaging"
|
|
)
|
|
|
|
func DrawBoxesOnFile(srcPath, dstPath string, boxes []PixelBox, stroke color.RGBA, thickness int) (int, int, error) {
|
|
img, err := imaging.Open(srcPath, imaging.AutoOrientation(true))
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
if thickness <= 0 {
|
|
thickness = 3
|
|
}
|
|
|
|
bounds := img.Bounds()
|
|
dst := image.NewNRGBA(bounds)
|
|
draw.Draw(dst, bounds, img, bounds.Min, draw.Src)
|
|
|
|
for _, box := range boxes {
|
|
drawRectBorder(dst, box, stroke, thickness)
|
|
}
|
|
|
|
if err = saveJPEG(dst, dstPath); err != nil {
|
|
return 0, 0, err
|
|
}
|
|
return bounds.Dx(), bounds.Dy(), nil
|
|
}
|
|
|
|
func drawRectBorder(img *image.NRGBA, box PixelBox, c color.RGBA, thickness int) {
|
|
x0 := int(box.Left)
|
|
y0 := int(box.Top)
|
|
x1 := int(box.Left + box.Width)
|
|
y1 := int(box.Top + box.Height)
|
|
|
|
b := img.Bounds()
|
|
if x0 < b.Min.X {
|
|
x0 = b.Min.X
|
|
}
|
|
if y0 < b.Min.Y {
|
|
y0 = b.Min.Y
|
|
}
|
|
if x1 > b.Max.X {
|
|
x1 = b.Max.X
|
|
}
|
|
if y1 > b.Max.Y {
|
|
y1 = b.Max.Y
|
|
}
|
|
if x1 <= x0 || y1 <= y0 {
|
|
return
|
|
}
|
|
|
|
for t := 0; t < thickness; t++ {
|
|
for x := x0; x < x1; x++ {
|
|
setNRGBA(img, x, y0+t, c)
|
|
setNRGBA(img, x, y1-1-t, c)
|
|
}
|
|
for y := y0; y < y1; y++ {
|
|
setNRGBA(img, x0+t, y, c)
|
|
setNRGBA(img, x1-1-t, y, c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func setNRGBA(img *image.NRGBA, x, y int, c color.RGBA) {
|
|
b := img.Bounds()
|
|
if x < b.Min.X || x >= b.Max.X || y < b.Min.Y || y >= b.Max.Y {
|
|
return
|
|
}
|
|
img.SetNRGBA(x, y, color.NRGBA{R: c.R, G: c.G, B: c.B, A: 255})
|
|
}
|
|
|
|
func saveJPEG(img image.Image, path string) error {
|
|
f, err := os.Create(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
return jpeg.Encode(f, img, &jpeg.Options{Quality: 92})
|
|
}
|