~dricottone/image2ascii

9b483cdb475a0647c7e1ea63a372efc7af7ab8fb — qeesung 6 years ago 1c6a4c0
add the resize package
1 files changed, 44 insertions(+), 0 deletions(-)

A resize/resize.go
A resize/resize.go => resize/resize.go +44 -0
@@ 0,0 1,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"
}