Created
July 21, 2023 09:02
-
-
Save CarsonSlovoka/dd332e468a045242d8dfd2a4503c810b to your computer and use it in GitHub Desktop.
新增畫布,並將圖片畫到該畫布上
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result: