feat(image-manipulator): redesign as Photoshop Lite with Visual Cropper, universal batch processing, and Black Obsidian Liquid Glass
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"math"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
@@ -45,9 +46,9 @@ func resizeImage(img image.Image, width, height int, maintainAspect bool) image.
|
||||
}
|
||||
|
||||
func filterImage(img image.Image, filterType string) image.Image {
|
||||
switch filterType {
|
||||
switch strings.ToUpper(filterType) {
|
||||
case "BLUR":
|
||||
return imaging.Blur(img, 2.0)
|
||||
return imaging.Blur(img, 2.5)
|
||||
case "SHARPEN":
|
||||
return imaging.Sharpen(img, 2.0)
|
||||
case "GRAYSCALE":
|
||||
@@ -57,24 +58,203 @@ func filterImage(img image.Image, filterType string) image.Image {
|
||||
case "SEPIA":
|
||||
gray := imaging.Grayscale(img)
|
||||
return imaging.AdjustSaturation(imaging.AdjustSigmoid(gray, 0.5, 3.0), 50)
|
||||
case "EMBOSS":
|
||||
kernel := [9]float64{
|
||||
-2, -1, 0,
|
||||
-1, 1, 1,
|
||||
0, 1, 2,
|
||||
}
|
||||
return imaging.Convolve3x3(img, kernel, nil)
|
||||
case "CONTOUR", "EDGE_ENHANCE":
|
||||
kernel := [9]float64{
|
||||
-1, -1, -1,
|
||||
-1, 8, -1,
|
||||
-1, -1, -1,
|
||||
}
|
||||
return imaging.Convolve3x3(img, kernel, nil)
|
||||
case "WARM":
|
||||
return imaging.AdjustFunc(img, func(c color.NRGBA) color.NRGBA {
|
||||
r := int(c.R) + 25
|
||||
b := int(c.B) - 15
|
||||
if r > 255 { r = 255 }
|
||||
if b < 0 { b = 0 }
|
||||
return color.NRGBA{R: uint8(r), G: c.G, B: uint8(b), A: c.A}
|
||||
})
|
||||
case "COOL":
|
||||
return imaging.AdjustFunc(img, func(c color.NRGBA) color.NRGBA {
|
||||
r := int(c.R) - 15
|
||||
b := int(c.B) + 30
|
||||
if r < 0 { r = 0 }
|
||||
if b > 255 { b = 255 }
|
||||
return color.NRGBA{R: uint8(r), G: c.G, B: uint8(b), A: c.A}
|
||||
})
|
||||
case "VINTAGE":
|
||||
sepia := imaging.AdjustSaturation(imaging.AdjustSigmoid(imaging.Grayscale(img), 0.5, 2.5), 35)
|
||||
return imaging.AdjustContrast(sepia, 15)
|
||||
default:
|
||||
return img
|
||||
}
|
||||
}
|
||||
|
||||
func parseHexColor(hexStr string) color.NRGBA {
|
||||
hexStr = strings.TrimPrefix(strings.TrimSpace(hexStr), "#")
|
||||
if len(hexStr) == 3 {
|
||||
hexStr = string([]byte{hexStr[0], hexStr[0], hexStr[1], hexStr[1], hexStr[2], hexStr[2]})
|
||||
}
|
||||
if len(hexStr) == 6 {
|
||||
val, err := strconv.ParseUint(hexStr, 16, 32)
|
||||
if err == nil {
|
||||
return color.NRGBA{
|
||||
R: uint8(val >> 16),
|
||||
G: uint8((val >> 8) & 0xFF),
|
||||
B: uint8(val & 0xFF),
|
||||
A: 255,
|
||||
}
|
||||
}
|
||||
} else if len(hexStr) == 8 {
|
||||
val, err := strconv.ParseUint(hexStr, 16, 32)
|
||||
if err == nil {
|
||||
return color.NRGBA{
|
||||
R: uint8(val >> 24),
|
||||
G: uint8((val >> 16) & 0xFF),
|
||||
B: uint8((val >> 8) & 0xFF),
|
||||
A: uint8(val & 0xFF),
|
||||
}
|
||||
}
|
||||
}
|
||||
return color.NRGBA{0, 0, 0, 255}
|
||||
}
|
||||
|
||||
func adjustColor(img image.Image, brightness, contrast, saturation, gamma float64) image.Image {
|
||||
res := img
|
||||
if brightness != 0 {
|
||||
res = imaging.AdjustBrightness(res, brightness)
|
||||
}
|
||||
if contrast != 0 {
|
||||
res = imaging.AdjustContrast(res, contrast)
|
||||
}
|
||||
if saturation != 0 {
|
||||
res = imaging.AdjustSaturation(res, saturation)
|
||||
}
|
||||
if gamma > 0 && gamma != 1.0 {
|
||||
res = imaging.AdjustGamma(res, gamma)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func roundCorners(img image.Image, radius int, isCircle bool) image.Image {
|
||||
bounds := img.Bounds()
|
||||
w, h := bounds.Dx(), bounds.Dy()
|
||||
if isCircle {
|
||||
radius = w / 2
|
||||
if h/2 < radius {
|
||||
radius = h / 2
|
||||
}
|
||||
}
|
||||
if radius <= 0 {
|
||||
return img
|
||||
}
|
||||
|
||||
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
|
||||
rSq := float64(radius * radius)
|
||||
innerRSq := float64((radius - 1) * (radius - 1))
|
||||
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
c := color.NRGBAModel.Convert(img.At(bounds.Min.X+x, bounds.Min.Y+y)).(color.NRGBA)
|
||||
|
||||
var dx, dy float64
|
||||
inCorner := false
|
||||
|
||||
if x < radius && y < radius {
|
||||
dx, dy = float64(radius-x), float64(radius-y)
|
||||
inCorner = true
|
||||
} else if x >= w-radius && y < radius {
|
||||
dx, dy = float64(x-(w-radius-1)), float64(radius-y)
|
||||
inCorner = true
|
||||
} else if x < radius && y >= h-radius {
|
||||
dx, dy = float64(radius-x), float64(y-(h-radius-1))
|
||||
inCorner = true
|
||||
} else if x >= w-radius && y >= h-radius {
|
||||
dx, dy = float64(x-(w-radius-1)), float64(y-(h-radius-1))
|
||||
inCorner = true
|
||||
}
|
||||
|
||||
if inCorner {
|
||||
distSq := dx*dx + dy*dy
|
||||
if distSq > rSq {
|
||||
c.A = 0
|
||||
} else if distSq > innerRSq {
|
||||
dist := math.Sqrt(distSq)
|
||||
factor := float64(radius) - dist
|
||||
if factor < 0 {
|
||||
factor = 0
|
||||
} else if factor > 1 {
|
||||
factor = 1
|
||||
}
|
||||
c.A = uint8(float64(c.A) * factor)
|
||||
}
|
||||
}
|
||||
dst.SetNRGBA(x, y, c)
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func addBorderCanvas(img image.Image, borderWidth int, borderColorHex string, padAspect string) image.Image {
|
||||
bounds := img.Bounds()
|
||||
w, h := bounds.Dx(), bounds.Dy()
|
||||
borderColor := parseHexColor(borderColorHex)
|
||||
|
||||
newW := w + borderWidth*2
|
||||
newH := h + borderWidth*2
|
||||
|
||||
if padAspect != "" && padAspect != "none" {
|
||||
var targetRatio float64
|
||||
switch padAspect {
|
||||
case "1:1":
|
||||
targetRatio = 1.0
|
||||
case "16:9":
|
||||
targetRatio = 16.0 / 9.0
|
||||
case "9:16":
|
||||
targetRatio = 9.0 / 16.0
|
||||
case "4:5":
|
||||
targetRatio = 4.0 / 5.0
|
||||
case "4:3":
|
||||
targetRatio = 4.0 / 3.0
|
||||
}
|
||||
if targetRatio > 0 {
|
||||
curRatio := float64(newW) / float64(newH)
|
||||
if curRatio < targetRatio {
|
||||
newW = int(float64(newH) * targetRatio)
|
||||
} else if curRatio > targetRatio {
|
||||
newH = int(float64(newW) / targetRatio)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
canvas := imaging.New(newW, newH, borderColor)
|
||||
posX := (newW - w) / 2
|
||||
posY := (newH - h) / 2
|
||||
return imaging.Paste(canvas, img, image.Pt(posX, posY))
|
||||
}
|
||||
|
||||
func rotateImage(img image.Image, op string) image.Image {
|
||||
switch op {
|
||||
case "90":
|
||||
return imaging.Rotate270(img) // Python's -90 is Rotate270
|
||||
return imaging.Rotate270(img) // Clockwise 90
|
||||
case "180":
|
||||
return imaging.Rotate180(img)
|
||||
case "270":
|
||||
return imaging.Rotate90(img) // Python's 90 is Rotate90
|
||||
return imaging.Rotate90(img) // Clockwise 270
|
||||
case "FLIP_H":
|
||||
return imaging.FlipH(img)
|
||||
case "FLIP_V":
|
||||
return imaging.FlipV(img)
|
||||
default:
|
||||
if angle, err := strconv.ParseFloat(op, 64); err == nil {
|
||||
return imaging.Rotate(img, -angle, color.Transparent)
|
||||
}
|
||||
return img
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user