Error handing for dimensions, bolder font on saved image

This commit is contained in:
Zoraiz 2021-06-03 13:49:13 +05:00
parent 0889006bf2
commit 3d91be1a1b
6 changed files with 17 additions and 10 deletions

Binary file not shown.

Binary file not shown.

View File

@ -40,7 +40,8 @@ import (
"github.com/asaskevich/govalidator"
)
// Return default configuration for flags
// Return default configuration for flags.
// Can be sent directly to ConvertImage() for default ascii art
func DefaultFlags() map[string]interface{} {
return map[string]interface{}{
"complex": false,
@ -141,9 +142,8 @@ func ConvertImage(imagePath string, flags map[string]interface{}) (string, error
return "", fmt.Errorf("can't decode %v: %v", imagePath, err)
}
// x and y are height and width of resulting ascii art
// These numbers are important for creating png image to save
imgSet, x, y, err := imgManip.ConvertToAsciiPixels(imData, dimensions, flipX, flipY)
// Ascii art height and width are important for creating png image to save
imgSet, imgWidth, imgHeight, err := imgManip.ConvertToAsciiPixels(imData, dimensions, flipX, flipY)
if err != nil {
return "", fmt.Errorf("%v", err)
}
@ -152,7 +152,7 @@ func ConvertImage(imagePath string, flags map[string]interface{}) (string, error
// Save ascii art as .png image before printing it, if --save-img flag is passed
if saveImagePath != "" {
if err := createImageToSave(asciiSet, x, y, colored, saveImagePath, imagePath, urlImgName); err != nil {
if err := createImageToSave(asciiSet, imgWidth, imgHeight, colored, saveImagePath, imagePath, urlImgName); err != nil {
return "", fmt.Errorf("can't save file: %v", err)
}
}

View File

@ -17,11 +17,12 @@ limitations under the License.
package aic_package
import (
_ "embed"
"image"
"image/color"
"os"
_ "embed"
imgManip "github.com/TheZoraiz/ascii-image-converter/image_manipulation"
"github.com/golang/freetype/truetype"
@ -32,7 +33,7 @@ import (
// Will work on organizing it into a readable format soon. Apologies...
// To embed font directly into the binary, instead of packaging it as a separate file
//go:embed RobotoMono-Regular.ttf
//go:embed RobotoMono-Bold.ttf
var embeddedFontFile []byte
func createImageToSave(asciiArt [][]imgManip.AsciiChar, x, y int, colored bool, saveImagePath, imagePath, urlImgName string) error {
@ -72,9 +73,9 @@ func createImageToSave(asciiArt [][]imgManip.AsciiChar, x, y int, colored bool,
if err != nil {
return err
}
robotoFontFace := truetype.NewFace(tempFont, &truetype.Options{Size: 21.0})
robotoBoldFontFace := truetype.NewFace(tempFont, &truetype.Options{Size: 21.0})
dc.SetFontFace(robotoFontFace)
dc.SetFontFace(robotoBoldFontFace)
// Font color of text on picture is white by default
dc.SetColor(color.White)

View File

@ -44,7 +44,7 @@ var (
rootCmd = &cobra.Command{
Use: "ascii-image-converter [image paths/urls]",
Short: "Converts images into ascii art",
Version: "1.3.0",
Version: "1.3.1",
Long: "This tool converts images into ascii art and prints them on the terminal.\nFurther configuration can be managed with flags.",
// Not RunE since help text is getting larger and seeing it for every error impacts user experience
@ -71,6 +71,11 @@ var (
return
}
if dimensions != nil && (dimensions[0] < 1 || dimensions[1] < 1) {
fmt.Printf("Error: invalid values for dimensions\n\n")
return
}
flags := map[string]interface{}{
"complex": complex,
"dimensions": dimensions,

View File

@ -139,6 +139,7 @@ func ConvertToAscii(imgSet [][]AsciiPixel, negative bool, colored bool, complex
}
} else {
chosenTable = map[int]string{}
for index, char := range customMap {
chosenTable[index] = string(char)
}