Skip to content

Instantly share code, notes, and snippets.

@lhlyu
Created March 22, 2022 07:21
Show Gist options
  • Save lhlyu/cdfae00741b1fcd4fee89a7fbdd6b59b to your computer and use it in GitHub Desktop.
Save lhlyu/cdfae00741b1fcd4fee89a7fbdd6b59b to your computer and use it in GitHub Desktop.
Go绘制马赛克图片
package main
import (
"image"
)
func main() {
// 需要被马赛克的图片
var img image.Image
// 宽
w := img.Bounds().Size().X
// 高
h := img.Bounds().Size().Y
// 马赛克块的尺寸
neighbor := 30
// 最终的图片
m := image.NewRGBA(image.Rect(0, 0, w, h))
// 绘制马赛克图片
for i := 0; i < w; i += neighbor {
for j := 0; j < h; j += neighbor {
// 马赛克块的颜色
color := img.At(i, j)
// 绘制马赛克块
for x := i; x < i+neighbor; x++ {
for y := j; y < j+neighbor; y++ {
m.Set(x, y, color)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment