easygif

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2024 License: Apache-2.0 Imports: 17 Imported by: 1

README

EasyGif

Easy creation of screenshots and GIFs with Golang.

Go Reference Go CI Go Report Card Coverage Badge

Install

go get github.com/GaryBrownEEngr/easygif

Easy Screenshots

package main

import "github.com/GaryBrownEEngr/easygif"

func main() {
	img, _ := easygif.Screenshot()
	_ = easygif.SaveImageToPNG(img, "./examples/screenshot/screenshot.png")

	// trimmed
	img, _ = easygif.ScreenshotTrimmed(200, 200, 200, 600)
	_ = easygif.SaveImageToPNG(img, "./examples/screenshot/screenshotTrimmed.png")
}

Easy GIF Creation

This package layers on top of the built-in golang image/gif package. The goal is to remove all the complexity of making a GIF with go. Once you have a slice of images then only a single function call is required to generate and write your GIF.

package main

import (
	"time"

	"github.com/GaryBrownEEngr/easygif"
)

func main() {
	// Collect screenshots of either the entire screen or a trimmed section of it.
	//frames, _ := easygif.ScreenshotVideo(50, time.Millisecond*100)
	frames, _ := easygif.ScreenshotVideoTrimmed(50, time.Millisecond*100, 200, 10, 50, 400)

	// Create a GIF using the Plan9 color palette and nearest color approximation.
	easygif.NearestWrite(frames, time.Millisecond*100, "easy.gif")

	// Use dithering for better colors, but adds noise
	easygif.DitheredWrite(frames, time.Millisecond*100, "easyDithered.gif")

	// Use the 256 most common colors found in the frames
	easygif.MostCommonColorsWrite(frames, time.Millisecond*100, "mostCommonColors.gif")
}

easygif.Nearest

GIF made by golang easygif - One does not simply make a gif - using nearest Plan9 color

easygif.Dithered

GIF made by golang easygif - One does not simply make a gif - using dithering

easygif.MostCommonColors

GIF made by golang easygif - One does not simply make a gif - using the 256 most common colors

Screenshots taken from here.

I would recommend using easygif.MostCommonColors. Happy GIFing.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Black to white
	Black     color.RGBA = color.RGBA{0x00, 0x00, 0x00, 0xFF} // #000000
	DarkGray  color.RGBA = color.RGBA{0x26, 0x26, 0x26, 0xFF} // #262626
	Gray      color.RGBA = color.RGBA{0x80, 0x80, 0x80, 0xFF} // #808080
	LightGray color.RGBA = color.RGBA{0xD3, 0xD3, 0xD3, 0xFF} // #D3D3D3
	White     color.RGBA = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} // #FFFFFF

	// Primary Colors
	Red  color.RGBA = color.RGBA{0xFF, 0x00, 0x00, 0xFF} // #FF0000
	Lime color.RGBA = color.RGBA{0x00, 0xFF, 0x00, 0xFF} // #00FF00
	Blue color.RGBA = color.RGBA{0x00, 0x00, 0xFF, 0xFF} // #0000FF

	// half strength primary colors
	Maroon   color.RGBA = color.RGBA{0x80, 0x00, 0x00, 0xFF} // #800000
	Green    color.RGBA = color.RGBA{0x00, 0x80, 0x00, 0xFF} // #008000
	NavyBlue color.RGBA = color.RGBA{0x00, 0x00, 0x80, 0xFF} // #000080

	// full strength primary mixes
	Yellow  color.RGBA = color.RGBA{0xFF, 0xFF, 0x00, 0xFF} // #FFFF00
	Aqua    color.RGBA = color.RGBA{0x00, 0xFF, 0xFF, 0xFF} // #00FFFF
	Cyan               = Aqua                               // #00FFFF
	Magenta color.RGBA = color.RGBA{0xFF, 0x00, 0xFF, 0xFF} // #FF00FF
	Fuchsia            = Magenta                            // #FF00FF

	// half strength primary mixes
	Olive  color.RGBA = color.RGBA{0x80, 0x80, 0x00, 0xFF} // #808000
	Purple color.RGBA = color.RGBA{0x80, 0x00, 0x80, 0xFF} // #800080
	Teal   color.RGBA = color.RGBA{0x00, 0x80, 0x80, 0xFF} // #008080

	// Other interesting colors
	Orange      color.RGBA = color.RGBA{0xFF, 0xA5, 0x00, 0xFF} // #FFA500
	Indigo      color.RGBA = color.RGBA{0x4B, 0x00, 0x82, 0xFF} // #4B0082
	Violet      color.RGBA = color.RGBA{0xEE, 0x82, 0xEE, 0xFF} // #EE82EE
	Gold        color.RGBA = color.RGBA{0xFF, 0xD7, 0x00, 0xFF} // #FFD700
	SkyBlue     color.RGBA = color.RGBA{0x87, 0xCE, 0xEB, 0xFF} // #87CEEB
	SaddleBrown color.RGBA = color.RGBA{0x8B, 0x45, 0x13, 0xFF} // #8B4513
	Tan         color.RGBA = color.RGBA{0xD2, 0xB4, 0x8C, 0xFF} // #D2B48C
	Crimson     color.RGBA = color.RGBA{0xDC, 0x14, 0x3C, 0xFF} // #DC143C
	Pink        color.RGBA = color.RGBA{0xFF, 0xC0, 0xCB, 0xFF} // #FFC0CB
	Salmon      color.RGBA = color.RGBA{0xFA, 0x80, 0x72, 0xFF} // #FA8072
	Turquoise   color.RGBA = color.RGBA{0x40, 0xE0, 0xD0, 0xFF} // #40E0D0
)

https://www.w3schools.com/colors/colors_names.asp

Functions

func Dithered added in v0.0.4

func Dithered(
	frames []image.Image,
	timeBetweenFrames time.Duration,
) *gif.GIF

uses draw.FloydSteinberg.Draw() and the Plan9 Palette Much slower, but is able to approximate colors much better that just a nearest fit match.

func DitheredOptions added in v0.0.4

func DitheredOptions(
	frames []image.Image,
	timeBetweenFrames time.Duration,
	colorPalette color.Palette,
) *gif.GIF

uses draw.FloydSteinberg.Draw() Much slower, but is able to approximate colors much better that just a nearest fit match.

func DitheredWrite added in v0.0.4

func DitheredWrite(
	frames []image.Image,
	timeBetweenFrames time.Duration,
	outputGifFilePath string,
) error

uses draw.FloydSteinberg.Draw() and the Plan9 Palette Much slower, but is able to approximate colors much better that just a nearest fit match.

func FindMostCommonColors added in v0.0.3

func FindMostCommonColors(frames []image.Image) []color.Color

func Lerp

func Lerp[T constraints.Integer | constraints.Float](a, b T, ratio float64) T

Linearly interpolate between any two numbers of the same type using a ratio. The ratio is capped between 0 and 1

func LerpColor

func LerpColor(a, b color.RGBA, ratio float64) color.RGBA

Creates a color between the given a and b. 0 means a is given, 1 means b is given, .5 is a color half way between. The ratio is capped between 0 and 1 Currently the function floors the number instead of rounding to nearest.

func LoadFramesToFile added in v0.0.4

func LoadFramesToFile(filePath string) ([]image.Image, error)

func MostCommonColors added in v0.0.4

func MostCommonColors(
	frames []image.Image,
	timeBetweenFrames time.Duration,
) *gif.GIF

func MostCommonColorsWrite added in v0.0.4

func MostCommonColorsWrite(
	frames []image.Image,
	timeBetweenFrames time.Duration,
	outputGifFilePath string,
) error

func Nearest added in v0.0.4

func Nearest(
	frames []image.Image,
	timeBetweenFrames time.Duration,
) *gif.GIF

Create a GIF with the Plan9 palette of colors. If an exact match is not found, use the nearest available color.

func NearestOptions added in v0.0.4

func NearestOptions(
	frames []image.Image,
	timeBetweenFrames time.Duration,
	colorPalette color.Palette,
) *gif.GIF

Create a GIF with the given palette of colors. If an exact match is not found, use the nearest available color.

func NearestWrite added in v0.0.4

func NearestWrite(
	frames []image.Image,
	timeBetweenFrames time.Duration,
	outputGifFilePath string,
) error

Create a GIF with the Plan9 palette of colors. If an exact match is not found, use the nearest available color.

func SaveFramesToFile added in v0.0.4

func SaveFramesToFile(frames []image.Image, filePath string) error

func SaveImageToJPEG added in v0.0.4

func SaveImageToJPEG(img image.Image, outputPath string) error

func SaveImageToPNG

func SaveImageToPNG(img image.Image, outputPath string) error

func Screenshot

func Screenshot() (image.Image, error)

func ScreenshotTrimmed

func ScreenshotTrimmed(leftTrim, rightTrim, topTrim, bottomTrim int) (image.Image, error)

func ScreenshotVideo

func ScreenshotVideo(
	frameCount int,
	delayBetweenScreenshots time.Duration,
) ([]image.Image, error)

func ScreenshotVideoTrimmed

func ScreenshotVideoTrimmed(
	frameCount int,
	delayBetweenScreenshots time.Duration,
	leftTrim, rightTrim, topTrim, bottomTrim int,
) ([]image.Image, error)

Types

This section is empty.

Directories

Path Synopsis
examples
gif

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL