colorcrop

package module
v0.0.0-...-9fb5e50 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 3 Imported by: 3

README

colorcrop
=========

|bs| |cs| |rc| |gd|

.. |bs| image:: https://travis-ci.org/nxshock/colorcrop.svg?branch=master
   :alt: Build Status
   :target: https://travis-ci.org/nxshock/colorcrop
.. |cs| image:: https://coveralls.io/repos/github/nxshock/colorcrop/badge.svg
   :alt: Coverage Status
   :target: https://coveralls.io/github/nxshock/colorcrop
.. |rc| image:: https://goreportcard.com/badge/github.com/nxshock/colorcrop
   :alt: Go Report Card Status
   :target: https://goreportcard.com/report/github.com/nxshock/colorcrop
.. |gd| image:: https://godoc.org/github.com/nxshock/colorcrop?status.svg
   :alt: GoDoc
   :target: https://godoc.org/github.com/nxshock/colorcrop

A pure Go library for cropping images by removing borders with specified color.

Installation
------------

``go get -u github.com/nxshock/colorcrop``

Usage
-----

Import package with:

.. code:: go

    import "github.com/nxshock/colorcrop"

Crop white borders with 50% of thresold:

.. code:: go

    croppedImage := colorcrop.Crop(
        sourceImage,                    // for source image
        color.RGBA{255, 255, 255, 255}, // crop white border
        0.5)                            // with 50% thresold

You may use custom comparator of colors:

.. code:: go

    croppedImage := colorcrop.CropWithComparator(
        sourceImage,                    // for source image
        color.RGBA{255, 255, 255, 255}, // crop white border
        0.5,                            // with 50% thresold
        colorcrop.CmpCIE76)             // using CIE76 standart for defining color difference

List of available comparators:

================  =============================================================================================================
Comparator        Description
================  =============================================================================================================
CmpRGBComponents  simple RGB components difference: ``abs(r1-r2)+abs(g1-g2)+abs(b1-b2)`` (default);
CmpEuclidean      `Euclidean difference <https://en.wikipedia.org/wiki/Color_difference#Euclidean>`_;
CmpCIE76          difference of two colors defined in `CIE76 standard <https://en.wikipedia.org/wiki/Color_difference#CIE76>`_.
================  =============================================================================================================

Examples
--------

See `here <https://github.com/nxshock/colorcrop/blob/master/example_test.go>`_.

Documentation

Overview

Package colorcrop is a Go library for cropping images by removing borders with specified color.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CmpCIE76

func CmpCIE76(color1 color.Color, color2 color.Color) float64

CmpCIE76 returns difference of two colors defined in CIE76 standart.

https://en.wikipedia.org/wiki/Color_difference#CIE76

func CmpEuclidean

func CmpEuclidean(color1 color.Color, color2 color.Color) float64

CmpEuclidean returns Euclidean difference of two colors.

https://en.wikipedia.org/wiki/Color_difference#Euclidean

func CmpRGBComponents

func CmpRGBComponents(color1 color.Color, color2 color.Color) float64

CmpRGBComponents returns RGB components difference of two colors.

func Crop

func Crop(img image.Image, color color.Color, thresold float64) image.Image

Crop returns cropped image with default comparator.

Example

Simple remove of white borders.

package main

import (
	"image/color"
	"image/png"
	"os"

	"github.com/nxshock/colorcrop"
)

func main() {
	// Read source image
	sourceFile, _ := os.Open("img.png")
	defer sourceFile.Close()

	sourceImage, _ := png.Decode(sourceFile)

	// Crop white border with 50% thresold
	croppedImage := colorcrop.Crop(
		sourceImage,                    // for source image
		color.RGBA{255, 255, 255, 255}, // crop white border
		0.5)                            // with 50% thresold

	// Save cropped image
	croppedFile, _ := os.Create("cropped.png")
	defer croppedFile.Close()

	png.Encode(croppedFile, croppedImage)
}
Output:

func CropWithComparator

func CropWithComparator(img image.Image, color color.Color, thresold float64, comparator comparator) image.Image

CropWithComparator returns cropped image with specified comparator.

Example

Remove white borders with custom color comparator.

package main

import (
	"image/color"
	"image/png"
	"os"

	"github.com/nxshock/colorcrop"
)

func main() {
	// Read source image
	sourceFile, _ := os.Open("img.png")
	defer sourceFile.Close()

	sourceImage, _ := png.Decode(sourceFile)

	// Crop white border with 50% thresold
	croppedImage := colorcrop.CropWithComparator(
		sourceImage,                    // for source image
		color.RGBA{255, 255, 255, 255}, // crop white border
		0.5,                            // with 50% thresold
		colorcrop.CmpCIE76)             // using CIE76 standart for defining color difference

	// Save cropped image
	croppedFile, _ := os.Create("cropped.png")
	defer croppedFile.Close()

	png.Encode(croppedFile, croppedImage)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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