Skip to content

Commit

Permalink
add image
Browse files Browse the repository at this point in the history
  • Loading branch information
qwenode committed Apr 26, 2024
1 parent 254d861 commit 07e6d52
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions iimg/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package iimg

import (
"bytes"
"image"
"image/color"
"image/jpeg"
)
import (
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)

// ToRGB convert image color to rgb
func ToRGB(img image.Image) image.Image {
bounds := img.Bounds()
rgbImage := image.NewRGBA(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
originalPixel := img.At(x, y)
r, g, b, _ := originalPixel.RGBA()
rgbImage.Set(x, y, color.RGBA{
R: uint8(r >> 8),
G: uint8(g >> 8),
B: uint8(b >> 8),
A: 255,
})
}
}
return rgbImage
}

// ToJPG
func ToJPG(img image.Image, quality int) ([]byte, error) {
b := new(bytes.Buffer)
err := jpeg.Encode(b, img, &jpeg.Options{Quality: quality})
return b.Bytes(), err
}

0 comments on commit 07e6d52

Please sign in to comment.