gocrop

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrImageLoadFailed = errors.New("unable to load image")
View Source
var ErrImageUncroppable = errors.New("image does not support cropping")
View Source
var ErrUnsupportedFormat = errors.New("unsupported format")

Functions

This section is empty.

Types

type Croppable

type Croppable struct {
	Path   string
	Image  CroppableImage
	Decode func(r io.Reader) (image.Image, error)
	Encode func(w io.Writer, m image.Image) error
}

Croppable holds the path of the image, the image itself and a proper encoder function for encoding the image.

func Load

func Load(path string) (*Croppable, error)

Load validates if given image format is supported, if so creates a *Croppable and calls it's Load() method.

func (*Croppable) Load

func (c *Croppable) Load() error

Load loads the image of the croppable using it's decoder returns an error if image was not successfully decoded or image is not croppable.

func (*Croppable) With

func (c *Croppable) With(ci CroppableImage) *Croppable

With returns a copy of current croppable with Image set to provided image.

type CroppableImage

type CroppableImage interface {
	image.Image
	SubImage(r image.Rectangle) image.Image
}

Croppable image is an extension of image.Image interface to ensure the image is croppable.

type Cropper

type Cropper struct {
	// contains filtered or unexported fields
}

Cropper crops and saves images.

func NewCropper

func NewCropper(options ...CropperOption) (*Cropper, error)

NewCropper creates an instance of *Cropped with provided options, returns error if any option fails.

Default Cropper with no options:

- has alpha threshold of 0 and no padding

- saves images under the same name in the same directory as the source image (file will be overwritten). If cropping made no changes it still saves the result.

func (*Cropper) Crop

func (i *Cropper) Crop(croppable *Croppable) (*Croppable, bool)

Crop takes a *Croppable and returns a cropped version of it and a bool flag indicating if cropping was done. If cropping would make no changes to given *Croppable, the provided *Croppable is returned back with false flag.

func (*Cropper) CropAndSave

func (i *Cropper) CropAndSave(croppable *Croppable) error

CropAndSave crops an image and saves it, output directory will be created if it does not exist. Error will be returned if the image was not saved successfully.

func (*Cropper) Rect

func (i *Cropper) Rect(img image.Image) image.Rectangle

Rect returns the cropping rectangle of the image, does not include padding.

func (*Cropper) Save

func (i *Cropper) Save(c *Croppable) error

Save saves the croppable, creates a directory if it doesn't exist.

type CropperOption

type CropperOption func(*Cropper) error

func WithEnumerate

func WithEnumerate(enumerate bool) CropperOption

WithEnumerate enables enumeration of output images. "_n" is appended to it's name (before the suffix), n is an integer incremented by 1 each time an image is saved. Given n of 5, image file name of "image1.png", the output image will be named "image1_5.png".

func WithOutDir

func WithOutDir(dir string) CropperOption

WithOutDir sets the output directory for cropped images.

func WithOutPrefix

func WithOutPrefix(prefix string) CropperOption

WithOutPrefix adds a prefix to an image name. Given prefix "cropped_" and image file name "image1.png", the output image will be named "cropped_image1.png".

func WithOutSuffix

func WithOutSuffix(suffix string) CropperOption

WithOutSuffix adds a suffix to an image name. Given suffix "_cropped" and image file name "image1.png", the output image will be named "image1_cropped.png".

func WithPadding

func WithPadding(padding int) CropperOption

WithPadding sets the number of pixels to add in each direction around the cropped image. If the cropped output is the size 25x25px, with 5px of padding it will be 35x35px with the cropped element centered.

func WithSkipUnchanged

func WithSkipUnchanged(skip bool) CropperOption

WithSkipUnchanged if set to true output image won't be saved if cropping made no changes to original image.

func WithThreshold

func WithThreshold(threshold uint32) CropperOption

WithThreshold sets the alpha channel threshold for cropping. Only pixels that satisfy the condition pixelAlpha > threshold will be used in the process of finding a cropping rectangle.

type Finder

type Finder struct {
	// contains filtered or unexported fields
}

Finder is a tool for loading all valid images from given directories. It can apply regex filtering and descend recursively through the directory tree.

func NewFinder

func NewFinder(options ...FinderOptions) (*Finder, error)

NewFinder is a constructor for *Finder, accepts FinderOptions, returns an error if any option fails.

func (*Finder) Find

func (d *Finder) Find(dirs []string) ([]*Croppable, error)

Find takes a slice of directory paths, searches those directories for images in supported formats and returns a list of croppables, ready to be loaded. Does not load images to check if they can be cropped.

type FinderOptions

type FinderOptions func(*Finder) error

func WithRecursive

func WithRecursive(enable bool) FinderOptions

WithRecursive enables Finder to traverse all subdirectories.

func WithRegex

func WithRegex(regex string) FinderOptions

WithRegex attempts to compile given regex string if successful enables Finder regex filtering.

Jump to

Keyboard shortcuts

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