~dricottone/image2ascii

ref: 4baed2ab2306694338792e5648bfb743accad05a image2ascii/resize/resize.go -rw-r--r-- 961 bytes
4baed2ab — 秦世成 Update README.md 6 years ago
                                                                                
9b483cdb 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
// The resize package resize the image to expected size
// base on the ratio, for the most matched display
package resize

import (
	"github.com/nfnt/resize"
	"image"
	"image/jpeg"
	"log"
	"os"
	"runtime"
)

// Resize the convert to expected size base on the ratio
func ScaleImage(image image.Image, ratio float64) (newImage image.Image) {
	sz := image.Bounds()
	newWidth := int(float64(sz.Max.X) * ratio)
	newHeight := int(float64(sz.Max.Y) * ratio * charWidth())

	newImage = resize.Resize(uint(newWidth), uint(newHeight), image, resize.Lanczos3)
	out, err := os.Create("test_resized.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// write new image to file
	jpeg.Encode(out, newImage, nil)
	return
}

// Get the terminal char width on different system
func charWidth() float64 {
	if isWindows() {
		return 0.714
	} else {
		return 0.625
	}
}

// Check if current system is windows
func isWindows() bool {
	return runtime.GOOS == "windows"
}