~dricottone/image2ascii

ref: cc4686e71951e548a55097a1922908a4e527722d image2ascii/image2ascii.go -rw-r--r-- 2.3 KiB
cc4686e7 — 秦世成 Update README.md 6 years ago
                                                                                
3c5ecdf9 qeesung
e6d60b68 qeesung
3c5ecdf9 qeesung
9e6742da qeesung
3c5ecdf9 qeesung
9e6742da qeesung
c5de7795 qeesung
32c6345e qeesung
3c5ecdf9 qeesung
9e6742da qeesung
3c5ecdf9 qeesung
9e6742da qeesung
4ad980ef qeesung
9e6742da qeesung
3c5ecdf9 qeesung
e6d60b68 qeesung
4ad980ef qeesung
e6d60b68 qeesung
3c5ecdf9 qeesung
e6d60b68 qeesung
228de409 qeesung
e6d60b68 qeesung
228de409 qeesung
3c5ecdf9 qeesung
9e6742da qeesung
3c5ecdf9 qeesung
e6d60b68 qeesung
3c5ecdf9 qeesung
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
	"errors"
	"flag"
	"fmt"
	"github.com/qeesung/image2ascii/convert"
	_ "image/jpeg"
	_ "image/png"
	"os"
)

var imageFilename string
var ratio float64
var fixedWidth int
var fixedHeight int
var fitScreen bool
var stretchedScreen bool
var colored bool
var reversed bool

var convertDefaultOptions = convert.DefaultOptions

func init() {
	flag.StringVar(&imageFilename,
		"f",
		"docs/images/lufei.jpg",
		"Image filename to be convert")
	flag.Float64Var(&ratio,
		"r",
		convertDefaultOptions.Ratio,
		"Ratio to scale the image, ignored when use -w or -g")
	flag.IntVar(&fixedWidth,
		"w",
		convertDefaultOptions.FixedWidth,
		"Expected image width, -1 for image default width")
	flag.IntVar(&fixedHeight,
		"g",
		convertDefaultOptions.FixedHeight,
		"Expected image height, -1 for image default height")
	flag.BoolVar(&fitScreen,
		"s",
		convertDefaultOptions.FitScreen,
		"Fit the terminal screen, ignored when use -w, -g, -r")
	flag.BoolVar(&colored,
		"c",
		convertDefaultOptions.Colored,
		"Colored the ascii when output to the terminal")
	flag.BoolVar(&reversed,
		"i",
		convertDefaultOptions.Reversed,
		"Reversed the ascii when output to the terminal")
	flag.BoolVar(&stretchedScreen,
		"t",
		convertDefaultOptions.StretchedScreen,
		"Stretch the picture to overspread the screen")
	flag.Usage = usage
}

func main() {
	flag.Parse()
	if convertOptions, err := parseOptions(); err == nil {
		converter := convert.NewImageConverter()
		fmt.Print(converter.ImageFile2ASCIIString(imageFilename, convertOptions))
	} else {
		usage()
	}
}

func parseOptions() (*convert.Options, error) {
	if imageFilename == "" {
		return nil, errors.New("image file should not be empty")
	}
	// config  the options
	convertOptions := &convert.Options{
		Ratio:           ratio,
		FixedWidth:      fixedWidth,
		FixedHeight:     fixedHeight,
		FitScreen:       fitScreen,
		StretchedScreen: stretchedScreen,
		Colored:         colored,
		Reversed:        reversed,
	}
	return convertOptions, nil
}

func usage() {
	fmt.Fprintf(os.Stderr, `image2ascii version: image2ascii/1.0.0 
>> HomePage: https://github.com/qeesung/image2ascii
>> Issue   : https://github.com/qeesung/image2ascii/issues
>> Author  : qeesung
Usage: image2ascii [-s] -f <filename> -r <ratio> -w <width> -g <height>

Options:
`)
	flag.PrintDefaults()
}