matlab drawthego,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的方法。

它的参数描述如下:

dst  绘图的背景图。

r 是背景图的绘图区域

src 是要绘制的图

sp 是 src 对应的绘图开始点(绘制的大小 r变量定义了)

mask 是绘图时用的蒙版,控制替换图片的方式。

mp 是绘图时蒙版开始点(绘制的大小 r变量定义了)

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

mask 蒙版是渐变

2937dd84750ee79c99d3e71f202d449c.png

给一个矩形填充颜色

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

9ca1ee9715b1a10be3137807e09a8e38.png

代码:

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)

拷贝图片的一部分

效果特效如下:

0d882f6876b2d6daf4b1fc5053b90d2d.png

相关代码:

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)

图片滚动效果

效果如下图:

c93429102e5d9d791a1a99f77dec8b36.png

假设我们需要把图片 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格式

效果图:

69a22d896b5f96b94ed4205494568f5b.png

相关代码:

1: b := src.Bounds()

2: m := image.NewRGBA(b)

3: draw.Draw(m, b, src, b.Min, draw.Src)

通过蒙版画特效

效果图

45ab28c28216bcaed39d91514564131e.png

相关代码

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: }

画一个字体

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

d125ba3580a3e226d906635144ef30f0.png

相关伪代码:

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)

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

参考:

有疑问加站长微信联系(非本文作者)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值