zoukankan      html  css  js  c++  java
  • Golang 绘图技术(image/draw包介绍)

          image/draw 包仅仅定义了一个操作:通过可选的蒙版图(mask image),把一个原始图片绘制到目标图片上,这个操作是出奇的灵活,可以优雅和高效的执行很多常见的图像处理任务。

       1: // Draw calls DrawMask with a nil mask.
       2: func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)
       3: func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point,
       4:     mask image.Image, mp image.Point, op Op)

    第一个函数Draw是没有使用蒙版mask的调用方法,它内部其实就是调用的mask为 nil的方法。

    它的参数描述如下:

     

     

    下图就是几个相关的例子:

    mask 蒙版是渐变

    go-imagedraw-package_20

    给一个矩形填充颜色

    使用 Draw方法的逻辑效果图:

    image-2a

    代码:

       1: m := image.NewRGBA(image.Rect(0, 0, 640, 480))
       2: blue := color.RGBA{0, 0, 255, 255}
       3: draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)

    拷贝图片的一部分

    效果特效如下:

    image-2b

    相关代码:

       1: r := image.Rectangle{dp, dp.Add(sr.Size())}  // 获得更换区域
       2: draw.Draw(dst, r, src, sr.Min, draw.Src)

    如果是复制整个图片,则更简单:

       1: sr = src.Bounds()         // 获取要复制图片的尺寸
       2: r := sr.Sub(sr.Min).Add(dp)   // 目标图的要剪切区域
       3: draw.Draw(dst, r, src, sr.Min, draw.Src)

    图片滚动效果

    效果如下图:

    image-2c

    假设我们需要把图片 m 上移20个像素.

    相关代码:

       1: b := m.Bounds()
       2: p := image.Pt(0, 20)
       3: // Note that even though the second argument is b,
       4: // the effective rectangle is smaller due to clipping.
       5: draw.Draw(m, b, m, b.Min.Add(p), draw.Src)
       6: dirtyRect := b.Intersect(image.Rect(b.Min.X, b.Max.Y-20, b.Max.X, b.Max.Y))

    把一个图片转成RGBA格式

    效果图:

    image-2d

    相关代码:

       1: b := src.Bounds()
       2: m := image.NewRGBA(b)
       3: draw.Draw(m, b, src, b.Min, draw.Src)

    通过蒙版画特效

    效果图

    image-2e

    相关代码

       1: type circle struct {
       2:     p image.Point
       3:     r int
       4: }
       5:  
       6: func (c *circle) ColorModel() color.Model {
       7:     return color.AlphaModel
       8: }
       9:  
      10: func (c *circle) Bounds() image.Rectangle {
      11:     return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
      12: }
      13:  
      14: func (c *circle) At(x, y int) color.Color {
      15:     xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
      16:     if xx*xx+yy*yy < rr*rr {
      17:         return color.Alpha{255}
      18:     }
      19:     return color.Alpha{0}
      20: }
      21:  
      22:  
      23: draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{p, r}, image.ZP, draw.Over)

    注意,一个image对象只需要实现下面几个就可,这也就是Go接口强大的地方.

       1: type Image interface {
       2:     // ColorModel returns the Image's color model.
       3:     ColorModel() color.Model
       4:     // Bounds returns the domain for which At can return non-zero color.
       5:     // The bounds do not necessarily contain the point (0, 0).
       6:     Bounds() Rectangle
       7:     // At returns the color of the pixel at (x, y).
       8:     // At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
       9:     // At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
      10:     At(x, y int) color.Color
      11: }

    画一个字体

    效果图,画一个蓝色背景的字体。

    image-2f

    相关伪代码:

       1: src := &image.Uniform{color.RGBA{0, 0, 255, 255}}
       2: mask := theGlyphImageForAFont()
       3: mr := theBoundsFor(glyphIndex)
       4: draw.DrawMask(dst, mr.Sub(mr.Min).Add(p), src, image.ZP, mask, mr.Min, draw.Over)

     

    上面例子完整的代码请看:

    http://golang.org/doc/progs/image_draw.go

     

    参考:

     http://blog.golang.org/go-imagedraw-package

  • 相关阅读:
    Flask-1-05-Cookie&Session
    Flask-1-05-Response
    Flask-1-04-Request
    Python3 装饰器无参有参 Egon老师视频
    python魔法方法、构造函数、序列与映射、迭代器、生成器---python基础教程
    Python3(廖雪峰教程) 链接
    Python面向对象之私有属性和方法(转载)
    python3 isinstance
    生成器 讲得不错的帖子
    Python3 pickle模块
  • 原文地址:https://www.cnblogs.com/ghj1976/p/3443638.html
Copyright © 2011-2022 走看看