Skip to content

gg

공식 저장소: fogleman/gg: Go Graphics - 2D rendering in Go with a simple API. (github.com)

공식 문서: gg package - github.com/fogleman/gg - Go Packages

공식 예제: gg/examples at master · fogleman/gg (github.com)

gg 는 비교적 역사가 긴 2D 그래픽 렌더링 엔진으로 이미지 생성에 적합합니다.

설치

go get -u github.com/fogleman/gg

빠른 시작

go
package main

import "github.com/fogleman/gg"

func main() {
    dc := gg.NewContext(1000, 1000) // 캔버스 생성 높이 1000 너비 1000
    dc.DrawCircle(500, 500, 400) // (500,500) 좌표 위치에 반지름 400 의 원 그리기
    dc.SetRGB(0, 0, 0) // 검은색 설정
    dc.Fill() // 채우기
    dc.SavePNG("out.png") // 이미지 파일로 저장
}

![](/images/gg/points.png =400x400)

go
func TestDot(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   for i := 1; i < 10; i++ {
      dc.DrawPoint(float64(50*i), float64(50*i), 5) // 점의 좌표와 반지름 설정
   }
   dc.Fill() // 채우기
   dc.SavePNG("out.png")
}

선 그리기

![](/images/gg/lines.png =400x400)

go
func TestLines(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   dc.SetLineWidth(5) // 선 너비 설정
   dc.DrawLine(1000, 0, 0, 1000)
   dc.DrawLine(1000, 1000, 0, 0)
   dc.Stroke() // 선 연결
   dc.SavePNG("lines.png")
}

Golang by www.golangdev.cn edit