Go での .gif のレンダリング

PHPz
リリース: 2024-02-09 08:45:29
転載
889 人が閲覧しました

在 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
}
ログイン後にコピー

解決策

Usebufio.newwriter

リーリー ###または### リーリー

以上がGo での .gif のレンダリングの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:stackoverflow.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!