You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
4.2 KiB
Go
194 lines
4.2 KiB
Go
2 years ago
|
package image
|
||
|
|
||
|
import (
|
||
|
"b612.me/staros"
|
||
|
"errors"
|
||
|
"image"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
|
||
|
"github.com/golang/freetype"
|
||
|
|
||
|
"github.com/nfnt/resize"
|
||
|
|
||
|
"image/color"
|
||
|
"image/draw"
|
||
|
_ "image/gif"
|
||
|
_ "image/jpeg"
|
||
|
"image/png"
|
||
|
_ "image/png"
|
||
|
)
|
||
|
|
||
|
func OpenImage(name string) (image.Image, error) {
|
||
|
if !staros.Exists(name) {
|
||
|
return nil, errors.New("File Not Exists")
|
||
|
}
|
||
|
fso, err := os.Open(name)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
img, _, err := image.Decode(fso)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return img, nil
|
||
|
}
|
||
|
|
||
|
func MergePhoto(big, small image.Image, bigsize, smallsize uint, x, y int) image.Image {
|
||
|
big = resize.Resize(bigsize, bigsize, big, resize.Lanczos3)
|
||
|
small = resize.Resize(smallsize, smallsize, small, resize.Lanczos3)
|
||
|
offset := image.Pt(x, y)
|
||
|
b := big.Bounds()
|
||
|
nimg := image.NewRGBA(b)
|
||
|
draw.Draw(nimg, b, big, image.ZP, draw.Src)
|
||
|
draw.Draw(nimg, small.Bounds(), small, offset, draw.Over)
|
||
|
return nimg
|
||
|
}
|
||
|
|
||
|
func SavePhoto(path string, img image.Image) error {
|
||
|
imgf, err := os.Create(path)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer imgf.Close()
|
||
|
return png.Encode(imgf, img)
|
||
|
}
|
||
|
|
||
|
func SetAlpha(img image.Image, alpha uint8) image.Image {
|
||
|
size := img.Bounds()
|
||
|
nimg := image.NewRGBA(size)
|
||
|
for x := 0; x < size.Dx(); x++ {
|
||
|
for y := 0; y < size.Dy(); y++ {
|
||
|
r, g, b, a := img.At(x, y).RGBA()
|
||
|
r = r >> 8
|
||
|
g = g >> 8
|
||
|
b = b >> 8
|
||
|
a = a >> 8
|
||
|
nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), alpha})
|
||
|
//nimg.Set(x, y, color.Alpha{alpha})
|
||
|
//nimg.Set(x, y, img.At(x, y))
|
||
|
}
|
||
|
}
|
||
|
return nimg
|
||
|
}
|
||
|
|
||
|
func AddText(text string, img image.Image, x, y int, dpi, size float64, colors color.RGBA, ttf string) (image.Image, error) {
|
||
|
if !staros.Exists(ttf) {
|
||
|
return nil, errors.New("File Not Exists")
|
||
|
}
|
||
|
fontbyte, err := ioutil.ReadFile(ttf)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
font, err := freetype.ParseFont(fontbyte)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
nimg, ok := img.(draw.Image)
|
||
|
if !ok {
|
||
|
size := img.Bounds()
|
||
|
nimg = image.NewRGBA(img.Bounds())
|
||
|
for x := 0; x < size.Dx(); x++ {
|
||
|
for y := 0; y < size.Dy(); y++ {
|
||
|
r, g, b, a := img.At(x, y).RGBA()
|
||
|
nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
f := freetype.NewContext()
|
||
|
f.SetDPI(dpi)
|
||
|
f.SetFontSize(size)
|
||
|
f.SetFont(font)
|
||
|
f.SetClip(nimg.Bounds())
|
||
|
f.SetDst(nimg)
|
||
|
f.SetSrc(image.NewUniform(colors))
|
||
|
_, err = f.DrawString(text, freetype.Pt(x, y))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return nimg, nil
|
||
|
}
|
||
|
|
||
|
type TextImg struct {
|
||
|
Text string
|
||
|
X int
|
||
|
Y int
|
||
|
Dpi float64
|
||
|
Size float64
|
||
|
Color color.NRGBA
|
||
|
}
|
||
|
|
||
|
type TextList struct {
|
||
|
List []TextImg
|
||
|
TTF []byte
|
||
|
}
|
||
|
|
||
|
func AddListTests(list TextList, img image.Image) (image.Image, error) {
|
||
|
font, err := freetype.ParseFont(list.TTF)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
nimg, ok := img.(draw.Image)
|
||
|
if !ok {
|
||
|
size := img.Bounds()
|
||
|
nimg = image.NewRGBA(img.Bounds())
|
||
|
for x := 0; x < size.Dx(); x++ {
|
||
|
for y := 0; y < size.Dy(); y++ {
|
||
|
r, g, b, a := img.At(x, y).RGBA()
|
||
|
nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
for _, v := range list.List {
|
||
|
f := freetype.NewContext()
|
||
|
f.SetDPI(v.Dpi)
|
||
|
f.SetFontSize(v.Size)
|
||
|
f.SetFont(font)
|
||
|
f.SetClip(nimg.Bounds())
|
||
|
f.SetDst(nimg)
|
||
|
f.SetSrc(image.NewUniform(v.Color))
|
||
|
_, err = f.DrawString(v.Text, freetype.Pt(v.X, v.Y))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
return nimg, nil
|
||
|
}
|
||
|
|
||
|
func AddTexts(text string, img image.Image, x, y int, dpi, size float64, colors color.NRGBA, ttf string) (image.Image, error) {
|
||
|
if !staros.Exists(ttf) {
|
||
|
return nil, errors.New("File Not Exists")
|
||
|
}
|
||
|
fontbyte, err := ioutil.ReadFile(ttf)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
font, err := freetype.ParseFont(fontbyte)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
nimg, ok := img.(draw.Image)
|
||
|
if !ok {
|
||
|
size := img.Bounds()
|
||
|
nimg = image.NewRGBA(img.Bounds())
|
||
|
for x := 0; x < size.Dx(); x++ {
|
||
|
for y := 0; y < size.Dy(); y++ {
|
||
|
r, g, b, a := img.At(x, y).RGBA()
|
||
|
nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
f := freetype.NewContext()
|
||
|
f.SetDPI(dpi)
|
||
|
f.SetFontSize(size)
|
||
|
f.SetFont(font)
|
||
|
f.SetClip(nimg.Bounds())
|
||
|
f.SetDst(nimg)
|
||
|
f.SetSrc(image.NewUniform(colors))
|
||
|
_, err = f.DrawString(text, freetype.Pt(x, y))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return nimg, nil
|
||
|
}
|