ximgy

package module
v0.0.0-...-830e073 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2020 License: GPL-3.0 Imports: 12 Imported by: 0

README

ximgy

Image handling utility in Go

Example

Download with

go get github.com/ximfect/ximgy

Example script

package main

import (
	"fmt"
	"image/color"

	"github.com/ximfect/ximgy"
)

func invert(pixel ximgy.Pixel) color.RGBA {
	return color.RGBA{255 - pixel.R, 255 - pixel.G, 255 - pixel.B, pixel.A}
}

func main() {
	img, err := ximgy.Open("image.png")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("image loaded", img.Size)
	img.Iterate(invert)
	fmt.Println("image iterated")

	err = ximgy.Save(img, "out.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("image saved")
}

How to use

You can load an image with ximgy.Open:

img, err := ximgy.Open("image.png")

By default, PNG and JPEG are supported. Open returns a ximgy.Image pointer. You get individual pixels of the image by using At:

pixel := img.At(10, 20)

And you can replace pixels with Set:

newColor := color.RGBA{255, 255, 255, 255}
img.Set(15, 25, newColor)

NOTE: At and Set affect different instances of an image.RGBA. That means: if you use Set to change a pixel at x:10/y:10, At will not reflect that change.

You can save your image later using ximgy.Save, and again -- only PNG and JPEG are supported.

ximgy.Save(img, "my_new_image.png")

Another useful function the ximgy.Image provides is Iterate. It allows us to run a function on every pixel of the image, and take the function's output as the color of the pixel.

// Error handling is ignored in this example, but you should always handle your errors

func invert(pixel ximgy.Pixel) color.RGBA {
    return color.RGBA{255 - pixel.R, 255 - pixel.G, 255 - pixel.B, pixel.A}
}

func main() {
    img, _ := ximgy.Open("my_image.jpeg")
    img.Iterate(invert)
    ximgy.Save(img, "my_new_image.jpeg")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClampCoords

func ClampCoords(x, y int, size image.Point, underflow bool) (int, int)

ClampCoords ensures x and y fit within size

func GetFilenameFormat

func GetFilenameFormat(filename string) string

GetFilenameFormat returns the format from the filename

func MakeRGBA

func MakeRGBA(src image.Image) *image.RGBA

MakeRGBA returns an RGBA version of the source image

func Save

func Save(img *Image, filename string) error

Save saves an image under the given filename

Types

type Image

type Image struct {
	Size image.Point
	// contains filtered or unexported fields
}

Image represents two copies of an Image, one read-only and one write-only

func MakeEmpty

func MakeEmpty(r image.Rectangle) *Image

MakeEmpty constructs and returns an empty Image

func Open

func Open(filename string) (*Image, error)

Open opens, reads, decodes and constructs an Image

func (*Image) At

func (i *Image) At(x, y int) color.RGBA

At returns a pixel from the source image

func (*Image) GetOutput

func (i *Image) GetOutput() *image.RGBA

GetOutput returns the image that was (most likely) modified

func (*Image) Iterate

func (i *Image) Iterate(f func(Pixel) (color.RGBA, error)) error

Iterate calls the given function for every pixel in the source image, taking it's output as the new value for the pixel in the output image.

func (*Image) Set

func (i *Image) Set(x, y int, v color.RGBA)

Set modifies a pixel in the output image

func (*Image) SetSource

func (i *Image) SetSource(src *image.RGBA)

SetSource sets the input image

type Pixel

type Pixel struct {
	R uint8
	G uint8
	B uint8
	A uint8
	X int
	Y int
}

Pixel represents a pixel used in Iterate

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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