imageslicer

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: MIT Imports: 13 Imported by: 2

README

image-slicer

Split images into tiles.

Perform advanced image processing in Go - slice images into tiles, combine the tiles.

Go Go.Dev

Idea and Inspiration

I was tasked with building a puzzle game in Go. I struggled to find an article or a library that can accomplish image slicing. Eventually, I had to settle down with python's image-slicer, and build a microservice Game-Engine.

This incident made me realize that I can build a 35x faster library in Go. Hence, the inception of image-slicer.

Usecase 1: Split images into tiles
Grid [2*2]

hiro 2x2

Grid [3*3]

hiro3x3J

Grid [4*4]

Usecases 2: Join the tiles back together
Grid [2*2]

hiro 2x2R

Grid [3*3]

hiro3x3R

Grid [4*4]

Support

You can file an Issue. See documentation in Go.Dev

Getting Started

Download
go get -u github.com/goferHiro/image-slicer

Example

Slice Images
Click to Expand

imgUrl := "https://static.wikia.nocookie.net/big-hero-6-fanon/images/0/0f/Hiro.jpg/revision/latest?cb=20180511180437"

img := imageslicer.GetImageFromUrl(imgUrl)

if img == nil {
log.Fatalln("invalid image url or image format not supported!")
}

grid := [2]uint{2, 2} //rows,columns

tiles := imageslicer.Slice(img, grid)

expectedTiles := int(grid[0] * grid[1])

if len(tiles) != expectedTiles {
log.Fatalf("expected %d rcvd %d\n", expectedTiles, len(tiles))
}

Join the tiles back
Click to Expand
//lets join the tiles back

joinedImg, err := imageslicer.Join(tiles, grid)

if err != nil {
log.Fatalf("joining tiles failed due to %s", err)
}

shapeJ := joinedImg.Bounds()
shapeI := img.Bounds()

fmt.Println(shapeJ, shapeI) //shape might change due to pixel loss
}

Get Images
//From base64

base64str := "" //very long.

img, err := imageslicer.GetImageFromBase64(base64str)


//From urls

imgUrl := "https://static.wikia.nocookie.net/big-hero-6-fanon/images/0/0f/Hiro.jpg/revision/latest?cb=20180511180437"

img := imageslicer.GetImageFromUrl(imgUrl)


Inspired from ...

Important Notes/Caveats/Pitfalls

  • Use the latest release.
  • Slice(img,grid)
    • ensure grid[0]*grid[1]>0.
    • ensure img is not nil.
    • its best if the img is downsized to the area of grid to avoid pixel losses. //Will bring support in future releases
  • Join(tiles,grid)
    • ensure that the grid is the same grid used originally for slicing.
    • the grid is utilized to figure out positions of tiles & the dimensions of the final image.
    • For best results, ensure the tiles were generated by image-slicer.
  • GetImageFromBase64(base64str)
    • Supports the image tag format i.e "data:image/jpeg;base64,/" as well
    • Supports jpeg,png only
    • Does not support svg, or any other formats
  • GetImageFromUrl(imgUrl)
    • Expects jpeg,png formats only

Contribution


  • You can submit an issue or create a Pull Request (PR)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckSlice added in v1.2.0

func CheckSlice(tiles []image.Image, grid [2]uint) (err error)

func GetBytes

func GetBytes(i image.Image) (b []byte)

func GetImageFromBase64

func GetImageFromBase64(base64Img string) (img image.Image, err error)

func GetImageFromPath

func GetImageFromPath(imgPath string) (img image.Image, err error)

func GetImageFromUrl

func GetImageFromUrl(imgUrl string) (img image.Image)

func Join

func Join(tiles []image.Image, grid [2]uint) (img image.Image, err error)

func Slice

func Slice(img image.Image, grid [2]uint) (tiles []image.Image)

func Version

func Version() string

Version is the current release version.

Types

type Grid

type Grid struct {
	Rows    int
	Columns int
}

Directories

Path Synopsis
examples
basic Module

Jump to

Keyboard shortcuts

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