imageutil

package
v0.63.9 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 23 Imported by: 9

Documentation

Index

Constants

View Source
const (
	AlignTop    = "top"
	AlignCenter = "center"
	AlignBottom = "bottom"
	AlignLeft   = "left"
	AlignRight  = "right"
)
View Source
const (
	AlgNearestNeighbor = "nearestneighbor"
	AlgApproxBiLinear  = "approxbilinear"
	AlgBiLinear        = "bilinear"
	AlgCatmullRom      = "catmullrom"
)
View Source
const (
	LocUpper      = "upper"
	LocMiddle     = "middle"
	LocLower      = "lower"
	LocLeft       = "left"
	LocCenter     = "center"
	LocRight      = "right"
	LocUpperLeft  = "upperleft"
	LocUpperRight = "upperright"
	LocLowerLeft  = "lowerleft"
	LocLowerRight = "lowerright"
)
View Source
const (
	FileExtensionWebp = ".webp"

	FormatNameJPG  = "jpeg"
	FormatNamePNG  = "png"
	FormatNameWEBP = "webp"
)
View Source
const (
	JPEGExt            = ".jpeg"
	JPEGExtJPG         = ".jpg"
	JPEGQualityDefault = jpeg.DefaultQuality // 75
	JPEGQualityMax     = 100
	JPEGQualityMin     = 1
	JPEGMarkerPrefix   = 0xff
	JPEGMarkerExif     = 0xe1
	JPEGMarkerSOI      = 0xd8
)

Variables

View Source
var (
	ErrImageNotSet  = errors.New("image not set")
	ErrWriterNotSet = errors.New("writer not set")
)
View Source
var (
	ErrSrcDirNotDefined = errors.New("source directory not defined")
	ErrOutDirNotDefined = errors.New("output directory not defined")
	ErrSrcDirNotDir     = errors.New("source directory is not a directory")
	ErrOutDirNotDir     = errors.New("output directory is not a directory")
)
View Source
var JPEGEncodeOptionsQualityMax = &JPEGEncodeOptions{
	Options: &jpeg.Options{
		Quality: JPEGQualityMax}}
View Source
var RxFileExtensionJPG = regexp.MustCompile(`(?i)^.*\.*jpe?g$`)

Functions

func AddBackgroundColor

func AddBackgroundColor(img image.Image, clr color.Color) draw.Image

AddBackgroundColor adds a background of `color.Color` to an image. It is is useful when the image has a transparent background. Use `colornames` for more colors, e.g. `colornames.Blue`. This returns a `draw.Image` so it can be used as an input to `draw.Draw()`.

func AddBackgroundWhite

func AddBackgroundWhite(img image.Image) image.Image

AddBackgroundWhite adds a white background which is usable when the image has a transparent background.

func AddBorder added in v0.35.7

func AddBorder(img draw.Image, clr color.Color, width uint) draw.Image

AddBorder adds a border to a `draw.Image`. If you have an `image.Image`, first convert it with `ImageToRGBA(img)`.

func AddBorderAverageColor added in v0.49.0

func AddBorderAverageColor(img image.Image, width uint) image.Image

func Aspect

func Aspect(width, height int) float64

func BuildGifAnimationSimple added in v0.36.0

func BuildGifAnimationSimple(src *gif.GIF, delay int, imgs []image.Image, f ToPalettedFunc) *gif.GIF

BuildGifAnimationSimple assembles a set of images in an animated GIF file. Set `delay` to `0`.

func BuildGifAnimationSimpleRead added in v0.37.0

func BuildGifAnimationSimpleRead(src *gif.GIF, delay int, names []string, f ToPalettedFunc, consistentSize bool) (*gif.GIF, error)

func ColsFilteredColor added in v0.33.3

func ColsFilteredColor(img image.Image, c ...color.Color) []int

func Crop added in v0.35.0

func Crop(src image.Image, retain image.Rectangle) image.Image

Crop takes an image and crops it to the specified rectangle.

func CropX added in v0.35.2

func CropX(src image.Image, width uint, align string) image.Image

CropX crops an image by its width horizontally.

func CropY added in v0.35.2

func CropY(src image.Image, height uint, align string) image.Image

CropY crops an image by its height vertically.

func DecodeBytes added in v0.51.0

func DecodeBytes(data []byte) (image.Image, string, error)

DecodeBytes wraps Decode which decodes an image that has been encoded in a registered format. The string returned is the format name used during format registration. Format registration is typically done by an init function in the codec- specific package.

func ImageAspect

func ImageAspect(img image.Image) float64

func ImageColors added in v0.33.5

func ImageColors(img image.Image) [][]color.Color

ImageColors returns colors for an image covering the pixels described in `image.Rectangle`: https://pkg.go.dev/image#Rectangle

func ImageToPaletted added in v0.36.0

func ImageToPaletted(src image.Image, p color.Palette) *image.Paletted

func ImageToPalettedPlan9 added in v0.36.0

func ImageToPalettedPlan9(src image.Image) *image.Paletted

ImageToPalettedPlan9 converts an image to `*image.Paletted`. See the go implementation here: https://github.com/golang/go/blob/master/src/image/gif/writer.go

func ImageToPalettedWebSafe added in v0.36.5

func ImageToPalettedWebSafe(src image.Image) *image.Paletted

ImageToPalettedWebSafe uses the 216 color palete created by Netscape. See more here: https://en.wikipedia.org/wiki/Web_colors#Web-safe_colors

func ImageToRGBA

func ImageToRGBA(src image.Image) *image.RGBA

func IsImageExt

func IsImageExt(imagePath string) bool

func IsNilOrEmpty

func IsNilOrEmpty(img image.Image) bool

func JPEGMarker added in v0.49.0

func JPEGMarker(b byte) []byte

func MergeXRGBA added in v0.36.5

func MergeXRGBA(imgs []image.Image) image.Image

MergeXRGBA returns an image that is composed of non-overlapping imagers in ImageMetaSet. The code here is adapted from https://stackoverflow.com/a/35965499/1908967

func MergeXSameY

func MergeXSameY(images []image.Image, larger bool) image.Image

func MergeXSameYRead

func MergeXSameYRead(locations []string, larger bool) (image.Image, error)

func MergeYSameX

func MergeYSameX(images []image.Image, larger bool) image.Image

func MergeYSameXRead

func MergeYSameXRead(locations []string, larger bool) (image.Image, error)

func NegativeOffset

func NegativeOffset(width, height, offset uint) image.Point

func NewRGBAColor

func NewRGBAColor(rect image.Rectangle, clr color.RGBA) *image.RGBA

func NewRGBATransparent

func NewRGBATransparent(rect image.Rectangle) *image.RGBA

func NewRGBAWhite

func NewRGBAWhite(rect image.Rectangle) *image.RGBA

func Overlay added in v0.35.1

func Overlay(src, overlay image.Image, offset image.Point) image.Image

func OverlayMore added in v0.35.6

func OverlayMore(src, overlay image.Image, overlayLocation string, padX, padY int) image.Image

func OverlayOffset added in v0.35.7

func OverlayOffset(src, overlay image.Rectangle, overlayLocation string, padX, padY int) image.Point

func PaintBorder added in v0.35.7

func PaintBorder(img draw.Image, clr color.Color, width uint)

PaintBorder colorizes a border of size `width` in the existing canvas, overwriting any colors in that space.

func PaintColor added in v0.35.7

func PaintColor(img draw.Image, clr color.Color, area image.Rectangle)

func ParseScaler

func ParseScaler(rawInterpolation string) (draw.Scaler, error)

func ReadDirJPEGFiles added in v0.49.0

func ReadDirJPEGFiles(dir string, rx *regexp.Regexp) (osutil.DirEntries, error)

func ReadImage added in v0.33.3

func ReadImage(location string) (image.Image, string, error)

func ReadImageDimensions

func ReadImageDimensions(imagePath string) (int, int, error)

func ReadImageFile

func ReadImageFile(filename string) (image.Image, string, error)

func ReadImageHTTP added in v0.37.1

func ReadImageHTTP(imageURL string) (image.Image, string, error)

func ReadImages

func ReadImages(locations []string) ([]image.Image, error)

func RectangleBorderXMax

func RectangleBorderXMax(rect image.Rectangle, pixels uint) image.Rectangle

func RectangleBorderXMin

func RectangleBorderXMin(rect image.Rectangle, pixels uint) image.Rectangle

func RectangleBorderYMax

func RectangleBorderYMax(rect image.Rectangle, pixels uint) image.Rectangle

func RectangleBorderYMin

func RectangleBorderYMin(rect image.Rectangle, pixels uint) image.Rectangle

func RectanglePixelCount added in v0.35.1

func RectanglePixelCount(r image.Rectangle) int

func Resize

func Resize(width, height uint, src image.Image, scale draw.Scaler) image.Image

Resize scales an image to the provided size units. Use a 0 to scale the aspect ratio. See gitub.com/nfnt/resize for Lanczos3, etc. https://github.com/nfnt/resize .

func ResizeMax

func ResizeMax(maxWidth, maxHeight uint, src image.Image, scale draw.Scaler) image.Image

ResizeMax resizes an image to maximum dimensions. To resize to a maximum of 800 pixels width, the following can be used: `ResizeMax(800, 0, img, nil)`.

func ResizeMaxDimension

func ResizeMaxDimension(maxSide uint, src image.Image, scale draw.Scaler) image.Image

func ResizeMin

func ResizeMin(minWidth, minHeight uint, src image.Image, scale draw.Scaler) image.Image

ResizeMin resizes an image to minimum dimensions. To resize to a minimum of 800 pixels width, the following can be used: `ResizeMin(800, 0, img, nil)`.

func ResizePathJPEG added in v0.51.0

func ResizePathJPEG(src, out string, x, y uint, o *JPEGEncodeOptions) error

func ResizePathJPEGDir added in v0.51.0

func ResizePathJPEGDir(src, out string, x, y uint, o *JPEGEncodeOptions) error

func ResizePathJPEGFile added in v0.51.0

func ResizePathJPEGFile(src, out string, x, y uint, o *JPEGEncodeOptions) error

func ResizeSameX

func ResizeSameX(images []image.Image, larger bool) []image.Image

func ResizeSameY

func ResizeSameY(images []image.Image, larger bool) []image.Image

func RowsFilteredColor added in v0.33.2

func RowsFilteredColor(img image.Image, c color.Color, cmore ...color.Color) []int

func Scale

func Scale(src image.Image, rect image.Rectangle, scale draw.Scaler) image.Image

Scale will resize the image to the provided rectangle using the provided interpolation function.

func ScalerBest

func ScalerBest() draw.Scaler

func SliceXY

func SliceXY(images []image.Image, maxIdx int) (minX, maxX, minY, maxY, sumX, sumY int)

func Square

func Square(src image.Image) image.Image

Square returns an image that is cropped to where the height and weight are equal to the smaller of the source image.

func SquareLarger added in v0.35.5

func SquareLarger(src image.Image, bgcolor color.Color) image.Image

SquareLarger returns an image that is cropped to where the height and weight are equal to the larger of the source image.

func WriteGIFFile added in v0.51.0

func WriteGIFFile(filename string, img *gif.GIF) error

Types

type Image added in v0.51.0

type Image struct {
	image.Image
}

func (Image) BytesJPEG added in v0.51.0

func (im Image) BytesJPEG(opt *JPEGEncodeOptions) ([]byte, error)

func (Image) BytesPNG added in v0.51.0

func (im Image) BytesPNG() ([]byte, error)

func (Image) WriteJPEG added in v0.51.0

func (im Image) WriteJPEG(w io.Writer, opt *JPEGEncodeOptions) error

func (Image) WriteJPEGFile added in v0.51.0

func (im Image) WriteJPEGFile(filename string, opt *JPEGEncodeOptions) error

func (Image) WriteJPEGResponseWriter added in v0.51.0

func (im Image) WriteJPEGResponseWriter(w http.ResponseWriter, addContentTypeHeader bool, opt *JPEGEncodeOptions) error

func (Image) WritePNG added in v0.51.0

func (im Image) WritePNG(w io.Writer) error

func (Image) WritePNGFile added in v0.51.0

func (im Image) WritePNGFile(filename string) error

func (Image) WritePNGResponseWriter added in v0.51.0

func (im Image) WritePNGResponseWriter(w http.ResponseWriter, addContentTypeHeader bool) error

type ImageMeta

type ImageMeta struct {
	File       *os.File
	FormatName string
	Image      image.Image
}

func NewImageMeta added in v0.35.0

func NewImageMeta(img image.Image) ImageMeta

func (*ImageMeta) ColorAverage added in v0.33.2

func (meta *ImageMeta) ColorAverage() color.Color

type ImageMetaSet

type ImageMetaSet struct {
	ImageMetas []ImageMeta
}

func NewImageSetFiles

func NewImageSetFiles(filenames []string) (ImageMetaSet, error)

func (*ImageMetaSet) CloseFilesAll

func (ims *ImageMetaSet) CloseFilesAll() error

func (*ImageMetaSet) Images added in v0.37.8

func (ims *ImageMetaSet) Images() []image.Image

func (*ImageMetaSet) MaxX

func (ims *ImageMetaSet) MaxX() int

func (*ImageMetaSet) MaxY

func (ims *ImageMetaSet) MaxY() int

func (*ImageMetaSet) Stats

func (ims *ImageMetaSet) Stats() ImagesStats

func (*ImageMetaSet) SumX

func (ims *ImageMetaSet) SumX(maxIndexInclusive int) int

func (*ImageMetaSet) SumY

func (ims *ImageMetaSet) SumY(maxIndexInclusive int) int

type Images added in v0.36.5

type Images []image.Image

func (Images) ConsistentSize added in v0.36.5

func (imgs Images) ConsistentSize(scale draw.Scaler, yAlign string)

ConsistentSize resizes and crops the images so that they have all the same size. It prioritize resizing images to max Dx and then cropping Dy so they are consistent.

func (Images) Dimensions added in v0.37.8

func (imgs Images) Dimensions() []image.Point

func (Images) DxMax added in v0.36.5

func (imgs Images) DxMax() int

func (Images) DxMin added in v0.36.5

func (imgs Images) DxMin() int

func (Images) DxSum added in v0.36.5

func (imgs Images) DxSum(maxIndexInclusive int) int

DxSum returns the sum of widths up to and including `maxIndexInclusive`. Use a negative value for `maxIndexInclusive` to include all elements.

func (Images) Dxs added in v0.36.5

func (imgs Images) Dxs() []int

func (Images) DyMax added in v0.36.5

func (imgs Images) DyMax() int

func (Images) DyMin added in v0.36.5

func (imgs Images) DyMin() int

func (Images) DySum added in v0.36.5

func (imgs Images) DySum(maxIndexInclusive int) int

DySum returns the sum of heights up to and including `maxIndexInclusive`. Use a negative value for `maxIndexInclusive` to include all elements.

func (Images) Dys added in v0.36.5

func (imgs Images) Dys() []int

func (Images) Stats added in v0.36.5

func (imgs Images) Stats() ImagesStats

type ImagesStats added in v0.36.5

type ImagesStats struct {
	Dxs   []int
	DxMax int
	DxMin int
	DxSum int
	Dys   []int
	DyMax int
	DyMin int
	DySum int
}

type JPEGEncodeOptions added in v0.49.0

type JPEGEncodeOptions struct {
	Options            *jpeg.Options
	Exif               []byte
	ReadFilenameRegexp *regexp.Regexp
	WriteExtension     string
}

func (JPEGEncodeOptions) ReadFilenameRegexpOrDefault added in v0.49.0

func (opts JPEGEncodeOptions) ReadFilenameRegexpOrDefault() *regexp.Regexp

func (JPEGEncodeOptions) WriteExtensionOrDefault added in v0.49.0

func (opts JPEGEncodeOptions) WriteExtensionOrDefault() string

type Matrix

type Matrix [][]image.Image

func MatrixRead

func MatrixRead(imglocations [][]string) (Matrix, error)

func (Matrix) AddBackgroundColor

func (matrix Matrix) AddBackgroundColor(clr color.Color)

AddBackgroundColor adds a background of `color.Color` to the images. It is is useful when the image has a transparent background. Use `colornames` for more colors, e.g. `colornames.Blue`.

func (Matrix) AddBackgroundColorHex

func (matrix Matrix) AddBackgroundColorHex(hexcolor string) error

func (Matrix) Dimensions added in v0.37.8

func (matrix Matrix) Dimensions() [][]image.Point

func (Matrix) DxSumMax added in v0.37.8

func (matrix Matrix) DxSumMax() int

func (Matrix) DxSumMin added in v0.37.8

func (matrix Matrix) DxSumMin() int

func (Matrix) Merge

func (matrix Matrix) Merge(largerX, largerY bool) image.Image

Merge combines a set of images resizing each row element's height for consistent rows, an each row's width for consistent widths.

type ToPalettedFunc added in v0.36.5

type ToPalettedFunc func(src image.Image) *image.Paletted

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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