I am rewriting with golang a script I originally wrote in python. The function I am trying to recreate currently takes one image (a coloured overlay) and pastes it on a background (a map) of the same size, with 50% transparency. The desired output, as produced by my python script, is:
The golang code I have written to try to replicate this in golang, with reference to this stackoverflow thread.
In main.go
// overlayImg is type image.Image, consists of the coloured overlay
overlayImg = lib.AddMap(overlayImg)
// lib.SaveToRecords is a function that saves image.Image overlayImg to path specified by string download.FileName
lib.SaveToRecords(overlayImg, download.FileName)
In package myproject/lib
func AddMap(i image.Image) image.Image{
// mapFile is the background map image
mapFile, err := os.Open("./res/map.png")
if err != nil { panic(err) }
mapImg, _, err := image.Decode(mapFile)
if err != nil { panic(err) }
mask := image.NewUniform(color.Alpha{128})
canvas := image.NewRGBA(mapImg.Bounds())
draw.Draw(canvas, canvas.Bounds(), mapImg, image.Point{0, 0}, draw.Src)
draw.DrawMask(canvas, canvas.Bounds(), i, image.Point{0, 0}, mask, image.Point{0, 0}, draw.Over)
return canvas
}
However, the resulting image is produced with a sort of 'glitch' in parts of the coloured overlay. This colour glitch is reliably reproduced on each run of the script, even with different overlays (the map stays the same throughout, of course).
How do I get rid of the 'glitch'?
Things I have tried:
draw.Drawdraw.DrawMaskdraw.Over
draw.DrawMaskdraw.Src
draw.Drawdraw.DrawMaskdraw.Src
Update
mask := image.NewUniform(color.Alpha16{32767})mask := image.NewUniform(color.Alpha{128})
Update 2
mapImgicanvasmapImg
mapImg, _, err := image.Decode(mapFile)
mapImgRGBA := image.NewRGBA(mapImg.Bounds())
draw.Draw(mapImgRGBA, mapImgRGBA.Bounds(), mapImg, image.Point{0, 0}, draw.Src)
// Use mapImgRGBA in place of mapImg from here
Still don't work: