首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

绘图 | image/draw

  • import "image/draw"
  • 概述
  • 索引
  • 示例

概述

Package draw 提供图像合成功能。

有关此软件包的介绍,请参阅“Go image/draw 软件包”:https//golang.org/doc/articles/image_draw.html

索引

  • func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)
  • func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)
  • type Drawer
  • type Image
  • type Op
  • func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
  • type Quantizer

示例

Drawer (FloydSteinberg)

包文件

func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)

Draw 通过一个零掩码调用 DrawMask。

func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)

DrawMask 将 dst 中的 r.Min 与 src 中的 sp 和 mask 中的 mp 对齐,然后用 Porter-Duff 组合的结果替换 dst 中的矩形 r。一个无面具被视为不透明。

Drawer 包含 Draw 方法。

type Drawer interface {
        // 绘制在dst中使用sp在src中对齐r.Min然后替换
        // dst中的矩形r,在dst上绘制src的结果。
        Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
}

FloydSteinberg 是一个 Drawer,是 Floyd-Steinberg 误差扩散的 Src 操作。

var FloydSteinberg Drawer = floydSteinberg{}

示例(FloydSteinberg)

package main

import (
	"fmt"
	"image"
	"image/color"
	"image/draw"
	"math"
)

func main() {
	const width = 130
	const height = 50

	im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}})
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255
			var gray uint8
			if dist > 255 {
				gray = 255
			} else {
				gray = uint8(dist)
			}
			im.SetGray(x, y, color.Gray{Y: 255 - gray})
		}
	}
	pi := image.NewPaletted(im.Bounds(), []color.Color{
		color.Gray{Y: 255},
		color.Gray{Y: 160},
		color.Gray{Y: 70},
		color.Gray{Y: 35},
		color.Gray{Y: 0},
	})

	draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP)
	shade := []string{" ", "░", "▒", "▓", "█"}
	for i, p := range pi.Pix {
		fmt.Print(shade[p])
		if (i+1)%width == 0 {
			fmt.Print("\n")
		}
	}
}

Image 是一个 image.Image,使用 Set 方法更改单个像素。

type Image interface {
        image.Image
        Set(x, y int, c color.Color)
}

Op 是 Porter-Duff 合成操作员。

type Op int
const (
        // Over在dst''上指定``(掩码中的src)。
        Over Op = iota
        // Src指定``src in mask''。
        Src
)

func (Op) Draw(查看源代码)

func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)

Draw 通过调用 Drawer 接口操作来实现Draw 界面。

type Quantizer(查看源代码)

Quantizer 为图像生成调色板。

type Quantizer interface {
        // Quantize将cap(p) - len(p)颜色附加到p并返回
        // 适用于将m转换为调色板图像的更新调色板。
        Quantize(p color.Palette, m image.Image) color.Palette
}

扫码关注腾讯云开发者

领取腾讯云代金券