convert

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2018 License: MIT Imports: 10 Imported by: 57

Documentation

Overview

Package convert can convert a image to ascii string or matrix

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	Ratio:           1,
	FixedWidth:      -1,
	FixedHeight:     -1,
	FitScreen:       true,
	Colored:         true,
	Reversed:        false,
	StretchedScreen: false,
}

DefaultOptions for convert image

Functions

func OpenImageFile added in v1.0.1

func OpenImageFile(imageFilename string) (image.Image, error)

OpenImageFile open a image and return a image object

Types

type Converter added in v1.0.1

type Converter interface {
	Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string
	Image2ASCIIString(image image.Image, options *Options) string
	ImageFile2ASCIIMatrix(imageFilename string, option *Options) []string
	ImageFile2ASCIIString(imageFilename string, option *Options) string
	Image2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.CharPixel
	ImageFile2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.CharPixel
}

Converter define the convert image basic operations

type ImageConverter added in v1.0.1

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

ImageConverter implement the Convert interface, and responsible to image conversion

func NewImageConverter added in v1.0.1

func NewImageConverter() *ImageConverter

NewImageConverter create a new image converter

func (*ImageConverter) Image2ASCIIMatrix added in v1.0.1

func (converter *ImageConverter) Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string

Image2ASCIIMatrix converts a image to ASCII matrix

func (*ImageConverter) Image2ASCIIString added in v1.0.1

func (converter *ImageConverter) Image2ASCIIString(image image.Image, options *Options) string

Image2ASCIIString converts a image to ascii matrix, and the join the matrix to a string

func (*ImageConverter) Image2CharPixelMatrix added in v1.0.1

func (converter *ImageConverter) Image2CharPixelMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.CharPixel

Image2CharPixelMatrix convert a image to a pixel ascii matrix

func (*ImageConverter) ImageFile2ASCIIMatrix added in v1.0.1

func (converter *ImageConverter) ImageFile2ASCIIMatrix(imageFilename string, option *Options) []string

ImageFile2ASCIIMatrix converts a image file to ascii matrix

func (*ImageConverter) ImageFile2ASCIIString added in v1.0.1

func (converter *ImageConverter) ImageFile2ASCIIString(imageFilename string, option *Options) string

ImageFile2ASCIIString converts a image file to ascii string

Example

ExampleImage2ASCIIMatrix is example

converter := NewImageConverter()
imageFilename := "testdata/3x3_white.png"
convertOptions := DefaultOptions
convertOptions.FitScreen = false
convertOptions.Colored = false
asciiString := converter.ImageFile2ASCIIString(imageFilename, &convertOptions)
fmt.Println(asciiString)
/* Output:
@@@
@@@
@@@
*/
*/
Output:

@@@
@@@
@@@

func (*ImageConverter) ImageFile2CharPixelMatrix added in v1.0.1

func (converter *ImageConverter) ImageFile2CharPixelMatrix(imageFilename string, imageConvertOptions *Options) [][]ascii.CharPixel

ImageFile2CharPixelMatrix convert a image to a pixel ascii matrix

type ImageResizeHandler added in v1.0.1

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

ImageResizeHandler implement the ResizeHandler interface and responsible for image resizing

func (*ImageResizeHandler) CalcFitSize added in v1.0.1

func (handler *ImageResizeHandler) CalcFitSize(width, height, toBeFitWidth, toBeFitHeight float64) (fitWidth, fitHeight int)

CalcFitSize through the given length and width , Calculation is able to match the length and width of the specified size, and is proportional scaling.

func (*ImageResizeHandler) CalcFitSizeRatio added in v1.0.1

func (handler *ImageResizeHandler) CalcFitSizeRatio(width, height, imageWidth, imageHeight float64) (ratio float64)

CalcFitSizeRatio through the given length and width, the computation can match the optimal scaling ratio of the length and width. In other words, it is able to give a given size rectangle to contain pictures Either match the width first, then scale the length equally, or match the length first, then scale the height equally. More detail please check the example

func (*ImageResizeHandler) CalcProportionalFittingScreenSize added in v1.0.1

func (handler *ImageResizeHandler) CalcProportionalFittingScreenSize(image image.Image) (newWidth, newHeight int, err error)

CalcProportionalFittingScreenSize proportional scale the image so that the terminal can just show the picture.

func (*ImageResizeHandler) ScaleHeightByRatio added in v1.0.1

func (handler *ImageResizeHandler) ScaleHeightByRatio(height float64, ratio float64) int

ScaleHeightByRatio scaled the height by ratio

func (*ImageResizeHandler) ScaleImage added in v1.0.1

func (handler *ImageResizeHandler) ScaleImage(image image.Image, options *Options) (newImage image.Image)

ScaleImage resize the convert to expected size base on the convert options

Example

ExampleScaleImage is scale image example

handler := NewResizeHandler()
imageFilePath := "testdata/cat_2000x1500.jpg"
img, err := OpenImageFile(imageFilePath)
if err != nil {
	log.Fatal("open image file " + imageFilePath + " failed")
}

options := DefaultOptions
options.Colored = false
options.FixedWidth = 200
options.FixedHeight = 100

scaledImage := handler.ScaleImage(img, &options)
sz := scaledImage.Bounds()
fmt.Print(sz.Max.X, sz.Max.Y)
Output:

200 100

func (*ImageResizeHandler) ScaleWidthByRatio added in v1.0.1

func (handler *ImageResizeHandler) ScaleWidthByRatio(width float64, ratio float64) int

ScaleWidthByRatio scaled the width by ratio

type Options

type Options struct {
	Ratio           float64
	FixedWidth      int
	FixedHeight     int
	FitScreen       bool // only work on terminal
	StretchedScreen bool // only work on terminal
	Colored         bool // only work on terminal
	Reversed        bool
}

Options to convert the image to ASCII

type ResizeHandler added in v1.0.1

type ResizeHandler interface {
	ScaleImage(image image.Image, options *Options) (newImage image.Image)
}

ResizeHandler define the operation to resize a image

func NewResizeHandler added in v1.0.1

func NewResizeHandler() ResizeHandler

NewResizeHandler create a new resize handler

Jump to

Keyboard shortcuts

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