resigif

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 9 Imported by: 0

README

resigif

CI Pipeline Go Reference license

Animated GIF resizing library w/o cgo nor any third-party Libraries

Installation

You can install resigif with the go get command

go get -u github.com/logica0419/resigif

Quick Start

The only API of this library is Resize() function.
You can easily resize an animated GIF image by passing *gif.GIF struct and the target size.

Here's a simple example:

package main

import (
  "context"
  "image/gif"
  "os"

  "github.com/logica0419/resigif"
)

func main() {
  ctx := context.Background()

  src, err := os.Open("image.gif")
  if err != nil {
    panic(err)
  }
  defer src.Close()

  srcImg, err := gif.DecodeAll(src)
  if err != nil {
    panic(err)
  }

  width := 480
  height := 360

  dstImg, err := resigif.Resize(ctx, srcImg, width, height)
  if err != nil {
    panic(err)
  }

  dst, err := os.OpenFile("resized.gif", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  if err != nil {
    panic(err)
  }
  defer dst.Close()

  err = gif.EncodeAll(dst, dstImg)
  if err != nil {
    panic(err)
  }
}

Customization

  • Aspect Ratio Preservation
    • You can choose from Ignore or Maintain
dstImg, err := resigif.Resize(
  ctx,
  srcImg,
  width,
  height,
  resigif.WithAspectRatio(resigif.Maintain),
)

dstImg, err := resigif.Resize(
  ctx,
  srcImg,
  width,
  height,
  resigif.WithAspectRatio(resigif.Ignore),
)
  • Resizing algorithm
    • You can use you own resizing algorithm by implementing ImageResizeFunc interface and passing it to WithImageResizeFunc()
    • If you want to use golang.org/x/image/draw.Scaler, you can use FromDrawScaler() to convert it to ImageResizeFunc
dstImg, err := resigif.Resize(
  ctx,
  srcImg,
  width,
  height,
  resigif.WithImageResizeFunc(resigif.FromDrawScaler(draw.BiLinear)),
)
  • Parallelism
    • You can control the number of goroutine used for resizing by passing WithParallel()
dstImg, err := resigif.Resize(
  ctx,
  srcImg,
  width,
  height,
  resigif.WithParallel(3),
)

Documentation

Overview

Package resigif is an Animated GIF resizing library w/o cgo nor any third-party Libraries

Index

Constants

View Source
const (
	// Ignore ignores aspect ratio
	Ignore aspectRatioOption = iota
	// Maintain maintains aspect ratio
	Maintain
)

Variables

This section is empty.

Functions

func Resize

func Resize(ctx context.Context, src *gif.GIF, width, height int, opts ...Option) (*gif.GIF, error)

Resize resizes an animated GIF image

Returned error is either context error or error from ImageResizeFunc

Types

type ImageResizeFunc

type ImageResizeFunc func(src *image.NRGBA, width, height int) (dst *image.NRGBA, err error)

ImageResizeFunc resizes a single image

ImageResizeFunc can assume that src image is aligned to (0, 0) point

func FromDrawScaler

func FromDrawScaler(scaler draw.Scaler) ImageResizeFunc

FromDrawScaler converts draw.Scaler to ImageResizeFunc

draw.Interpolator / *draw.Kernel can be used as draw.Scaler

type Option

type Option func(*processor)

Option is option for GIF resizing1

func WithAspectRatio

func WithAspectRatio(aspectRatio aspectRatioOption) Option

WithAspectRatio sets aspect ratio option

default: Maintain

func WithImageResizeFunc

func WithImageResizeFunc(resizeFunc ImageResizeFunc) Option

WithImageResizeFunc sets image resize function

default: using draw.CatmullRom

func WithParallel

func WithParallel(limit int) Option

WithParallel sets limit of parallel processing threads

ignores limit if limit <= 0
default: runtime.NumCPU()

Jump to

Keyboard shortcuts

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