Go语言学习之image、image/color、image/png、image/jpeg包(the way to go)
来源:互联网 发布:石家庄软件学院 编辑:程序博客网 时间:2024/05/17 22:31
生命不止,继续 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 mainimport "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 mainimport ( "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)}
运行结果:
获取一张图片的尺寸
package mainimport ( "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)
- golang中image/jpeg包和image/png包用法
- Go语言学习之ioutil包(The way to go)
- Go语言学习之net包(The way to go)
- Go语言学习之bufio包(The way to go)
- Go语言学习之errors包(The way to go)
- Go语言学习之log包(The way to go)
- Go语言学习之runtime包(The way to go)
- Go语言学习之flag包(The way to go)
- Go语言学习之strings包(the way to go)
- Go语言学习之strconv包(The way to go)
- Go语言学习之reflect包(The way to go)
- Go语言学习之mime包(the way to go)
- Go image网站开发
- image jpeg
- Go语言学习之Hello World(The way to go)
- Go语言学习之变量(The way to go)
- Go语言学习之常量(The way to go)
- CSDN日报20170630——《降薪求职,到底该不该?》
- vim编辑器的使用总结
- 使用xcode查看和提高单元测试覆盖率
- 6 亿推送数据告诉你,我们的一天究竟在干嘛
- TensorFlow与OpenCV,读取图片,进行简单操作并显示
- Go语言学习之image、image/color、image/png、image/jpeg包(the way to go)
- chrome浏览器调试JS代码
- Bluetooth的使用
- vue.js+Echarts开发图表放大缩小功能
- android权限机制
- Hadoop Environment Setup(VM fushion. Centos7)
- AWS Security Primer
- HDU 3711 Binary Number【水题】【bitset】
- CORS简介--实现跨域调用