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.
To install SuperImage package, you need solve the following issues:
Install Go (version 1.22.+ recommended).
Get the package using go modules:
go get -u github.com/nicolito/superimage/v2
import "github.com/nicolito128/superimage/v2"
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)
}
You have some good examples on how to use the package in the examples/
folder.
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())
}
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())
}
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())
}
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())
}
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()))
}
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)
}
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)
}
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)
}
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)
}