graphics

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: MIT Imports: 9 Imported by: 4

Documentation

Overview

manipulating images pixel by pixel

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdjustBrightness added in v0.18.0

func AdjustBrightness(img image.Image, adjustment float64) image.Image

AdjustBrightness adjusts the brightness of an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	brightImg := graphics.AdjustBrightness(img, 30)
	saveImage("brightness_example.png", brightImg)
	fmt.Println("Brightness adjusted image saved to brightness_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Brightness adjusted image saved to brightness_example.png

func AdjustContrast added in v0.18.0

func AdjustContrast(img image.Image, factor float64) image.Image

AdjustContrast adjusts the contrast of an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	contrastImg := graphics.AdjustContrast(img, 1.5)
	saveImage("contrast_example.png", contrastImg)
	fmt.Println("Contrast adjusted image saved to contrast_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Contrast adjusted image saved to contrast_example.png

func Blur added in v0.18.0

func Blur(img image.Image) image.Image

Blur applies a simple blur filter to an image

func EachPixel

func EachPixel(file *os.File, f func(uint8, uint8, uint8, uint8) (uint8, uint8, uint8, uint8)) (image.Image, error)

EachPixel applies a function to each pixel of an image

func EachPixelOfImage added in v0.15.0

func EachPixelOfImage(src image.Image, f func(uint8, uint8, uint8, uint8) (uint8, uint8, uint8, uint8)) (image.Image, error)

EachPixelOfImage applies a function to each pixel of an image

func EdgeDetect added in v0.18.0

func EdgeDetect(img image.Image) image.Image

EdgeDetect detects edges in a grayscale image.

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	edgeImg := graphics.EdgeDetect(img)
	saveImage("edge_detect_example.png", edgeImg)
	fmt.Println("Edge detected image saved to edge_detect_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Edge detected image saved to edge_detect_example.png

func Edgedetect added in v0.14.0

func Edgedetect(img image.Image) image.Image

Edgedetect detects edges in an image

func FlipHorizontal added in v0.18.0

func FlipHorizontal(img image.Image) image.Image

FlipHorizontal flips an image horizontally

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	flippedImg := graphics.FlipHorizontal(img)
	saveImage("flip_horizontal_example.png", flippedImg)
	fmt.Println("Flipped image saved to flip_horizontal_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Flipped image saved to flip_horizontal_example.png

func GaussianBlur added in v0.18.0

func GaussianBlur(img image.Image) image.Image

GaussianBlur applies a Gaussian blur to an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	blurredImg := graphics.GaussianBlur(img)
	saveImage("gaussian_blur_example.png", blurredImg)
	fmt.Println("Gaussian blurred image saved to gaussian_blur_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Gaussian blurred image saved to gaussian_blur_example.png

func GrayAt added in v0.15.0

func GrayAt(img image.Image, x, y int) uint8

GrayAt returns the grayscale value of a pixel

func Grayscale added in v0.14.0

func Grayscale(img image.Image) image.Image

Grayscale converts an image to grayscale

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	grayImg := graphics.Grayscale(img)
	saveImage("grayscale_example.png", grayImg)
	fmt.Println("Grayscale image saved to grayscale_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Grayscale image saved to grayscale_example.png

func Invert added in v0.15.0

func Invert(img image.Image) (image.Image, error)

Invert inverts an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	invertedImg, _ := graphics.Invert(img)
	saveImage("invert_example.png", invertedImg)
	fmt.Println("Inverted image saved to invert_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Inverted image saved to invert_example.png

func LoadImage added in v0.15.0

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

LoadImage loads an image from a file.

func NearestNeighbor added in v0.14.0

func NearestNeighbor(img image.Image, newWidth, newHeight int) (*image.NRGBA, error)

NearestNeighbor resizes an image using the nearest neighbor algorithm

func ResizeNearestNeighbor

func ResizeNearestNeighbor(file *os.File, newWidth, newHeight int) (*image.NRGBA, error)

ResizeNearestNeighbor resizes an image using the nearest neighbor algorithm

func Rotate90 added in v0.18.0

func Rotate90(img image.Image) image.Image

Rotate90 rotates an image 90 degrees clockwise

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	rotatedImg := graphics.Rotate90(img)
	saveImage("rotate90_example.png", rotatedImg)
	fmt.Println("Rotated image saved to rotate90_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Rotated image saved to rotate90_example.png

func SaveImage added in v0.15.0

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

SaveImage saves an image to a file.

func Sepia added in v0.18.0

func Sepia(img image.Image) image.Image

Sepia applies a sepia tone to an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	sepiaImg := graphics.Sepia(img)
	saveImage("sepia_example.png", sepiaImg)
	fmt.Println("Sepia image saved to sepia_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Sepia image saved to sepia_example.png

func Sharpen added in v0.18.0

func Sharpen(img image.Image) image.Image

Sharpen applies a sharpening filter to an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	sharpenedImg := graphics.Sharpen(img)
	saveImage("sharpen_example.png", sharpenedImg)
	fmt.Println("Sharpened image saved to sharpen_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Sharpened image saved to sharpen_example.png

func Threshold added in v0.15.0

func Threshold(img image.Image, threshold uint8) image.Image

Threshold thresholds an image

Example
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"simonwaldherr.de/go/golibs/graphics"
)

func main() {
	img := createTestImage()

	threshImg := graphics.Threshold(img, 128)
	saveImage("threshold_example.png", threshImg)
	fmt.Println("Thresholded image saved to threshold_example.png")
}

// createTestImage creates a simple test image
func createTestImage() image.Image {
	img, _ := graphics.LoadImage("img.jpg")

	return img
}

// saveImage saves an image to a file
func saveImage(filename string, img image.Image) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println("Failed to encode image:", err)
	}
}
Output:

Thresholded image saved to threshold_example.png

Types

This section is empty.

Jump to

Keyboard shortcuts

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