我有以下代码。这确实在背景图像(background.jpg)上粘贴了一个前景图像(logo.jpg) 在粘贴这个前景图像时,我想设置这个前景图像的不透明度。我使用了 DrawMask (http://golang.org/doc/articles/image_draw.html),但似乎我在这里遗漏了一些东西。请看下面的代码。有人有什么想法吗?
package main
import (
"os"
"image/draw"
"image"
"image/jpeg"
)
func main() {
//Background image
fImg1, _ := os.Open("background.jpg")
defer fImg1.Close()
img1, _, _ := image.Decode(fImg1)
//Logo to stick over background image
fImg2, _ := os.Open("logo.jpg")
defer fImg2.Close()
img2, _, _ := image.Decode(fImg2)
//Mask image (that is just a solid light gray image of same size as logo)
fmask, _ := os.Open("mask.jpg")
defer fImg2.Close()
mask, _, _ := image.Decode(fmask)
//Create a new blank image m
m := image.NewRGBA(image.Rect(0, 0, 1920, 1280))
//Paste background image over m
draw.Draw(m, m.Bounds(), img1, image.Point{0,0}, draw.Src)
//Now paste logo image over m using a mask (ref. http://golang.org/doc/articles/image_draw.html )
//******Goal is to have opacity value 50 of logo image, when we paste it****
draw.DrawMask(m, m.Bounds(), img2, image.Point{-100,-100}, mask, image.Point{-100,-100}, draw.Src)
toimg, _ := os.Create("new.jpg")
defer toimg.Close()
jpeg.Encode(toimg, m, &jpeg.Options{jpeg.DefaultQuality})
}
Sathish (http://stackoverflow.com/questions/12430874/image-manipulation-in-golang) 帮助了上述代码。谢谢萨蒂什。