Skip to content

Instantly share code, notes, and snippets.

@CarsonSlovoka
Created July 21, 2023 09:02
Show Gist options
  • Select an option

  • Save CarsonSlovoka/dd332e468a045242d8dfd2a4503c810b to your computer and use it in GitHub Desktop.

Select an option

Save CarsonSlovoka/dd332e468a045242d8dfd2a4503c810b to your computer and use it in GitHub Desktop.
新增畫布,並將圖片畫到該畫布上
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
func main() {
// 建立目的地圖像(RGBA)
dst := image.NewRGBA(image.Rect(0, 0, 1960, 1024))
// 在目的地圖像上繪製一個紅色的矩形
draw.Draw(dst, dst.Bounds(), &image.Uniform{color.RGBA{255, 0, 0, 128}}, image.Point{}, draw.Src)
// 開啟源圖像
srcFile, err := os.Open("good.png")
if err != nil {
panic(err)
}
defer srcFile.Close()
// 解碼源圖像
src, _, err := image.Decode(srcFile)
if err != nil {
panic(err)
}
// 在目的地圖像的指定位置(x, y)上繪製源圖像
x, y := 100, 600
draw.Draw(dst, image.Rect(x, y, x+src.Bounds().Dx(), y+src.Bounds().Dy()), src, image.Point{}, draw.Over)
// 儲存目的地圖像
dstFile, err := os.Create("destination_image.png")
if err != nil {
panic(err)
}
defer dstFile.Close()
png.Encode(dstFile, dst)
}
@CarsonSlovoka
Copy link
Copy Markdown
Author

result:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment