mergi

package module
v0.0.0-...-221cd73 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 10 Imported by: 69

README

Mergi

Result Terminal Code
Intro mergi -t TT -i https://raw.githubusercontent.com/ashleymcnamara/gophers/master/Facepalm_Gopher.png -r "131 131" -i https://raw.githubusercontent.com/ashleymcnamara/gophers/master/Facepalm_Picard_Gopher.png -r "131 131" -a "sprite 50"

Go Doc Build Status codecov Go Report Card Teligram Chat

🎉 Basic Overview

Image manipulation go library plus cross platform CLI tool.

⚡ Features

  • 🛠 Merge
  • ✂️ Crop
  • 💣 Resize
  • 🖃 Watermark
  • 💖 Animate
  • 🔥 Easing
  • 🦎 Transition

🚀 Getting started

Install via go get

To install Mergi, use go get, or download the binary file from Releases page.

$ go get github.com/noelyahan/mergi

Usage:

 ╔╦╗╔═╗╦═╗╔═╗╦
 ║║║║╣ ╠╦╝║ ╦║
 ╩ ╩╚═╝╩╚═╚═╝╩
 let's go & make imaging fun
 http://mergi.io
 version 1.0.0

  -a string
    	Enter animation type=[sprite, slide] and the delay to get mergi gif animation ex: smooth 10
  -c value
    	Enter crop points and height and width ex: x y w h
  -f string
    	Enter true if you want to process the final output
  -i value
    	Enter images that want to merge ex: /path/img1 or url
  -o string
    	Enter image outputs file ex: out.png or out.jpg (default "out.png")
  -r value
    	Enter resize width and height of the output ex: 100 200
  -t string
    	Enter a merge template string ex: TBTBTB (default "T")
  -w value
    	Enter watermark image and points to place it, [-r w h] is optional  ex: /path/img -r w h x y


🛠 Merge
Image 1 Image 2 Result Image
dstImage srcImage dstImage
dstImage srcImage dstImage
Mergi Tool
Horizontal
mergi \
-t TT \
-i testdata/mergi_bg_1.png \
-i testdata/mergi_bg_2.png
Vertical
mergi \
-t TB \
-i testdata/mergi_bg_1.png \
-i testdata/mergi_bg_2.png
Mergi Library
image1, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_1.png"))
image2, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_2.png"))

horizontalImage, _ := mergi.Merge("TT", []image.Image{image1, image2})
mergi.Export(impexp.NewFileExporter(horizontalImage, "horizontal.png"))

verticalImage, _ := mergi.Merge("TB", []image.Image{image1, image2})
mergi.Export(impexp.NewFileExporter(verticalImage, "vertical.png"))

✂️ Crop
Image Result Image
srcImage dstImage
Mergi Tool
mergi \
-i testdata/mergi_bg_1.png \
-c "10 40 200 110"
Mergi Library
img, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_1.png"))
res, _ := mergi.Crop(img, image.Pt(10, 40), image.Pt(200, 110))
mergi.Export(impexp.NewFileExporter(res, "crop.png"))

💣 Resize
Image Result Image
srcImage dstImage
Mergi Tool
mergi \
-i testdata/mergi_bg_1.png \
-r "180 80"
Mergi Library
img, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_1.png"))
res, _ := mergi.Resize(img, uint(180), uint(80))
mergi.Export(impexp.NewFileExporter(res, "resize.png"))

🖃 Watermark
Image Watermark Image Result Image
srcImage dstImage dstImage
Mergi Tool
mergi \
-i testdata/mergi_bg_1.png \
-w "testdata/mergi_logo_watermark_90x40.png 250 10"
Mergi Library
originalImage, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_1.png"))
watermarkImage, _ := mergi.Import(impexp.NewFileImporter("./testdata/glass-mergi_logo_watermark_90x40.jpg"))

res, _ := mergi.Watermark(watermarkImage, originalImage, image.Pt(250, 10))
mergi.Export(impexp.NewFileExporter(res, "watermark.png"))

💖 Animate
Image 1 Image 2 Result Animation
srcImage dstImage dstImage
srcImage dstImage dstImage
Mergi Tool
Sprite Animation
mergi \
-t "TT" \
-i testdata/mergi_bg_1.png \
-i testdata/mergi_bg_2.png \
-a "sprite 50"
Smooth Animation
mergi \
-t "TT" \
-i testdata/mergi_bg_1.png \
-i testdata/mergi_bg_2.png \
-a "smooth 5"
Mergi Library
image1, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_1.png"))
image2, _ := mergi.Import(impexp.NewFileImporter("./testdata/mergi_bg_2.png"))

gif, _ := mergi.Animate([]image.Image{image1, image2}, 50)
mergi.Export(impexp.NewAnimationExporter(gif, "out.gif"))

🔥 Easing
dstImage
InBounce
dstImage
InBack
dstImage
InOutQuad
dstImage
InSine
dstImage
InCubic
dstImage
InElastic
dstImage
InOutExpo
dstImage
Linear
dstImage
InOutBounce
dstImage
InCirc
dstImage
InOutCubic
dstImage
InOutQuart
dstImage
InOutBack
dstImage
InCubic
dstImage
InOutCirc
dstImage
InOutSine
dstImage
InExpo
dstImage
OutBounce
dstImage
InQuint
Mergi Library

Note: Ease function can be applied with any function, in this example it's applied with Watermark function

// Load background and the square images
square, _ := mergi.Import(impexp.NewFileImporter("./testdata/square.jpg"))
bg, _ := mergi.Import(impexp.NewFileImporter("./testdata/white_bg.jpg"))

// Init images frames to add applied ease frames
frames := make([]image.Image, 0)

// Init the limts of the Ease
to := bg.Bounds().Max.X - square.Bounds().Max.X
posY := bg.Bounds().Max.Y/2 - square.Bounds().Max.Y/2
speed := 4

// Ease from 0 to width of background
for i := 0; i < to; i += speed {
  // Apply Easeing function InBounce
  posX := mergi.Ease(float64(i), 0, float64(to), mergi.InBounce)
  img, _ := mergi.Watermark(square, bg, image.Pt(int(posX), posY))
  frames = append(frames, img)
}

// For preview example, save as a gif
gif, _ := mergi.Animate(frames, 1)
mergi.Export(impexp.NewAnimationExporter(gif, "out.gif"))

🦎 Transition
dstImage
SlideBar
dstImage
Ink1
dstImage
Ink2
dstImage
Ink3
dstImage
ScaleUpFastRect
dstImage
ScaleDownFastRect
dstImage
ScaleUpFastCircle
dstImage
ScaleDownFastCircle

Learn more examples

💻 Contribute

  • Clone the repository
$ go get github.com/noelyahan/mergi
  • Run unit tests
  • Fix bug
  • Add new feature
  • Push

🌠 Contributors


Noel

💻 📖 💬 👀 🤔 🎨

This project follows the all-contributors specification. Contributions of any kind are welcome!


Star History

Star History Chart

🔵 License

This project is licensed under the MIT License - see the LICENSE.md file for details

Documentation

Overview

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

Constants

This section is empty.

Variables

View Source
var MaskBlack = color.RGBA{0, 0, 0, 0}

MaskBlack provides black masking

View Source
var MaskWhite = color.RGBA{255, 255, 255, 0}

MaskWhite provides white masking

Functions

func Animate

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

Example (Animating)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"image"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	img1, err := mergi.Import(impexp.NewFileImporter("testdata/avocado-3210885_960_720.jpg"))
	if err != nil {
		log.Fatalf("failed to load via local: %s", err)
	}
	img2, err := mergi.Import(impexp.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(impexp.NewAnimationExporter(animation, "result.gif"))
}
Output:

func Crop

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

Example (Croping)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"image"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	img, err := mergi.Import(impexp.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(impexp.NewFileExporter(resultImage, "result.jpg"))
	if err != nil {
		log.Fatalf("failed to save: %s", err)
	}
}
Output:

func Ease

func Ease(value, start, end float64, ease func(t float64) float64) float64

func Export

func Export(exporter Exporter) error

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

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

Example (Exporting)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	imgFromLocal, err := mergi.Import(impexp.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(impexp.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())
}
Output:

func Import

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

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

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

Example (Importing)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	imgFromLocal, err := mergi.Import(impexp.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(impexp.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())
}
Output:

func InBack

func InBack(t float64) float64

func InBounce

func InBounce(t float64) float64

func InCirc

func InCirc(t float64) float64

func InCubic

func InCubic(t float64) float64

func InElastic

func InElastic(t float64) float64

func InExpo

func InExpo(t float64) float64

func InOutBack

func InOutBack(t float64) float64

func InOutBounce

func InOutBounce(t float64) float64

func InOutCirc

func InOutCirc(t float64) float64

func InOutCubic

func InOutCubic(t float64) float64

func InOutElastic

func InOutElastic(t float64) float64

func InOutExpo

func InOutExpo(t float64) float64

func InOutQuad

func InOutQuad(t float64) float64

func InOutQuart

func InOutQuart(t float64) float64

func InOutQuint

func InOutQuint(t float64) float64

func InOutSine

func InOutSine(t float64) float64

func InQuad

func InQuad(t float64) float64

func InQuart

func InQuart(t float64) float64

func InQuint

func InQuint(t float64) float64

func InSine

func InSine(t float64) float64

func Linear

func Linear(t float64) float64

func Mask

func Mask(maskImg, original image.Image, maskColor color.RGBA) (image.Image, error)

Mask uses go standard image.Image to get the masked image with original image,

Mask simply match the given color and matched with mask image and original to replace alpha values 0, 255

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

func Merge

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

Example (Merging)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"image"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	img, err := mergi.Import(impexp.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(impexp.NewFileExporter(resultImage, "result.jpg"))
	if err != nil {
		log.Fatalf("failed to save: %s", err)
	}
}
Output:

func Opacity

func Opacity(img image.Image, alpha float64) (image.Image, error)

Opacity uses go standard image.Image to change the alpha channel of the given image,

the floating point alpha amount has to provide with the given image and it'll return the opacity image

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

func OutBack

func OutBack(t float64) float64

func OutBounce

func OutBounce(t float64) float64

func OutCirc

func OutCirc(t float64) float64

func OutCubic

func OutCubic(t float64) float64

func OutElastic

func OutElastic(t float64) float64

func OutExpo

func OutExpo(t float64) float64

func OutQuad

func OutQuad(t float64) float64

func OutQuart

func OutQuart(t float64) float64

func OutQuint

func OutQuint(t float64) float64

func OutSine

func OutSine(t float64) float64

func Resize

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

Example (Resizing)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	img, err := mergi.Import(impexp.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(impexp.NewFileExporter(resultImage, "result.jpg"))
	if err != nil {
		log.Fatalf("failed to save: %s", err)
	}
}
Output:

func Transit

func Transit(imgs1, imgs2, trans []image.Image, mask color.RGBA, start, end, speed float64) []image.Image

func Watermark

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

Example (Watermarking)

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

package main

import (
	"github.com/noelyahan/impexp"
	"github.com/noelyahan/mergi"
	"image"
	"log"
)

func main() {
	// Get the image content by passing image path url or file path
	imgOriginal, err := mergi.Import(impexp.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(impexp.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(impexp.NewFileExporter(resultImage, "result.jpg"))
	if err != nil {
		log.Fatalf("failed to save: %s", err)
	}
}
Output:

Types

type Exporter

type Exporter interface {
	Export() error
}

Exporter uses to plug any kind of image.Image exporter

type Importer

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

Importer uses to plug any kind of image.Image importer

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

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