brille

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 5 Imported by: 0

README

Brille Image Processing

Brille is an image processing module for Golang

Works with JPEG and PNG images

It does not have any dependencies, only standard Golang libraries are being used

Demo: https://images.dyum.in

Installation

Minimal required Golang version: v1.18

go get github.com/julyskies/brille@latest

Usage

All of the Brille filter functions return 3 values:

  • processed file as io.Reader
  • file format as string
  • processing error

Basic example:

package main

import (
	"io"
	"log"
	"os"

	"github.com/julyskies/brille"
)

func main() {
	file, fileError := os.Open("/path/to/file.jpeg")
	if fileError != nil {
		log.Fatal(fileError)
	}
	defer file.Close()

	sobel, format, processingError := brille.SobelFilter(file)
	if processingError != nil {
		log.Fatal(processingError)
	}

	outputFile, outputError := os.Create("sobel." + format)
	if outputError != nil {
		log.Fatal(outputError)
	}
	defer outputFile.Close()

	_, copyError := io.Copy(outputFile, sobel)
	if copyError != nil {
		log.Fatal(copyError)
	}
	
	log.Println("processed " + format)
}

Using with Fiber:

package apis

import (
	"strings"

	"github.com/gofiber/fiber/v2"
	"github.com/julyskies/brille"
)

func controller(context *fiber.Ctx) error {
	file, fileError := context.FormFile("image")
	if fileError != nil {
		return fiber.NewError(fiber.StatusInternalServerError)
	}

	partials := strings.Split(file.Filename, ".")
	extension := partials[len(partials)-1]
	if extension == "" {
		return fiber.NewError(fiber.StatusBadRequest)
	}

	fileHandle, readerError := file.Open()
	if readerError != nil {
		return fiber.NewError(fiber.StatusInternalServerError)
	}

	result, format, processingError := brille.Grayscale(
		fileHandle,
		brille.GRAYSCALE_AVERAGE,
	)
	if processingError != nil {
		return fiber.NewError(fiber.StatusInternalServerError)
	}

	context.Set("Content-Type", "image/"+format)
	return context.SendStream(result)
}

Full Fiber example is available at https://github.com/peterdee/filtering-backend

Available filters

  • Binary: convert an image to 1 bit black and white. Requires a threshold value (from 0 to 255):

    binary, format, processingError := brille.Binary(file, 155)
    
  • Box blur: blur the image. Requires blur amount to be provided. Blur amount is a uint value. Box blur function will automatically reduce the amount value if it is too big, maximum amount is min(width, height) / 2:

    blurred, format, processingError := brille.BoxBlur(file, 5)
    
  • Brightness: adjust image bightness. Requires brightness amount to be provided. Brightness amount ranges from -255 (darker) to 255 (brighter):

    brightened, format, processingError := brille.Brightness(file, 107)
    
  • Color inversion: invert image colors.

    inverted, format, processingError := brille.ColorInversion(file)
    
  • Contrast: adjust image contrast. Requires contrast amount to be provided. Contrast amount ranges from -255 (less contrast) to 255 (more contrast):

    contrastless, format, processingError := brille.Contrast(file, -40)
    
  • Eight colors: this filter leaves only eight colors present on the image (red, green, blue, yellow, cyan, magenta, white, black).

    indexedColors, format, processingError := brille.EightColors(file)
    
  • Emboss filter: a static edge detection filter that uses a 3x3 kernel. It can be used to outline edges on an image:

    embossed, format, processingError := brille.EmbossFilter(file)
    
  • Flip horizontal: flip image horizontally, basically reflect the image in X axis.

    flippedX, format, processingError := brille.FlipHorizontal(file)
    
  • Flip vertical: flip image vertically, basically reflect the image in Y axis.

    flippedY, format, processingError := brille.FlipVertical(file)
    
  • Gamma correction: image gamma correction. Requires correction amount to be provided. Correction amount ranges from 0 to 3.99 (float64). By default image gamma equals to 1, so providing a value less than 1 makes colors more intense, and values more than 1 decrease color intensity:

    corrected, format, processingError := brille.GammaCorrection(file, 2.05)
    
  • Grayscale: turn colors into shades of gray. Requires grayscale type to be provided. There are 2 grayscale types available: average (or mean) and luminocity (or weighted). Both types are available as constants in brille module:

    grayAverage, format, processingError := brille.Grayscale(
      file,
      brille.GRAYSCALE_AVERAGE,
    )
    
    grayLuminocity, format, processingError := brille.Grayscale(
      file,
      brille.GRAYSCALE_LUMINOCITY,
    )
    
  • Hue rotation: rotate image hue to change the colors. Requires an angle to be provided. Angle represents degree of a hue rotation, can be any int number:

    rotated, format, processingError := brille.HueRotate(file, 278)
    
  • Kuwahara filter: an edge detection filter with dynamic aperture size. Requires aperture size to be provided, but due to the perfomance reasons maximum aperture size is limited to 40. This filter is very slow, and will probably be optimized in the future:

    kuwahara, format, processingError := brille.KuwaharaFilter(file, 9)
    
  • Laplasian filter: a static edge detection filter that uses a 3x3 kernel. It can be used to outline edges on an image:

    laplasian, format, processingError := brille.LaplasianFilter(file)
    
  • Rotate image (fixed angle): rotate an image. Available fixed angeles are 90, 180 and 270 degrees (clockwise):

    rotated90, format, processingError := brille.Rotate90(file)
    
    rotated180, format, processingError := brille.Rotate180(file)
    
    rotated270, format, processingError := brille.Rotate270(file)
    
  • Sepia: sepia color filter.

    sepia, format, processingError := brille.Sepia(file)
    
  • Sharpen filter: image sharpening. Requires an ammount to be provided. Effect amount ranges from 0 to 100:

    sharpen, format, processingError := brille.SharpenFilter(file, 77)
    
  • Sobel filter: a static edge detection filter that uses a 3x3 kernel. It can be used to outline edges on an image:

    sobel, format, processingError := brille.SobelFilter(file)
    
  • Solarize: solarization affects image colors, partially inversing the colors. Requires a threshold to be provided. Threshold ranges from 0 to 255:

    solarized, format, processingError := brille.Solarize(file, 99)
    

Environment variables

  • BRILLE_JPEG_QUALITY (int) - controls output quality for JPEG images, should be a number from 0 (low quality) to 100 (highest quality)

License

MIT

Documentation

Index

Constants

View Source
const GRAYSCALE_AVERAGE string = constants.GRAYSCALE_AVERAGE
View Source
const GRAYSCALE_LUMINOCITY string = constants.GRAYSCALE_LUMINOCITY

Variables

This section is empty.

Functions

func Binary

func Binary(file io.Reader, threshold uint) (io.Reader, string, error)

threshold: 0 to 255

func BoxBlur added in v1.0.0

func BoxBlur(file io.Reader, amount uint) (io.Reader, string, error)

max amount: (min(width, height) / 2)

func Brightness added in v1.0.0

func Brightness(file io.Reader, amount int) (io.Reader, string, error)

amount: from -255 to 255

func ColorInversion

func ColorInversion(file io.Reader) (io.Reader, string, error)

func Contrast added in v1.0.0

func Contrast(file io.Reader, amount int) (io.Reader, string, error)

amount: from -255 to 255

func EightColors added in v1.0.0

func EightColors(file io.Reader) (io.Reader, string, error)

func EmbossFilter added in v1.0.0

func EmbossFilter(file io.Reader) (io.Reader, string, error)

func FlipHorizontal added in v1.0.0

func FlipHorizontal(file io.Reader) (io.Reader, string, error)

func FlipVertical added in v1.0.0

func FlipVertical(file io.Reader) (io.Reader, string, error)

func GammaCorrection added in v1.0.0

func GammaCorrection(file io.Reader, amount float64) (io.Reader, string, error)

amount: from 0 to 3.99

func Grayscale

func Grayscale(file io.Reader, grayscaleType string) (io.Reader, string, error)

type: average or luminocity

func HueRotate added in v1.0.1

func HueRotate(file io.Reader, angle int) (io.Reader, string, error)

angle: any int value

func KuwaharaFilter added in v1.0.2

func KuwaharaFilter(file io.Reader, aperture uint) (io.Reader, string, error)

aperture: 0 to 40

func LaplasianFilter added in v1.0.0

func LaplasianFilter(file io.Reader) (io.Reader, string, error)

func Rotate180 added in v1.0.0

func Rotate180(file io.Reader) (io.Reader, string, error)

func Rotate270 added in v1.0.0

func Rotate270(file io.Reader) (io.Reader, string, error)

func Rotate90 added in v1.0.0

func Rotate90(file io.Reader) (io.Reader, string, error)

func Sepia added in v1.0.0

func Sepia(file io.Reader) (io.Reader, string, error)

func SharpenFilter added in v1.0.2

func SharpenFilter(file io.Reader, amount uint) (io.Reader, string, error)

amount: 0 to 100

func SobelFilter added in v1.0.0

func SobelFilter(file io.Reader) (io.Reader, string, error)

func Solarize added in v1.0.0

func Solarize(file io.Reader, threshold uint) (io.Reader, string, error)

threshold: 0 to 255

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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