Code Monkey home page Code Monkey logo

go-image-merge's Introduction

gim - Grid Based Image Merge Library

gim is a image merging library that can accept image paths as input, read the image contents, add background color, draw layers on top of each other, merge them into a grid with the desired size.

Go Report Card GoDoc

Table of Contents

Overview

gim provides an easy and extensible way to merge images into a flexible grid system.

The main purpose of the library is to help creating image collages programmatically.

Installation

go get -u github.com/ozankasikci/go-image-merge

Getting Started

Import the library and give the image paths and grid size as the minimum required arguments.

Basic usage:

import gim "github.com/ozankasikci/go-image-merge"

// accepts *Grid instances, grid unit count x, grid unit count y
// returns an *image.RGBA object
grids := []*gim.Grid{
	{ImageFilePath: "file.jpg"},
	{ImageFilePath: "file.png"},
}
rgba, err := gim.New(grids, 2, 1).Merge()

// save the output to jpg or png
file, err := os.Create("file/path.jpg|png")
err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 80})
err = png.Encode(file, rgba)

See Examples for available options and advanced usage.

Examples

Grid Unit Count - Vertical & Horizontal

grids := []*gim.Grid{
    {ImageFilePath: "./cmd/gim/input/kitten.jpg"},
    {ImageFilePath: "./cmd/gim/input/kitten.jpg"},
    {ImageFilePath: "./cmd/gim/input/kitten.jpg"},
    {ImageFilePath: "./cmd/gim/input/kitten.jpg"},
}
rgba, err := gim.New(grids, 2, 2).Merge()

Output:

Grid Background Color

grids := []*gim.Grid{
    {
        ImageFilePath: "./cmd/gim/input/ginger.png",
        BackgroundColor: color.White,
    },
    {
        ImageFilePath: "./cmd/gim/input/ginger.png",
        BackgroundColor: color.RGBA{R: 0x8b, G: 0xd0, B: 0xc6},
    },
}
rgba, err := gim.New(grids, 2, 1).Merge()

Output:

Grid Layers - Draw Grids on top of Grids

grids := []*gim.Grid{
    {
        ImageFilePath: "./cmd/gim/input/ginger.png",
        BackgroundColor: color.White,
        // these grids will be drawn on top of the first grid
        Grids: []*gim.Grid{
            {
            	ImageFilePath: "./cmd/gim/input/tick.png",
            	OffsetX: 50, OffsetY: 20,
            },
        },
    },
    {
        ImageFilePath: "./cmd/gim/input/ginger.png",
        BackgroundColor: color.RGBA{R: 0x8b, G: 0xd0, B: 0xc6},
        // these grids will be drawn on top of the second grid
        Grids: []*gim.Grid{
            {
            	ImageFilePath: "./cmd/gim/input/tick.png",
            	OffsetX: 200, OffsetY: 170,
            },
            {
            	ImageFilePath: "./cmd/gim/input/tick.png",
            	OffsetX: 200, OffsetY: 20,
            },
        },
    },
}
rgba, err := gim.New(grids, 2, 1).Merge()

Output:

Filters

Available Filters

  • Black & White: Converts an image to grayscale.
  • Sepia: Applies a sepia filter to an image.
  • Vignette: Applies a vignette filter to an image.

Applying Filters

To apply filters, include them in the Filters slice of the Grid struct. Each Grid can have multiple filters applied, which will be processed in the order they are added.

Example: Black & White Filter

grids := []*gim.Grid{
    {
        ImageFilePath:   "./cmd/gim/input/ginger2.png",
        Filters: []imagefilter.FilterType{
            imagefilter.BlackAndWhite,
        },
    },
    {
        ImageFilePath:   "./cmd/gim/input/ginger2.png",
    },
}

Output:

Functional Options

OptBaseDir

// you can omit the full path if you set a base dir
grids := []*gim.Grid{
    {ImageFilePath: "kitten.jpg"},
    {ImageFilePath: "kitten.jpg"},
}
rgba, err := gim.New(
	grids, 1, 2,
	gim.OptBaseDir("./cmd/gim/input"),
).Merge()

OptGridSize

// you can resize the grids in pixels
grids := []*gim.Grid{
    {ImageFilePath: "kitten.jpg"},
    {ImageFilePath: "kitten.jpg"},
}
rgba, err := gim.New(
	grids, 2, 1,
	gim.OptBaseDir("./cmd/gim"),
	gim.OptGridSize(200,150),
).Merge()

Output:

TODO

  • Add colored background support
  • Add resize support (stretch, fit etc.)
  • Add filters

go-image-merge's People

Contributors

blue14753 avatar gucio321 avatar marcsantiago avatar ozankasikci avatar setanarut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-image-merge's Issues

May I ask why I could merge it as transparent png?

I'm sorry to trouble you here.

I used code below to render my multiple layers to a single image:

layers := getInputLayers() // slice of files in string
grids := []*gim.Grid{
{ImageFilePath: "input/" + layers[6]},
{ImageFilePath: "input/" + layers[5]},
{ImageFilePath: "input/" + layers[4]},
}
rgba, err := gim.New(grids, 1, 1).Merge()
file, err := os.Create("output/images/path.png")
err = png.Encode(file, rgba)

All layers that I've used are transparent which can be merged/combined in other program/software.
However I always got the top layer with solid rendering with my code.

May I ask for some help?
Appreciate your time and attention.

No api for byte slice

Love the project,

Would love to see an API for handling images as a slice of byte

May fork and implement on own time

Error when pasting image from memory

I originally was making a script to paste an image on to a new image, I saw your comment on stackoverflow and then found my way to this.

I wanted to create a new image the size of both images then post imageA at coords 0,0 then imageB to the right of imageA.

Your script works flawlessly when you enter directories.

Image looks like this using directory param https://imgur.com/a/qoYi4K7

Image looks like this using image.Image https://imgur.com/a/ddb3qbX

I even tried setting the pixels on the new image equal to each pixel on the old image but it starts editing the image half way down the y axis and im not sure why even tho i'm starting at 0,0

I wanted to create the new image in memory rather than writing and reading files [just means i'll have to delete them later]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.