superimage

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

SuperImage

The package provides some useful structures and functions for working with images. Apply effects such as blur or negative color to an image with a well-performing tool.

Index

Getting Started

Installation

To install SuperImage package, you need solve the following issues:

  1. Install Go (version 1.18.+ recommended).

  2. Get the package using go modules:

    go get github.com/nicolito/superimage/v2
  1. Import it in your code:
    import "github.com/nicolito128/superimage/v2"
Quick start

project/main.go:

package main

import (
    "bytes"
	"os"

    "github.com/nicolito128/superimage"
)

func main() {
    img, err := superimage.GetByURL("https://go.dev/blog/gopher/gopher.png")
    if err != nil {
        panic(err)
    }

    // Buffer for store the image data
    buf := new(bytes.Buffer)
    // Encode writes the image into the buffer
    // gopher is ".png", so options can be nil
    err = superimage.Encode(buf, img, nil, nil)
    if err != nil {
        panic(err)
    }

    // Writing the cute gopher
    os.WriteFile("gopher.png", buf.Bytes(), 0666)
}

Examples

You have some good examples on how to use the package in the examples/ folder.

References

About SuperImage

SuperImage is an Go struct, it can be used as any Image from the std image package because it's an Image composition. You can create a new SuperImage with the New(...) function.

func main() {
    rec := image.Rectangle{image.Point{0, 0}, image.Point{500, 500}}
    boringImg := image.NewRGBA(rec)
    superImg := superimage.New(boringImg, "png")

    println(superImg.Bounds())
}
Using GetByURL

Get a new SuperImage with an URL.

func main() {
    // Getting a new SuperImage with a link
    urlImg, err := superimage.GetByURL("https://awesomeurl.com/image.png")
    if err != nil {
        panic(err)
    }

    println(urlImg.Bounds())
}
Using GetByFile

Get a new SuperImage with a project file image.

func main() {
    // Getting a new SuperImage with a file
    fileImg, err := superimage.GetByFile("./folder/cool_image.jpg")
    if err != nil {
        panic(err)
    }

    println(fileImg.Bounds())
}
Using Decode

Decodes an reader on a new SuperImage.

func main() {
    file, _ := os.Open("./examples/gopher/gopher.png")
	i, err := superimage.Decode(file, "png")
    if err != nil {
        panic(err)
    }

	println(i.Bounds())
}
Using Encode

Encodes a writer on a new SuperImage.

func main() {
    fileImg, err := superimage.GetByFile("./folder/cool_image.jpg")
    if err != nil {
        panic(err)
    }

    buf := new(bytes.Buffer)
	err = superimage.Encode(buf, img, nil, nil)
	if err != nil {
		panic(err)
	}

    println(len(buf.Bytes()))
}
Using Negative

Inverts the colors of an image.

func main() {
    img, err := superimage.GetByURL("https://awesomeurl.com/image.png")
    if err != nil {
        panic(err)
    }

    // Inverting image colors
    neg := superimage.Negative(img)

    // Saving
    buf := new(bytes.Buffer)
    err = superimage.Encode(buf, neg, nil, nil)
    if err != nil {
        panic(err)
    }

    ioutil.WriteFile("./negative.png", buf.Bytes(), 0666)
}
Using Flip

Turn an image upside down.

func main() {
    img, err := superimage.GetByURL("https://awesomeurl.com/image.png")
    if err != nil {
        panic(err)
    }

    // Flipping image
    flipped := superimage.Flip(img)

    // Saving
    buf := new(bytes.Buffer)
    err = superimage.Encode(buf, flipped, nil, nil)
    if err != nil {
        panic(err)
    }

    ioutil.WriteFile("./flipped.png", buf.Bytes(), 0666)
}
Using Reflect

Reflects an image vertically.

func main() {
    img, err := superimage.GetByURL("https://awesomeurl.com/image.png")
    if err != nil {
        panic(err)
    }

    // Reflecting image
    reflect := superimage.Reflect(img)

    // Saving
    buf := new(bytes.Buffer)
    err = superimage.Encode(buf, reflect, nil, nil)
    if err != nil {
        panic(err)
    }

    ioutil.WriteFile("./reflect.png", buf.Bytes(), 0666)
}
Using Blur

Blur an image by a given radio.

func main() {
    img, err := superimage.GetByURL("https://awesomeurl.com/image.png")
    if err != nil {
        panic(err)
    }

    // Blur
    blurred, err := superimage.Blur(img, 2)
    if err != nil {
        panic(err)
    }

    // Saving
    buf := new(bytes.Buffer)
    err = superimage.Encode(buf, blurred, nil, nil)
    if err != nil {
        panic(err)
    }

    ioutil.WriteFile("./blurred.png", buf.Bytes(), 0666)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidOpacity = errors.New("opacity must be between 0 and 1")
View Source
var ErrNegativeRadio = errors.New("radio must be higher than 0")

Functions

func Encode

func Encode(w io.Writer, m image.Image, jpgOptions *jpeg.Options, gifOptions *gif.Options) error

Encode writes the Image m to the given writer in the specified format (png, jpg/jpeg, gif). If your image isn't a jpeg or gif just pass nil.

Types

type SuperImage

type SuperImage struct {
	// Image format: png, jpg, jpeg.
	Format string
	image.Image
}

SuperImage is an image.Image implementation that wraps another image.Image.

func Blur

func Blur(img image.Image, radio int) (*SuperImage, error)

Blur blurs an image by a given radio. If the radio is negative or bigger than the image's width or height, it returns an error. Radio 0 returns the original image without any change.

References: https://relate.cs.illinois.edu/course/cs357-f15/file-version/03473f64afb954c74c02e8988f518de3eddf49a4/media/00-python-numpy/Image%20Blurring.html | http://arantxa.ii.uam.es/~jms/pfcsteleco/lecturas/20081215IreneBlasco.pdf

func Decode

func Decode(r io.Reader, format string) (*SuperImage, error)

Decode decodes an image from r using the specified format (png, jpg, jpeg).

func Flip

func Flip(img image.Image) *SuperImage

Flip inverts the image horizontally returning a new *SuperImage.

func GetByFile

func GetByFile(filename string) (*SuperImage, error)

GetImageFile gets an image from a current project file.

func GetByURL

func GetByURL(link string) (*SuperImage, error)

GetImageFromURL gets an image from an URL with an http GET request.

func Negative

func Negative(img image.Image) *SuperImage

Negative inverts the colors of an image.

func New

func New(im image.Image, format string) *SuperImage

func Opacity

func Opacity(img image.Image, op float64) (*SuperImage, error)

func Reflect

func Reflect(img image.Image) *SuperImage

Reflect inverts the image vertically returning a new *SuperImage.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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