Go语言学习之image、image/color、image/png、image/jpeg包(the way to go)

时间:2021-04-25 21:23:09

生命不止,继续 go go go !!!

golang同样为我们提供了很多关于图片处理的标准库,这里与大家一起学习分享。

image package

作用:
Package image implements a basic 2-D image library.
image包实现了一个基本的2D图像库.

方法:
func NewNRGBA

func NewNRGBA(r Rectangle) *NRGBA

NewNRGBA returns a new NRGBA image with the given bounds.

什么是NRGBA?
NRGBA is an in-memory image whose At method returns color.NRGBA values.

type NRGBA struct {
Pix []uint8
Stride int
Rect Rectangle
}

func (*NRGBA) Set

func (p *NRGBA) Set(x, y int, c color.Color)

设定指定位置的color,image/color稍后会介绍。

func Rect

func Rect(x0, y0, x1, y1 int) Rectangle

Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}.

image/color package

作用:
Package color implements a basic color library.

func (NRGBA) RGBA

func (c NRGBA) RGBA() (r, g, b, a uint32)

func DecodeConfig

func DecodeConfig(r io.Reader) (Config, string, error)

DecodeConfig decodes the color model and dimensions of an image that has been encoded in a registered format.
更通俗说就是获得image的信息。

image/png package

作用:
Package png implements a PNG image decoder and encoder。

func (*Encoder) Encode

func (enc *Encoder) Encode(w io.Writer, m image.Image) error

编码png文件。

image/jpeg package

作用:
Package jpeg implements a JPEG image decoder and encoder.

func Encode

func Encode(w io.Writer, m image.Image, o *Options) error

编码JPG文件。

应用

生成图片

package main

import "image"
import "image/color"
import "image/png"
import "os"

func main() {
// Create an 100 x 50 image
img := image.NewRGBA(image.Rect(0, 0, 100, 50))

// Draw a red dot at (2, 3)
img.Set(2, 3, color.RGBA{255, 0, 0, 255})

// Save to out.png
f, _ := os.OpenFile("out.png", os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
}

运行结果,就是生成了一张png文件。

生成复杂色彩的图片

package main

import (
"fmt"
"image"
"image/color"
"image/png"
"math"
"os"
)

type Circle struct {
X, Y, R float64
}

func (c *Circle) Brightness(x, y float64) uint8 {
var dx, dy float64 = c.X - x, c.Y - y
d := math.Sqrt(dx*dx+dy*dy) / c.R
if d > 1 {
return 0
} else {
return 255
}
}

func main() {
var w, h int = 280, 240
var hw, hh float64 = float64(w / 2), float64(h / 2)
r := 40.0
θ := 2 * math.Pi / 3
cr := &Circle{hw - r*math.Sin(0), hh - r*math.Cos(0), 60}
cg := &Circle{hw - r*math.Sin(θ), hh - r*math.Cos(θ), 60}
cb := &Circle{hw - r*math.Sin(-θ), hh - r*math.Cos(-θ), 60}

m := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
c := color.RGBA{
cr.Brightness(float64(x), float64(y)),
cg.Brightness(float64(x), float64(y)),
cb.Brightness(float64(x), float64(y)),
255,
}
m.Set(x, y, c)
}
}

f, err := os.OpenFile("rgb.png", os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
png.Encode(f, m)
}

运行结果:
Go语言学习之image、image/color、image/png、image/jpeg包(the way to go)

获取一张图片的尺寸

package main

import (
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"os"
)

func main() {
width, height := getImageDimension("rgb.png")
fmt.Println("Width:", width, "Height:", height)
}

func getImageDimension(imagePath string) (int, int) {
file, err := os.Open(imagePath)
defer file.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}

image, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err)
}
return image.Width, image.Height
}

生成缩略图
借助github开源项目:
https://github.com/disintegration/imaging
文档地址:
http://godoc.org/github.com/disintegration/imaging

Go语言学习之image、image/color、image/png、image/jpeg包(the way to go)