mergi: github.com/noelyahan/mergi Index | Examples | Files | Directories

package mergi

import "github.com/noelyahan/mergi"

Package mergi implements a image manipulation library mainly focusing on merge. See http://mergi.io for more information about mergi.

Mergi library supports

Merge
Crop
Resize
Watermark
Animate
Import
Export

Index

Examples

Package Files

animate.go crop.go doc.go image.go merge.go resize.go watermark.go

func Animate Uses

func Animate(imgs []image.Image, delay int) (gif.GIF, error)

Animate uses go standard image.Image type array and delay input to returns a gif animation

this type can be exported via mergi.Export function

for more animate examples https://github.com/noelyahan/mergi/examples/animate

This example shows how to use the mergi Animate Mergi provides the simplified api to animate any given image array result

Code:

// Get the image content by passing image path url or file path
img1, err := mergi.Import(loader.NewFileImporter("testdata/avocado-3210885_960_720.jpg"))
if err != nil {
    log.Fatalf("failed to load via local: %s", err)
}
img2, err := mergi.Import(loader.NewFileImporter("testdata/cherry-3074284_960_720.jpg"))
if err != nil {
    log.Fatalf("failed to load via local: %s", err)
}

imgFrames := []image.Image{img1, img2}
// Image array and delay should be passed here
animation, err := mergi.Animate(imgFrames, 20)
if err != nil {
    log.Fatalf("failed to create animation: %s", err)
}

// Export final result via animation exporter
mergi.Export(loader.NewAnimationExporter(animation, "result.gif"))

func Crop Uses

func Crop(img image.Image, p1 image.Point, p2 image.Point) (image.Image, error)

Crop uses go standard image.Image, the starting X, Y position as go standard image.Point crop width and height as image.Point returns the crop image output

for more crop examples https://github.com/noelyahan/mergi/examples/crop

This example shows how to use the basic mergi crop Read the byte[] from the file and decode to a go standard image file and saving the import_export image file to disk

Code:

// Get the image content by passing image path url or file path
img, err := mergi.Import(loader.NewFileImporter("testdata/tiger-2320819_960_720.jpg"))
if err != nil {
    log.Fatalf("failed to open: %s", err)
}

// Now let's use the mergi's crop API

// Set where to start the crop point
cropStartPoint := image.Pt(0, 0)

// Set crop width and height as point
cropSize := image.Pt(495, 600)

resultImage, err := mergi.Crop(img, cropStartPoint, cropSize)
if err != nil {
    log.Fatalf("Mergi crop fails due to [%v]", err)
}

// Let's save the image
err = mergi.Export(loader.NewFileExporter(resultImage, "result.jpg"))
if err != nil {
    log.Fatalf("failed to save: %s", err)
}

func Export Uses

func Export(exporter Exporter) error

Export uses to export output do different sources Multiple exporter implementation can be find in loader pkg

for more Import usages https://github.com/noelyahan/mergi/examples

This example shows how to use the mergi Export Mergi provides the simplified api to export the final result Mergi expects Exporter interface type Mergi supports base64, file, animation exporters

Code:

// Get the image content by passing image path url or file path
imgFromLocal, err := mergi.Import(loader.NewFileImporter("testdata/tiger-2320819_960_720.jpg"))
if err != nil {
    log.Fatalf("failed to load via local: %s", err)
}
// Get the image content by passing image path url or file path
// Reference: https://pixabay.com/en/woman-old-senior-female-elderly-1031000/
imageFromURL, err := mergi.Import(loader.NewURLImporter("https://cdn.pixabay.com/photo/2015/11/07/11/17/woman-1031000__340.jpg"))
if err != nil {
    log.Fatalf("failed to load via url: %s", err)
}

// Now you can use this returned standard go image type inside Mergi APIS
log.Println(imgFromLocal.Bounds(), imageFromURL.Bounds())

func Import Uses

func Import(importer Importer) (image.Image, error)

Import uses to import image.Image from different sources Multiple loader implementation can be find in loader pkg

for more Import usages https://github.com/noelyahan/mergi/examples

This example shows how to use the mergi Import Mergi provides the simplified api to load images via URL resource/file path Mergi.Import will return go standard image.Image

Code:

// Get the image content by passing image path url or file path
imgFromLocal, err := mergi.Import(loader.NewFileImporter("testdata/tiger-2320819_960_720.jpg"))
if err != nil {
    log.Fatalf("failed to load via local: %s", err)
}
// Get the image content by passing image path url or file path
// Reference: https://pixabay.com/en/woman-old-senior-female-elderly-1031000/
imageFromURL, err := mergi.Import(loader.NewURLImporter("https://cdn.pixabay.com/photo/2015/11/07/11/17/woman-1031000__340.jpg"))
if err != nil {
    log.Fatalf("failed to load via url: %s", err)
}

// Now you can use this returned standard go image type inside Mergi APIS
log.Println(imgFromLocal.Bounds(), imageFromURL.Bounds())

func Merge Uses

func Merge(template string, imgs []image.Image) (image.Image, error)

Merge uses to merge multiple images according to given templates

ex: "TT" - top, top - horizontal merge (2 images)

ex: "TB" - top, bottom - vertical merge (2 images)

for more merge techniques https://github.com/noelyahan/mergi/examples/merge

This example shows how to use the basic mergi merge for 2 images Read the byte[] from the file and decode to a go standard image file and saving the import_export image file to disk

Code:

// Get the image content by passing image path url or file path
img, err := mergi.Import(loader.NewFileImporter("testdata/evraiki-2514543_240_180.jpg"))
if err != nil {
    log.Fatalf("failed to open: %s", err)
}

// Now let's use the mergi's merge API

// Lets define a template to merge 2 images horizontally
template := "TT"
images := []image.Image{img, img}

resultImage, err := mergi.Merge(template, images)
if err != nil {
    log.Fatalf("failed to merge: %s", err)
}

// Let's save the image
err = mergi.Export(loader.NewFileExporter(resultImage, "result.jpg"))
if err != nil {
    log.Fatalf("failed to save: %s", err)
}

func Resize Uses

func Resize(img image.Image, w, h uint) (image.Image, error)

Resize uses go standard image.Image, unsigned int for width and height that want to resize returns the resize image output

for more resize examples https://github.com/noelyahan/mergi/examples/resize

This example shows how to use the basic mergi resize Read the byte[] from the file and decode to a go standard image file and saving the import_export image file to disk

Code:

// Get the image content by passing image path url or file path
img, err := mergi.Import(loader.NewFileImporter("testdata/evraiki-2514543_240_180.jpg"))
if err != nil {
    log.Fatalf("failed to open: %s", err)
}

// Now let's use the mergi's resize API

// Lets resize double of the given image's size
width := uint(img.Bounds().Max.X * 2)
height := uint(img.Bounds().Max.Y * 2)

resultImage, err := mergi.Resize(img, width, height)
if err != nil {
    log.Fatalf("failed to resize: %s", err)
}

// Let's save the image
err = mergi.Export(loader.NewFileExporter(resultImage, "result.jpg"))
if err != nil {
    log.Fatalf("failed to save: %s", err)
}

func Watermark Uses

func Watermark(watermark, original image.Image, p image.Point) (image.Image, error)

Watermark uses go standard image.Image to get the watermark image and original image that want to watermark,

the position of the watermark has to provide in image.Point then it'll returns the watermarked image output

for more watermark examples https://github.com/noelyahan/mergi/examples/watermark

This example shows how to use the basic mergi watermark Read the byte[] from the file and decode to a go standard image file and saving the import_export image file to disk

Code:

// Get the image content by passing image path url or file path
imgOriginal, err := mergi.Import(loader.NewFileImporter("testdata/tiger-2320819_960_720.jpg"))
if err != nil {
    log.Fatalf("failed to open: %s", err)
}
// Get the image content by passing image path url or file path
imgWatermark, err := mergi.Import(loader.NewFileImporter("./testdata/mergi_logo_watermark.png"))
if err != nil {
    log.Fatalf("failed to open: %s", err)
}

// Now let's use the mergi's watermark API

// Let's position the watermark left top corner
p := image.Pt(0, 0)

resultImage, err := mergi.Watermark(imgWatermark, imgOriginal, p)
if err != nil {
    log.Fatalf("failed to watermark image: %s", err)
}

// Let's save the image
err = mergi.Export(loader.NewFileExporter(resultImage, "result.jpg"))
if err != nil {
    log.Fatalf("failed to save: %s", err)
}

type Exporter Uses

type Exporter interface {
    Export() error
}

Exporter uses to plug any kind of image.Image exporter

type Importer Uses

type Importer interface {
    Import() (image.Image, error)
}

Importer uses to plug any kind of image.Image importer

Directories

PathSynopsis
loaderPackage loader implements Importer, Exporter interfaces for library inputs and outputs

Package mergi imports 8 packages (graph). Updated 2018-11-04. Refresh now. Tools for package owners.