~dricottone/image2ascii

c5de7795bcb4551edcde5176aaec82d41edc5d78 — qeesung 6 years ago 9330e7c
add colored the asicc output
6 files changed, 42 insertions(+), 21 deletions(-)

M Gopkg.lock
M Gopkg.toml
M ascii/ascii.go
M ascii/option.go
M convert/convert.go
M image2ascii.go
M Gopkg.lock => Gopkg.lock +9 -0
@@ 2,6 2,14 @@


[[projects]]
  branch = "master"
  digest = "1:f8c5815a76aec3fac7f6d67911eccd3a93841294052b0e81bca1ff325a1ee62d"
  name = "github.com/aybabtme/rgbterm"
  packages = ["."]
  pruneopts = "UT"
  revision = "cc83f3b3ce5911279513a46d6d3316d67bedaa54"

[[projects]]
  digest = "1:0981502f9816113c9c8c4ac301583841855c8cf4da8c72f696b3ebedf6d0e4e5"
  name = "github.com/mattn/go-isatty"
  packages = ["."]


@@ 37,6 45,7 @@
  analyzer-name = "dep"
  analyzer-version = 1
  input-imports = [
    "github.com/aybabtme/rgbterm",
    "github.com/mattn/go-isatty",
    "github.com/nfnt/resize",
    "github.com/wayneashleyberry/terminal-dimensions",

M Gopkg.toml => Gopkg.toml +4 -0
@@ 40,3 40,7 @@
[[constraint]]
  name = "github.com/wayneashleyberry/terminal-dimensions"
  version = "1.0.0"

[[constraint]]
  branch = "master"
  name = "github.com/aybabtme/rgbterm"

M ascii/ascii.go => ascii/ascii.go +13 -4
@@ 4,6 4,7 @@
package ascii

import (
	"github.com/aybabtme/rgbterm"
	"image/color"
	"math"
	"reflect"


@@ 14,8 15,8 @@ func ConvertPixelToASCII(pixel color.Color, options *Options) string {
	defaultOptions := NewOptions()
	defaultOptions.mergeOptions(options)

	if defaultOptions.reverse {
		defaultOptions.pixels = reverse(defaultOptions.pixels)
	if defaultOptions.Reverse {
		defaultOptions.Pixels = reverse(defaultOptions.Pixels)
	}

	r := reflect.ValueOf(pixel).FieldByName("R").Uint()


@@ 25,8 26,11 @@ func ConvertPixelToASCII(pixel color.Color, options *Options) string {
	value := intensity(r, g, b, a)

	// Choose the char
	precision := float64(255 * 3 / (len(options.pixels) - 1))
	rawChar := options.pixels[roundValue(float64(value)/precision)]
	precision := float64(255 * 3 / (len(options.Pixels) - 1))
	rawChar := options.Pixels[roundValue(float64(value)/precision)]
	if options.Colored {
		return decorateWithColor(r, g, b, rawChar)
	}
	return string([]byte{rawChar})
}



@@ 45,3 49,8 @@ func reverse(numbers []byte) []byte {
func intensity(r, g, b, a uint64) uint64 {
	return (r + g + b) * a / 255
}

func decorateWithColor(r, g, b uint64, rawChar byte) string {
	coloredChar := rgbterm.FgString(string([]byte{rawChar}), uint8(r), uint8(g), uint8(b))
	return coloredChar
}

M ascii/option.go => ascii/option.go +9 -15
@@ 2,20 2,16 @@ package ascii

// Convert options
type Options struct {
	pixels  []byte
	reverse bool
	colored bool
	bg      bool
	fg      bool
	Pixels  []byte
	Reverse bool
	Colored bool
}

// Default options
var DefaultOptions = Options{
	pixels:  []byte(" .,:;i1tfLCG08@"),
	reverse: false,
	colored: true,
	bg:      false,
	fg:      true,
	Pixels:  []byte(" .,:;i1tfLCG08@"),
	Reverse: false,
	Colored: true,
}

// Create a new options


@@ 25,9 21,7 @@ func NewOptions() Options {

// Merge options
func (options *Options) mergeOptions(newOptions *Options) {
	options.pixels = append([]byte{}, newOptions.pixels...)
	options.reverse = newOptions.reverse
	options.colored = newOptions.colored
	options.bg = newOptions.bg
	options.fg = newOptions.fg
	options.Pixels = append([]byte{}, newOptions.Pixels...)
	options.Reverse = newOptions.Reverse
	options.Colored = newOptions.Colored
}

M convert/convert.go => convert/convert.go +4 -2
@@ 15,12 15,13 @@ type Options struct {
	ExpectedWidth  int
	ExpectedHeight int
	FitScreen      bool
	Colored bool
}

// Convert a image to ascii matrix
func Image2ASCIIMatrix(image image.Image, options *Options) []string {
func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string {
	// Resize the convert first
	newImage := ScaleImage(image, options)
	newImage := ScaleImage(image, imageConvertOptions)
	sz := newImage.Bounds()
	newWidth := sz.Max.Y
	newHeight := sz.Max.X


@@ 30,6 31,7 @@ func Image2ASCIIMatrix(image image.Image, options *Options) []string {
			pixel := color.NRGBAModel.Convert(newImage.At(j, i))
			// Convert the pixel to ascii char
			pixelConvertOptions := ascii.NewOptions()
			pixelConvertOptions.Colored = imageConvertOptions.Colored
			rawChar := ascii.ConvertPixelToASCII(pixel, &pixelConvertOptions)
			rawCharValues = append(rawCharValues, rawChar)
		}

M image2ascii.go => image2ascii.go +3 -0
@@ 14,6 14,7 @@ var ratio float64
var expectedWidth int
var expectedHeight int
var fitScreen bool
var colored bool

func init() {
	flag.StringVar(&imageFilename, "f", "", "Image filename to be convert")


@@ 21,6 22,7 @@ func init() {
	flag.IntVar(&expectedWidth, "w", -1, "Expected image width, -1 for image default width")
	flag.IntVar(&expectedHeight, "g", -1, "Expected image height, -1 for image default height")
	flag.BoolVar(&fitScreen, "s", true, "Fit the terminal screen, ignored when use -w, -g, -r")
	flag.BoolVar(&colored, "c", true, "Colored the ascii when output to the terminal")
	flag.Usage = usage
}



@@ 37,6 39,7 @@ func main() {
		ExpectedHeight: expectedHeight,
		ExpectedWidth:  expectedWidth,
		FitScreen:      fitScreen,
		Colored:        colored,
	}
	fmt.Print(convert.ImageFile2ASCIIString(imageFilename, convertOptions))
}