>  기사  >  백엔드 개발  >  Go에서 .gif 렌더링

Go에서 .gif 렌더링

PHPz
PHPz앞으로
2024-02-09 08:45:29852검색

在 Go 中渲染 .gif

Go 언어에서는 .gif 파일을 렌더링하는 것이 일반적인 작업입니다. .gif 파일은 연속적인 이미지 프레임 사이를 전환하여 역동적인 효과를 나타낼 수 있는 일반적인 동적 사진 형식입니다. Go 언어에서는 일부 라이브러리를 사용하여 .gif 파일을 처리하고 렌더링하여 맞춤형 애니메이션 효과를 얻을 수 있습니다. 이 문서에서는 Go 언어로 .gif 파일을 렌더링하는 방법을 소개하고 이 기능을 더 잘 이해하고 적용하는 데 도움이 되는 몇 가지 샘플 코드와 기술을 제공합니다. 당신이 초보자이건 숙련된 개발자이건 관계없이 이 글은 유용한 참고자료와 지침을 제공할 것입니다. Go 언어로 .gif 파일을 렌더링하는 비결을 살펴보겠습니다!

질문 내용

코드 샘플을 작동시키려고 합니다. 이는 "go 프로그래밍 언어"(https://github.com/adonovan/gopl.io/blob/1ae3ec64947b7a5331b186f1b1138fc98c0f1c06/ch1/lissajous/main.go)에서 유래되었습니다. 애니메이션을 보려고 하면 GIF가 렌더링되지 않습니다. GIF 렌더링 소프트웨어 오류:

.gif 2016년부터 기준이 바뀐 걸까요, 아니면 제가 뭔가 잘못하고 있는 걸까요?

으아악

빌드 및 실행 명령은 다음과 같습니다.

// copyright © 2016 alan a. a. donovan & brian w. kernighan.
// license: https://creativecommons.org/licenses/by-nc-sa/4.0/

// lissajous generates gif animations of random lissajous figures.
package main

import (
    "image"
    "image/color"
    "image/gif"
    "io"
    "math"
    "math/rand"
    "os"
)

var palette = []color.color{color.white, color.black}

const (
    whiteindex = 0 // first color in palette
    blackindex = 1 // next color in palette
)

func main() {
    lissajous(os.stdout)
}

func lissajous(out io.writer) {
    const (
        cycles  = 5     // number of complete x oscillator revolutions
        res     = 0.001 // angular resolution
        size    = 100   // image canvas covers [-size..+size]
        nframes = 64    // number of animation frames
        delay   = 8     // delay between frames in 10ms units
    )
    freq := rand.float64() * 3.0 // relative frequency of y oscillator
    anim := gif.gif{loopcount: nframes}
    phase := 0.0 // phase difference
    for i := 0; i < nframes; i++ {
        rect := image.rect(0, 0, 2*size+1, 2*size+1)
        img := image.newpaletted(rect, palette)
        for t := 0.0; t < cycles*2*math.pi; t += res {
            x := math.sin(t)
            y := math.sin(t*freq + phase)
            img.setcolorindex(size+int(x*size+0.5), size+int(y*size+0.5),
                blackindex)
        }
        phase += 0.1
        anim.delay = append(anim.delay, delay)
        anim.image = append(anim.image, img)
    }
    gif.encodeall(out, &anim) // note: ignoring encoding errors
}

Solution

Usebufio.newwriter

으아악

또는

으아악

위 내용은 Go에서 .gif 렌더링의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 stackoverflow.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제