func (m *Image) WriteTo(w io.Writer) (int64, error) {
	n, err := w.Write(m.encodedPNG())
	return int64(n), err
}
WriteTo writes captcha image in PNG format into the given writer.
package captcha
import (
	"bytes"
	"image"
	"image/color"
	"image/png"
	"io"
	"math"
)
const (
	fontWidth  = 11
	fontHeight = 18
	blackChar  = 1
	stdWidth  = 240
	stdHeight = 80
	maxSkew = 0.7
	circleCount = 20
)
var font = [][]byte{
	{  
		0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
		0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
		0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0,
		0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0,
		1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
		0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0,
		0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0,
		0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
		0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
	},
	{  
		0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
		0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
		0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
		0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
		0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
		0, 0, 1, 0, 0,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1, 1, 1, 0, 0, 1, 1,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
		0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
		0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
		0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
		0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
		0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
	},
}
type Image struct {
	*image.Paletted
	numWidth  int
	numHeight int
	dotSize   int
}
var prng = &siprng{}
func randIntn(n int) int {
	return prng.Intn(n)
}
func randInt(from, to int) int {
	return prng.Intn(to+1-from) + from
}
func randFloat(from, to float64) float64 {
	return (to-from)*prng.Float64() + from
}
func randomPalette() color.Palette {
	p := make([]color.Color, circleCount+1)
	p[0] = color.RGBA{0xFF, 0xFF, 0xFF, 0x00}
	prim := color.RGBA{
		uint8(randIntn(129)),
		uint8(randIntn(129)),
		uint8(randIntn(129)),
		0xFF,
	}
	p[1] = prim
	for i := 2; i <= circleCount; i++ {
		p[i] = randomBrightness(prim, 255)
	}
	return p
}
func NewImage(digits []byte, width, height int) *Image {
	m := new(Image)
	m.Paletted = image.NewPaletted(image.Rect(0, 0, width, height), randomPalette())
	m.calculateSizes(width, height, len(digits))
	maxx := width - (m.numWidth+m.dotSize)*len(digits) - m.dotSize
	maxy := height - m.numHeight - m.dotSize*2
	var border int
	if width > height {
		border = height / 5
	} else {
		border = width / 5
	}
	x := randInt(border, maxx-border)
	y := randInt(border, maxy-border)
	for _, n := range digits {
		m.drawDigit(font[n], x, y)
		x += m.numWidth + m.dotSize
	}
	m.strikeThrough()
	m.distort(randFloat(5, 10), randFloat(100, 200))
	m.fillWithCircles(circleCount, m.dotSize)
	return m
}
func (m *Image) encodedPNG() []byte {
	var buf bytes.Buffer
	if err := png.Encode(&buf, m.Paletted); err != nil {
		panic(err.Error())
	}
	return buf.Bytes()
}