util

package module
v0.0.0-...-472708b Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 9 Imported by: 0

README

Image-Kit-Util

Image-Kit-Util provides a simple and clean interface for image manipulation in Go. Perfect for generative art and automated image manipulation scripts.

For documentation, refer to pkg.go.dev

Supported operations:
  • Crop
  • Rotate
  • Translate
  • Sort pixels by hue
  • Quantize
  • Greyscale conversion
  • Split/Combine images
  • Add colored border

Example

package main

import (
    "github.com/svader0/Image-Kit-Util"
)

func main() {
	img, err := LoadImage("res/test_image.jpg")
	if err != nil {
		panic(err)
	}

	// Crop the image to a square
	img = Crop(img, 0, 0, 500, 500)

	// Rotate the image by 45 degrees
	img = RotateDegrees(img, 45)

	// Convert the image to grayscale
	img = ConvertToGray(img)

	// Save the image
	err = SaveImage("res/test_output.jpg", img)
	if err != nil {
		panic(err)
	}
}

Before After

TODO / Needs Work

  • Current quantize and pixelsort algorithms are SLOW! Images over 1000x1000 take significant time to render.
    • Supports KMeansQuantize but this implementation is much worse...
  • New features to add soon:
    • Drawing shapes, lines, text, etc. on image.
    • Support for image overlays
    • Hue shift
    • Range hue shift

Documentation

Index

Constants

This section is empty.

Variables

View Source
var COLOR = struct {
	RED         color.RGBA
	BLUE        color.RGBA
	BLACK       color.RGBA
	WHITE       color.RGBA
	YELLOW      color.RGBA
	ORANGE      color.RGBA
	GREEN       color.RGBA
	PURPLE      color.RGBA
	PINK        color.RGBA
	TRANSPARENT color.RGBA
}{
	RED:         color.RGBA{255, 0, 0, 255},
	BLUE:        color.RGBA{0, 0, 255, 255},
	BLACK:       color.RGBA{0, 0, 0, 255},
	WHITE:       color.RGBA{255, 255, 255, 255},
	YELLOW:      color.RGBA{255, 255, 0, 255},
	ORANGE:      color.RGBA{255, 165, 0, 255},
	GREEN:       color.RGBA{0, 255, 0, 255},
	PURPLE:      color.RGBA{128, 0, 128, 255},
	PINK:        color.RGBA{255, 192, 203, 255},
	TRANSPARENT: color.RGBA{0, 0, 0, 0},
}

Color constants

Functions

func AddBorder

func AddBorder(img image.Image, size int, borderColor color.Color) *image.RGBA

Adds a border to the given image.

func AverageColor

func AverageColor(img *image.Image) color.RGBA

func AverageRGBA

func AverageRGBA(color color.Color) uint8

Finds the average of the RGB values of a color

func CombineImages

func CombineImages(imgs *Subimages) image.Image

Takes in a subimages struct and combines them into a single image

func ConvertToGray

func ConvertToGray(input image.Image) *image.Gray

Converts an image to grayscale

func Crop

func Crop(input image.Image, x0, y0, x1, y1 int) image.Image

Returns a new image that is a sub-image of the input image. The bounds of the new image are (x0, y0, x1, y1). The point (x0, y0) is the top-left corner of the new image. The point (x1, y1) is the bottom-right corner of the new image.

func GetColorPalette

func GetColorPalette(img image.Image, numColors int) []color.RGBA

Quantizes the image to the specified number of colors using the Median-cut algorithm. Returns a list of colors that represent the color palette of the image.

func LoadImage

func LoadImage(filename string) (image.Image, error)

Loads an image from file

func Quantize

func Quantize(img image.Image, numColors int) image.Image

Quantizes the input image using the median-cut algorithm

func QuantizeKMeans

func QuantizeKMeans(input image.Image, numColors int, dither bool) image.Image

IMPORTANT NOTE! QuantizeKMeans is TERRIBLY OPTIMIZED, and NOT PERFORMANT AT ALL!! Do not use it for anything of actual importance, as it will likely take ages to run. However, the end result does look pretty cool, so I'm keeping it here for now.

func RGBToHSL

func RGBToHSL(r, g, b uint8) (float64, float64, float64)

Converts RGB values to HSL.

func Rotate

func Rotate(img image.Image, radians float64) image.Image

Rotates the given image by the specified angle in radians.

func RotateDegrees

func RotateDegrees(input image.Image, angle float64) image.Image

Rotates the input image by the specified angle in degrees.

func SaveImage

func SaveImage(filename string, img image.Image) error

Saves an image to file

func SortPixelsByHue

func SortPixelsByHue(src image.Image) image.Image

SortPixelsByHue sorts the pixels of the input image from left to right based on their hue and returns a new image.

func Translate

func Translate(input image.Image, dx, dy int) image.Image

Returns a new image that is a translated version of the input image. The image is translated by dx pixels horizontally and dy pixels vertically.

Types

type Subimages

type Subimages struct {
	Images []image.Image
	// contains filtered or unexported fields
}

Represents a group of subimages that can be combined into a single image The subimages are arranged in a grid of size x by y.

func SplitImage

func SplitImage(img image.Image, x, y int) Subimages

Splits the image into a grid of smaller images. Returns a Subimages struct containing the smaller images and the arrangement of the grid. (x by y)

func (*Subimages) X

func (s *Subimages) X() int

X returns the x value.

func (*Subimages) Y

func (s *Subimages) Y() int

Y returns the y value.

Jump to

Keyboard shortcuts

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