本文整理匯總了Golang中image/draw.Draw函數的典型用法代碼示例。如果您正苦於以下問題:Golang Draw函數的具體用法?Golang Draw怎麽用?Golang Draw使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Draw函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: waterMark
//水印
func waterMark(picBytes []byte) []byte {
// 打開水印圖並解碼
img, fileType, _ := image.Decode(bytes.NewBuffer(picBytes))
//讀取水印圖片
watermark, _ := png.Decode(bytes.NewBuffer(wm))
//原始圖界限
origin_size := img.Bounds()
//創建新圖層
canvas := image.NewNRGBA(origin_size)
//貼原始圖
draw.Draw(canvas, origin_size, img, image.ZP, draw.Src)
//貼水印圖
draw.Draw(canvas, watermark.Bounds().Add(image.Pt(origin_size.Dx()-watermark.Bounds().Dx(), origin_size.Dy()-watermark.Bounds().Dy()-4)), watermark, image.ZP, draw.Over)
//生成新圖片
buff := bytes.NewBuffer([]byte{})
switch fileType {
case "jpeg":
jpeg.Encode(buff, canvas, &jpeg.Options{95})
default:
png.Encode(buff, canvas)
}
return buff.Bytes()
}
示例2: Render
func (p *LightPane) Render() (*image.RGBA, error) {
canvas := image.NewRGBA(image.Rect(0, 0, width, height))
if p.colorMode {
c := colorful.Hsv(p.airWheelState*360, 1, 1)
draw.Draw(canvas, canvas.Bounds(), &image.Uniform{color.RGBA{uint8(c.R * 255), uint8(c.G * 255), uint8(c.B * 255), 255}}, image.ZP, draw.Src)
} else {
brightness := uint8(p.airWheelState * 255)
if brightness < brightnessMinimum {
brightness = brightnessMinimum
}
draw.Draw(canvas, canvas.Bounds(), &image.Uniform{color.RGBA{brightness, brightness, brightness, 255}}, image.ZP, draw.Src)
}
var frame *image.RGBA
if p.onOffState {
frame = p.onImage.GetNextFrame()
} else {
frame = p.offImage.GetNextFrame()
}
draw.Draw(canvas, canvas.Bounds(), frame, image.ZP, draw.Over)
return canvas, nil
}
示例3: addLogo
func addLogo(profilePtr *image.Image, logo string, context appengine.Context) []byte {
profileImage := *profilePtr
destImage := image.NewRGBA(profileImage.Bounds())
draw.Draw(destImage, destImage.Bounds(), profileImage, image.ZP, draw.Src)
if logoImages, ok := THELOGOIMAGES[logo]; ok {
randi := rand.Intn(len(logoImages))
logoImage := logoImages[randi]
offset := image.Pt(5, 5)
if strings.HasPrefix(logo, "NLD-") {
offset = image.Pt(0, 0)
}
start := profileImage.Bounds().Size()
start = start.Sub(offset)
start = start.Sub(logoImage.Bounds().Size())
bounds := image.Rectangle{start, start.Add(logoImage.Bounds().Size())}
draw.Draw(destImage, bounds, logoImage, image.ZP, draw.Over)
} else {
context.Errorf("Cannot load logoimage for %s", logo)
}
buffer := new(bytes.Buffer)
err := png.Encode(buffer, destImage)
check(err, context)
return buffer.Bytes()
}
示例4: ImportOverlays
func (jw *JsonWed) ImportOverlays(wed *Wed) error {
jw.Overlays = make([]jsonWedOverlay, 0)
jw.TileIndices = make([]int, len(wed.TileIndices))
for idx, overlay := range wed.Overlays {
if overlay.Name.String() != "" {
ov := jsonWedOverlay{}
ov.Width = int(overlay.Width)
ov.Height = int(overlay.Height)
ov.Name = overlay.Name.String()
ov.Flags = int(overlay.LayerFlags)
ov.Tilemap = make([]jsonWedTilemap, len(wed.Tilemaps[idx]))
for tmIdx, tilemap := range wed.Tilemaps[idx] {
ov.Tilemap[tmIdx].Id = int(tilemap.TileIndexLookupIndex)
ov.Tilemap[tmIdx].Count = int(tilemap.TileIndexLookupCount)
ov.Tilemap[tmIdx].Alt = int(tilemap.AlternateTileIndex)
ov.Tilemap[tmIdx].Flags = int(tilemap.Flags)
ov.Tilemap[tmIdx].AnimSpeed = int(tilemap.AnimSpeed)
ov.Tilemap[tmIdx].WFlags = int(tilemap.WFlags)
}
tisFile, err := os.Open(overlay.Name.String() + ".tis")
if err != nil {
return fmt.Errorf("unable to open overlay: %s %v", overlay.Name.String(), err)
}
defer tisFile.Close()
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get working directory: %v", err)
}
tis, err := OpenTis(tisFile, overlay.Name.String(), cwd)
if err != nil {
return fmt.Errorf("unable to open tis: %v", err)
}
ov.Tis = tis
img := image.NewRGBA(image.Rect(0, 0, 64*ov.Width, 64*ov.Height))
closedimg := image.NewRGBA(image.Rect(0, 0, 64*ov.Width, 64*ov.Height))
for y := 0; y < int(ov.Height); y++ {
for x := 0; x < int(ov.Width); x++ {
tileNum := y*int(ov.Width) + x
tileImg := tis.SubImage(tileNum)
draw.Draw(img, image.Rect(x*64, y*64, x*64+64, y*64+64), tileImg, image.Pt(0, 0), draw.Src)
if ov.Tilemap[tileNum].Alt != -1 {
tileImg = tis.SubImage(ov.Tilemap[tileNum].Alt)
draw.Draw(closedimg, image.Rect(x*64, y*64, x*64+64, y*64+64), tileImg, image.Pt(0, 0), draw.Src)
}
}
}
ov.BackgroundImg = img
ov.ClosedImage = closedimg
jw.Overlays = append(jw.Overlays, ov)
}
}
for idx, ti := range wed.TileIndices {
jw.TileIndices[idx] = int(ti)
}
return nil
}
示例5: Render
// Render renders the grid each time a value is sent on the render channel.
func Render(win wde.Window, g *grid.Grid, render chan bool) {
screen := win.Screen()
bounds := screen.Bounds()
grey := image.NewUniform(color.RGBA{R: 0xF9, G: 0xF9, B: 0xF9}) /// ### todo ### A: 0xFF?
for {
<-render
// Draw grid background.
draw.Draw(screen, bounds, grey, image.ZP, draw.Src)
draw.Draw(screen, bounds, imgGrid, image.ZP, draw.Over)
// Draw grid markers.
var img image.Image
for col := 0; col < g.Width(); col++ {
for row := 0; row < g.Height(); row++ {
switch g[col][row] {
case grid.MarkO:
img = imgO
case grid.MarkX:
img = imgX
default:
continue
}
pt := getCellPt(col, row, g[col][row])
rect := image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy())
draw.Draw(screen, rect.Add(pt), img, image.ZP, draw.Over)
}
}
// Flush screen updates.
win.FlushImage(bounds)
}
}
示例6: main
func main() {
pngImgFile, err := os.Open("./2.png")
if err != nil {
fmt.Println("2.png file not found!")
os.Exit(1)
}
defer pngImgFile.Close()
imgSrc, err := png.Decode(pngImgFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
newImg := image.NewRGBA(imgSrc.Bounds())
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
draw.Draw(newImg, newImg.Bounds(), imgSrc, imgSrc.Bounds().Min, draw.Over)
jpgImgFile, err := os.Create("./draw.jpg")
if err != nil {
fmt.Println("Cannot create draw.jpg !")
fmt.Println(err)
os.Exit(1)
}
defer jpgImgFile.Close()
}
示例7: Crop
func Crop(img image.Image, width int, height int, padding int) (fimg draw.Image, err error) {
// For now assume top left is bg color
// future maybe avg outer rows of pixels
bgcolor := img.At(img.Bounds().Min.X, img.Bounds().Min.Y)
logoh, logow := height-2*padding, width-2*padding
logorat := float32(logow) / float32(logoh)
interior := findLogo(img, bgcolor)
interior.Max = interior.Max.Add(image.Pt(1, 1))
center := func(rect image.Rectangle) image.Point {
return image.Point{(rect.Max.X - rect.Min.X) / 2, (rect.Max.Y - rect.Min.Y) / 2}
}
fimg = image.NewRGBA(image.Rect(0, 0, width, height))
rc := center(fimg.Bounds())
origrat := float32(interior.Dx()) / float32(interior.Dy())
if logorat > origrat {
logow = int(origrat * float32(logoh))
} else {
logoh = int(float32(logow) / origrat)
}
logoimg := Resize(img, interior, logow, logoh)
logorect := image.Rect(0, 0, logoimg.Bounds().Dx(), logoimg.Bounds().Dy())
logorect = logorect.Add(rc.Sub(image.Pt(logorect.Dx()/2, logorect.Dy()/2)))
draw.Draw(fimg, fimg.Bounds(), &image.Uniform{bgcolor}, image.ZP, draw.Src)
draw.Draw(fimg, logorect, logoimg, image.ZP, draw.Src)
return
}
示例8: DrawLogo
func DrawLogo(qrPng []byte, logo string, logoBgColor color.Color) []byte {
if logo != "" {
// println(logo)
resp, err := http.Get(logo)
if err != nil {
fmt.Printf("Logo Error=%s\n", err.Error())
// println(resp)
} else {
logoImg, _, _ := image.Decode(resp.Body)
qrImg, _, _ := image.Decode(bytes.NewReader(qrPng))
pb := qrImg.Bounds()
logoImg = resize.Resize(uint(pb.Dx()/4), uint(pb.Dy()/4), logoImg, resize.Lanczos3)
logoBgImg := image.NewRGBA(image.Rect(0, 0, logoImg.Bounds().Dx()+2, logoImg.Bounds().Dy()+2))
draw.Draw(logoBgImg, logoBgImg.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
draw.Draw(logoBgImg, logoImg.Bounds(), logoImg, image.Point{-1, -1}, draw.Over)
offsetX := (pb.Dx() - logoBgImg.Bounds().Dx()) / 2
newImg := image.NewRGBA(pb)
draw.Draw(newImg, pb, qrImg, image.Point{}, draw.Src)
draw.Draw(newImg, newImg.Bounds(), logoBgImg, image.Point{-offsetX, -offsetX}, draw.Over)
// println(logoImg)
buf := new(bytes.Buffer)
err = png.Encode(buf, newImg)
// send_s3 := buf.Bytes()
return buf.Bytes()
}
}
return qrPng
}
示例9: main
func main() {
blocks := 10
blockw := 40
space := 5
rand.Seed(time.Now().UTC().UnixNano())
img := image.NewRGBA(image.Rect(0, 0, blocks*blockw+space*(blocks-1), 4*(blockw+space)))
for i := 0; i < blocks; i++ {
warm := colorful.WarmColor()
fwarm := colorful.FastWarmColor()
happy := colorful.HappyColor()
fhappy := colorful.FastHappyColor()
draw.Draw(img, image.Rect(i*(blockw+space), 0, i*(blockw+space)+blockw, blockw), &image.Uniform{warm}, image.ZP, draw.Src)
draw.Draw(img, image.Rect(i*(blockw+space), blockw+space, i*(blockw+space)+blockw, 2*blockw+space), &image.Uniform{fwarm}, image.ZP, draw.Src)
draw.Draw(img, image.Rect(i*(blockw+space), 2*blockw+3*space, i*(blockw+space)+blockw, 3*blockw+3*space), &image.Uniform{happy}, image.ZP, draw.Src)
draw.Draw(img, image.Rect(i*(blockw+space), 3*blockw+4*space, i*(blockw+space)+blockw, 4*blockw+4*space), &image.Uniform{fhappy}, image.ZP, draw.Src)
}
toimg, err := os.Create("colorgens.png")
if err != nil {
fmt.Printf("Error: %v", err)
return
}
defer toimg.Close()
png.Encode(toimg, img)
}
示例10: hor
func hor(code barcode.Barcode, scale int) image.Image {
b := NewBitmap(code)
img := image.NewRGBA(image.Rect(0, 0, b.height*scale, b.width*scale))
draw.Draw(img, img.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
patterns := [][][]int{
{{any, set, any},
{set, skp, set},
{any, set, any}}}
for _, pattern := range patterns {
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
if Match(b, pattern, x, y) {
Draw(img, b, pattern, x, y, scale)
}
}
}
}
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
if b.Get(x, y) {
pixel := image.Rect(x*scale, y*scale, (x+1)*scale, (y+1)*scale)
draw.Draw(img, pixel, &image.Uniform{black}, image.ZP, draw.Src)
}
}
}
return img
}
示例11: DrawFrame
func (w *Window) DrawFrame(frame int) {
var f Frame
var err os.Error
if frame >= w.FrameCount {
return
}
f = w.Frames[frame]
if f.img == nil {
f.img, err = getImage(f.Path)
if err != nil {
return
}
}
draw.Draw(w.Screen, f.img.Bounds(), f.img, image.Point{0, 0}, draw.Over)
if w.State == WORKING {
draw.Draw(w.Screen, image.Rectangle{image.Point{0, 0}, image.Point{20, 20}},
image.NewUniform(color.RGBA{0, 255, 0, 255}), image.Point{0, 0}, draw.Src)
}
DrawCircle(w.Screen, f.Centre.Centre, f.Centre.Radius, color.RGBA{0, 255, 255, 255})
w.Window.FlushImage()
}
示例12: GetCube
// Sets skin.Processed to an isometric render of the head from a top-left angle (showing 3 sides).
func (skin *mcSkin) GetCube(width int) error {
// Crop out the top of the head
topFlat := imaging.Crop(skin.Image, image.Rect(8, 0, 16, 8))
// Resize appropriately, so that it fills the `width` when rotated 45 def.
topFlat = imaging.Resize(topFlat, int(float64(width)*math.Sqrt(2)/3+1), 0, imaging.NearestNeighbor)
// Create the Gift filter
filter := gift.New(
gift.Rotate(45, color.Transparent, gift.LinearInterpolation),
)
bounds := filter.Bounds(topFlat.Bounds())
top := image.NewNRGBA(bounds)
// Draw it on the filter, then smush it!
filter.Draw(top, topFlat)
top = imaging.Resize(top, width+2, width/3, imaging.NearestNeighbor)
// Skew the front and sides at 15 degree angles to match up with the
// head that has been smushed
front := skin.cropHead(skin.Image).(*image.NRGBA)
side := imaging.Crop(skin.Image, image.Rect(0, 8, 8, 16))
front = imaging.Resize(front, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
side = imaging.Resize(side, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
front = skewVertical(front, math.Pi/12)
side = skewVertical(imaging.FlipH(side), math.Pi/-12)
// Create a new image to assemble upon
skin.Processed = image.NewNRGBA(image.Rect(0, 0, width, width))
// Draw each side
draw.Draw(skin.Processed.(draw.Image), image.Rect(0, width/6, width/2, width), side, image.Pt(0, 0), draw.Src)
draw.Draw(skin.Processed.(draw.Image), image.Rect(width/2, width/6, width, width), front, image.Pt(0, 0), draw.Src)
// Draw the top we created
draw.Draw(skin.Processed.(draw.Image), image.Rect(-1, 0, width+1, width/3), top, image.Pt(0, 0), draw.Over)
return nil
}
示例13: createDigitalImage
func createDigitalImage(imagePath string) {
//Create an image object with some dimensions
img := image.NewRGBA(image.Rect(0, 0, imageWidth, imageHeight))
//Define colors used in the digital examples
white, _ := colorful.Hex("#ffffff")
red, _ := colorful.Hex("#ff0000")
//Add a white background.
draw.Draw(img, image.Rect(0, 0, imageWidth, imageHeight), &image.Uniform{white}, image.ZP, draw.Src)
//Add some red blobs.
draw.Draw(img, image.Rect(50, 50, 55, 55), &image.Uniform{red}, image.ZP, draw.Src)
//draw.Draw(img, image.Rect(550, 50, 600, 100), &image.Uniform{red}, image.ZP, draw.Src)
//draw.Draw(img, image.Rect(50, 300, 100, 350), &image.Uniform{red}, image.ZP, draw.Src)
//draw.Draw(img, image.Rect(300, 200, 400, 300), &image.Uniform{red}, image.ZP, draw.Src)
toimg, err := os.Create("digitalImage.png")
if err != nil {
fmt.Printf("Error: %v", err)
return
}
defer toimg.Close()
png.Encode(toimg, img)
}
示例14: RenderMultiline
func (c *Context) RenderMultiline(txt []string, size float64, bg, fg color.Color) (*image.RGBA, error) {
w, h := 0, 0
imgs := []*image.RGBA{}
for _, l := range txt {
i, err := c.Render(l, size, fg)
if err != nil {
return nil, err
}
if i.Bounds().Dx() > w {
w = i.Bounds().Dx()
}
h += i.Bounds().Dy()
imgs = append(imgs, i)
}
dst := image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(dst, dst.Bounds(), image.NewUniform(bg), image.ZP, draw.Src)
y := 0
for _, src := range imgs {
sr := src.Bounds()
dp := image.Point{0, y}
r := image.Rectangle{dp, dp.Add(sr.Size())}
draw.Draw(dst, r, src, sr.Min, draw.Src)
y += sr.Dy()
}
return dst, nil
}
示例15: makePngShield
func makePngShield(w io.Writer, d Data) {
// render text to determine how wide the image has to be
// we leave 6 pixels at the start and end, and 3 for each in the middle
v, vw := renderString(d.Vendor, c)
s, sw := renderString(d.Status, c)
imageWidth := op + vw + ip*2 + sw + op
img := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
draw.Draw(img, img.Bounds(), &image.Uniform{C: d.Color}, image.ZP, draw.Src)
rect := image.Rect(0, 0, op+vw+ip, h)
draw.Draw(img, rect, &image.Uniform{C: Grey}, image.ZP, draw.Src)
dst := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
mask := image.NewRGBA(image.Rect(0, 0, imageWidth, h))
buildMask(mask, imageWidth, edge, draw.Src)
draw.DrawMask(dst, dst.Bounds(), img, image.ZP, mask, image.ZP, draw.Over)
buildMask(dst, imageWidth, gradient, draw.Over)
draw.Draw(dst, dst.Bounds(), v, image.Point{-op, 0}, draw.Over)
draw.Draw(dst, dst.Bounds(), s, image.Point{-(op + vw + ip*2), 0}, draw.Over)
png.Encode(w, dst)
}