Added --only-save and patch for #14

This commit is contained in:
Zoraiz Hassan 2021-10-06 15:32:35 +05:00
parent 39918a97fb
commit f31ac047ea
13 changed files with 161 additions and 215 deletions

View File

@ -415,6 +415,14 @@ This flag takes an RGB value that sets the font color in saved png and gif files
ascii-image-converter [image paths/urls] -s . --font-color 0,0,0 # For black font color ascii-image-converter [image paths/urls] -s . --font-color 0,0,0 # For black font color
``` ```
#### --only-save
Don't print ascii art on the terminal if some saving flag is passed.
```
ascii-image-converter [image paths/urls] -s . --only-save
```
#### --formats #### --formats
Display supported input formats. Display supported input formats.
@ -462,9 +470,8 @@ func main() {
flags.FontFilePath = "./RobotoMono-Regular.ttf" // If file is in current directory flags.FontFilePath = "./RobotoMono-Regular.ttf" // If file is in current directory
flags.SaveBackgroundColor = [3]int{50, 50, 50} flags.SaveBackgroundColor = [3]int{50, 50, 50}
// This MUST be set to true for environments where a terminal isn't available (such as web servers) // Note: For environments where a terminal isn't available (such as web servers), you MUST
// However, for this, one of flags.Width, flags.Height or flags.Dimensions must be set. // specify atleast one of flags.Width, flags.Height or flags.Dimensions
flags.NoTermSizeComparison = true
// Conversion for an image // Conversion for an image
asciiArt, err := aic_package.Convert(filePath, flags) asciiArt, err := aic_package.Convert(filePath, flags)

View File

@ -91,13 +91,17 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes []byte, l
// If a frame is found that is smaller than the first frame, then this gif contains smaller subimages that are // If a frame is found that is smaller than the first frame, then this gif contains smaller subimages that are
// positioned inside the original gif. This behavior isn't supported by this app // positioned inside the original gif. This behavior isn't supported by this app
if firstGifFrameWidth != frameImage.Bounds().Dx() || firstGifFrameHeight != frameImage.Bounds().Dy() { if firstGifFrameWidth != frameImage.Bounds().Dx() || firstGifFrameHeight != frameImage.Bounds().Dy() {
fmt.Printf("Error: GIF contains subimages smaller than default width and height\nProcess aborted because ascii-image-converter doesn't support subimage placement and transparency in GIFs\n\n") if urlImgName == "" {
fmt.Printf("Error: " + gifPath + " contains subimages smaller than default width and height\n\nProcess aborted because ascii-image-converter doesn't support subimage placement and transparency in GIFs\n\n")
} else {
fmt.Printf("Error: " + urlImgName + " contains subimages smaller than default width and height\n\nProcess aborted because ascii-image-converter doesn't support subimage placement and transparency in GIFs\n\n")
}
os.Exit(0) os.Exit(0)
} }
var imgSet [][]imgManip.AsciiPixel var imgSet [][]imgManip.AsciiPixel
imgSet, err = imgManip.ConvertToAsciiPixels(frameImage, dimensions, width, height, flipX, flipY, full, braille, dither, noTermSizeComparison) imgSet, err = imgManip.ConvertToAsciiPixels(frameImage, dimensions, width, height, flipX, flipY, full, braille, dither)
if err != nil { if err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
os.Exit(0) os.Exit(0)
@ -236,25 +240,29 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes []byte, l
gif.EncodeAll(gifFile, outGif) gif.EncodeAll(gifFile, outGif)
fmt.Printf(" \r") fmt.Printf(" \r")
fmt.Println("Saved " + fullPathName)
} }
// Display the gif // Display the gif
loopCount := 0 if !onlySave {
for { loopCount := 0
for i, asciiFrame := range asciiArtSet { for {
clearScreen() for i, asciiFrame := range asciiArtSet {
fmt.Println(asciiFrame) clearScreen()
time.Sleep(time.Duration((time.Second * time.Duration(originalGif.Delay[i])) / 100)) fmt.Println(asciiFrame)
} time.Sleep(time.Duration((time.Second * time.Duration(originalGif.Delay[i])) / 100))
}
// If gif is infinite loop // If gif is infinite loop
if originalGif.LoopCount == 0 { if originalGif.LoopCount == 0 {
continue continue
} }
loopCount++ loopCount++
if loopCount == originalGif.LoopCount { if loopCount == originalGif.LoopCount {
break break
}
} }
} }

View File

@ -43,7 +43,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
return "", fmt.Errorf("can't decode %v: %v", imagePath, err) return "", fmt.Errorf("can't decode %v: %v", imagePath, err)
} }
imgSet, err := imgManip.ConvertToAsciiPixels(imData, dimensions, width, height, flipX, flipY, full, braille, dither, noTermSizeComparison) imgSet, err := imgManip.ConvertToAsciiPixels(imData, dimensions, width, height, flipX, flipY, full, braille, dither)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -67,6 +67,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
saveImagePath, saveImagePath,
imagePath, imagePath,
urlImgName, urlImgName,
onlySave,
); err != nil { ); err != nil {
return "", fmt.Errorf("can't save file: %v", err) return "", fmt.Errorf("can't save file: %v", err)
@ -80,6 +81,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
imagePath, imagePath,
saveTxtPath, saveTxtPath,
urlImgName, urlImgName,
onlySave,
); err != nil { ); err != nil {
return "", fmt.Errorf("can't save file: %v", err) return "", fmt.Errorf("can't save file: %v", err)
@ -89,5 +91,8 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
ascii := flattenAscii(asciiSet, colored || grayscale, false) ascii := flattenAscii(asciiSet, colored || grayscale, false)
result := strings.Join(ascii, "\n") result := strings.Join(ascii, "\n")
if onlySave {
return "", nil
}
return result, nil return result, nil
} }

View File

@ -39,28 +39,28 @@ import (
// Can be sent directly to ConvertImage() for default ascii art // Can be sent directly to ConvertImage() for default ascii art
func DefaultFlags() Flags { func DefaultFlags() Flags {
return Flags{ return Flags{
Complex: false, Complex: false,
Dimensions: nil, Dimensions: nil,
Width: 0, Width: 0,
Height: 0, Height: 0,
SaveTxtPath: "", SaveTxtPath: "",
SaveImagePath: "", SaveImagePath: "",
SaveGifPath: "", SaveGifPath: "",
Negative: false, Negative: false,
Colored: false, Colored: false,
CharBackgroundColor: false, CharBackgroundColor: false,
Grayscale: false, Grayscale: false,
CustomMap: "", CustomMap: "",
FlipX: false, FlipX: false,
FlipY: false, FlipY: false,
Full: false, Full: false,
FontFilePath: "", FontFilePath: "",
FontColor: [3]int{255, 255, 255}, FontColor: [3]int{255, 255, 255},
SaveBackgroundColor: [3]int{0, 0, 0}, SaveBackgroundColor: [3]int{0, 0, 0},
Braille: false, Braille: false,
Threshold: 128, Threshold: 128,
Dither: false, Dither: false,
NoTermSizeComparison: false, OnlySave: false,
} }
} }
@ -96,7 +96,7 @@ func Convert(filePath string, flags Flags) (string, error) {
braille = flags.Braille braille = flags.Braille
threshold = flags.Threshold threshold = flags.Threshold
dither = flags.Dither dither = flags.Dither
noTermSizeComparison = flags.NoTermSizeComparison onlySave = flags.OnlySave
// Declared at the start since some variables are initially used in conditional blocks // Declared at the start since some variables are initially used in conditional blocks
var ( var (

View File

@ -17,6 +17,7 @@ limitations under the License.
package aic_package package aic_package
import ( import (
"fmt"
"image" "image"
"image/color" "image/color"
@ -48,7 +49,7 @@ images will considerably decrease ascii art quality because of smaller font size
Size of resulting image may also be considerably larger than original image. Size of resulting image may also be considerably larger than original image.
*/ */
func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImagePath, imagePath, urlImgName string) error { func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImagePath, imagePath, urlImgName string, onlySave bool) error {
constant := 14.0 constant := 14.0
@ -138,5 +139,9 @@ func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImageP
return err return err
} }
if onlySave {
fmt.Println("Saved " + fullPathName)
}
return dc.SavePNG(fullPathName) return dc.SavePNG(fullPathName)
} }

View File

@ -28,7 +28,7 @@ import (
imgManip "github.com/TheZoraiz/ascii-image-converter/image_manipulation" imgManip "github.com/TheZoraiz/ascii-image-converter/image_manipulation"
) )
func saveAsciiArt(asciiSet [][]imgManip.AsciiChar, imagePath, savePath, urlImgName string) error { func saveAsciiArt(asciiSet [][]imgManip.AsciiChar, imagePath, savePath, urlImgName string, onlySave bool) error {
// To make sure uncolored ascii art is the one saved as .txt // To make sure uncolored ascii art is the one saved as .txt
saveAscii := flattenAscii(asciiSet, false, true) saveAscii := flattenAscii(asciiSet, false, true)
@ -46,7 +46,13 @@ func saveAsciiArt(asciiSet [][]imgManip.AsciiChar, imagePath, savePath, urlImgNa
// If path exists // If path exists
if _, err := os.Stat(savePath); !os.IsNotExist(err) { if _, err := os.Stat(savePath); !os.IsNotExist(err) {
return ioutil.WriteFile(savePath+saveFileName, []byte(strings.Join(saveAscii, "\n")), 0666) err := ioutil.WriteFile(savePath+saveFileName, []byte(strings.Join(saveAscii, "\n")), 0666)
if err != nil {
return err
} else if onlySave {
fmt.Println("Saved " + savePath + saveFileName)
}
return nil
} else { } else {
return fmt.Errorf("save path %v does not exist", savePath) return fmt.Errorf("save path %v does not exist", savePath)
} }

View File

@ -99,36 +99,32 @@ type Flags struct {
// is meant for braille art. Therefore, it will be ignored if Flags.Braille is false // is meant for braille art. Therefore, it will be ignored if Flags.Braille is false
Dither bool Dither bool
// Set this to true to disable comparing ascii art size to terminal. However, at least // If Flags.SaveImagePath, Flags.SaveTxtPath or Flags.SaveGifPath are set, then don't
// one of Flags.Width, Flags.Height or Flags.Dimensions should be passed to keep it from // print on terminal
// throwing an error. OnlySave bool
//
// Note: This option is added for using the library in an environment without terminals (such as web servers).
// Furthermore, coloring options will not work outside of a terminal environment.
NoTermSizeComparison bool
} }
var ( var (
dimensions []int dimensions []int
width int width int
height int height int
complex bool complex bool
saveTxtPath string saveTxtPath string
saveImagePath string saveImagePath string
saveGifPath string saveGifPath string
grayscale bool grayscale bool
negative bool negative bool
colored bool colored bool
colorBg bool colorBg bool
customMap string customMap string
flipX bool flipX bool
flipY bool flipY bool
full bool full bool
fontPath string fontPath string
fontColor [3]int fontColor [3]int
saveBgColor [3]int saveBgColor [3]int
braille bool braille bool
threshold int threshold int
dither bool dither bool
noTermSizeComparison bool onlySave bool
) )

View File

@ -52,12 +52,13 @@ var (
braille bool braille bool
threshold int threshold int
dither bool dither bool
onlySave bool
// Root commands // Root commands
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "ascii-image-converter [image paths/urls]", Use: "ascii-image-converter [image paths/urls]",
Short: "Converts images and gifs into ascii art", Short: "Converts images and gifs into ascii art",
Version: "1.10.0", Version: "1.11.0",
Long: "This tool converts images into ascii art and prints them on the terminal.\nFurther configuration can be managed with flags.", 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 // Not RunE since help text is getting larger and seeing it for every error impacts user experience
@ -68,28 +69,28 @@ var (
} }
flags := aic_package.Flags{ flags := aic_package.Flags{
Complex: complex, Complex: complex,
Dimensions: dimensions, Dimensions: dimensions,
Width: width, Width: width,
Height: height, Height: height,
SaveTxtPath: saveTxtPath, SaveTxtPath: saveTxtPath,
SaveImagePath: saveImagePath, SaveImagePath: saveImagePath,
SaveGifPath: saveGifPath, SaveGifPath: saveGifPath,
Negative: negative, Negative: negative,
Colored: colored, Colored: colored,
CharBackgroundColor: colorBg, CharBackgroundColor: colorBg,
Grayscale: grayscale, Grayscale: grayscale,
CustomMap: customMap, CustomMap: customMap,
FlipX: flipX, FlipX: flipX,
FlipY: flipY, FlipY: flipY,
Full: full, Full: full,
FontFilePath: fontFile, FontFilePath: fontFile,
FontColor: [3]int{fontColor[0], fontColor[1], fontColor[2]}, FontColor: [3]int{fontColor[0], fontColor[1], fontColor[2]},
SaveBackgroundColor: [3]int{saveBgColor[0], saveBgColor[1], saveBgColor[2]}, SaveBackgroundColor: [3]int{saveBgColor[0], saveBgColor[1], saveBgColor[2]},
Braille: braille, Braille: braille,
Threshold: threshold, Threshold: threshold,
Dither: dither, Dither: dither,
NoTermSizeComparison: false, OnlySave: onlySave,
} }
for _, imagePath := range args { for _, imagePath := range args {
@ -106,7 +107,9 @@ var (
return return
} }
} }
fmt.Println() if !onlySave {
fmt.Println()
}
} }
}, },
} }
@ -149,6 +152,7 @@ func init() {
rootCmd.PersistentFlags().IntSliceVar(&saveBgColor, "save-bg", nil, "Set background color for --save-img\nand --save-gif flags\nPass an RGB value\ne.g. --save-bg 255,255,255\n(Defaults to 0,0,0)\n") rootCmd.PersistentFlags().IntSliceVar(&saveBgColor, "save-bg", nil, "Set background color for --save-img\nand --save-gif flags\nPass an RGB value\ne.g. --save-bg 255,255,255\n(Defaults to 0,0,0)\n")
rootCmd.PersistentFlags().StringVar(&fontFile, "font", "", "Set font for --save-img and --save-gif flags\nPass file path to font .ttf file\ne.g. --font ./RobotoMono-Regular.ttf\n(Defaults to Hack-Regular for ascii and\n DejaVuSans-Oblique for braille)\n") rootCmd.PersistentFlags().StringVar(&fontFile, "font", "", "Set font for --save-img and --save-gif flags\nPass file path to font .ttf file\ne.g. --font ./RobotoMono-Regular.ttf\n(Defaults to Hack-Regular for ascii and\n DejaVuSans-Oblique for braille)\n")
rootCmd.PersistentFlags().IntSliceVar(&fontColor, "font-color", nil, "Set font color for terminal as well as\n--save-img and --save-gif flags\nPass an RGB value\ne.g. --font-color 0,0,0\n(Defaults to 255,255,255)\n") rootCmd.PersistentFlags().IntSliceVar(&fontColor, "font-color", nil, "Set font color for terminal as well as\n--save-img and --save-gif flags\nPass an RGB value\ne.g. --font-color 0,0,0\n(Defaults to 255,255,255)\n")
rootCmd.PersistentFlags().BoolVar(&onlySave, "only-save", false, "Don't print ascii art on terminal\nif some saving flag is passed\n")
rootCmd.PersistentFlags().BoolVar(&formatsTrue, "formats", false, "Display supported input formats\n") rootCmd.PersistentFlags().BoolVar(&formatsTrue, "formats", false, "Display supported input formats\n")
rootCmd.PersistentFlags().BoolP("help", "h", false, "Help for "+rootCmd.Name()+"\n") rootCmd.PersistentFlags().BoolP("help", "h", false, "Help for "+rootCmd.Name()+"\n")

View File

@ -19,8 +19,6 @@ package cmd
import ( import (
"fmt" "fmt"
"path" "path"
"github.com/TheZoraiz/ascii-image-converter/aic_package/winsize"
) )
// Check input and flag values for detecting errors or invalid inputs // Check input and flag values for detecting errors or invalid inputs
@ -40,12 +38,12 @@ func checkInputAndFlags(args []string) bool {
} }
} }
if gifPresent && nonGifPresent { if gifPresent && nonGifPresent && !onlySave {
fmt.Printf("Error: There are other inputs along with GIFs\nDue to the potential looping nature of GIFs, non-GIFs must not be supplied alongside\n\n") fmt.Printf("Error: There are other inputs along with GIFs\nDue to the potential looping nature of GIFs, non-GIFs must not be supplied alongside\n\n")
return true return true
} }
if gifCount > 1 { if gifCount > 1 && !onlySave {
fmt.Printf("Error: There are multiple GIFs supplied\nDue to the potential looping nature of GIFs, only one GIF per command is supported\n\n") fmt.Printf("Error: There are multiple GIFs supplied\nDue to the potential looping nature of GIFs, only one GIF per command is supported\n\n")
return true return true
} }
@ -83,18 +81,6 @@ func checkInputAndFlags(args []string) bool {
fmt.Printf("Error: invalid values for dimensions\n\n") fmt.Printf("Error: invalid values for dimensions\n\n")
return true return true
} }
defaultTermWidth, _, err := winsize.GetTerminalSize()
if err != nil {
fmt.Printf("Error: %v\n\n", err)
return true
}
defaultTermWidth -= 1
if dimensions[0] > defaultTermWidth {
fmt.Printf("Error: set width must be lower than terminal width\n\n")
return true
}
} }
if width != 0 || height != 0 { if width != 0 || height != 0 {
@ -102,21 +88,9 @@ func checkInputAndFlags(args []string) bool {
if width != 0 && height != 0 { if width != 0 && height != 0 {
fmt.Printf("Error: both --width and --height can't be set. Use --dimensions instead\n\n") fmt.Printf("Error: both --width and --height can't be set. Use --dimensions instead\n\n")
return true return true
} else { } else {
defaultTermWidth, _, err := winsize.GetTerminalSize()
if err != nil {
fmt.Printf("Error: %v\n\n", err)
return true
}
// Check if set width exceeds terminal
defaultTermWidth -= 1
if width > defaultTermWidth {
fmt.Printf("Error: set width must be lower than terminal width\n\n")
return true
}
if width < 0 { if width < 0 {
fmt.Printf("Error: invalid value for width\n\n") fmt.Printf("Error: invalid value for width\n\n")
return true return true
@ -185,5 +159,10 @@ func checkInputAndFlags(args []string) bool {
return true return true
} }
if (saveTxtPath == "" && saveImagePath == "" && saveGifPath == "") && onlySave {
fmt.Printf("Error: you need to supply one of --save-img, --save-txt or --save-gif for using --only-save\n\n")
return true
}
return false return false
} }

View File

@ -124,13 +124,14 @@ func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, grayscale, co
var char AsciiChar var char AsciiChar
char.Simple = chosenTable[tempInt] asciiChar := chosenTable[tempInt]
char.Simple = asciiChar
var err error var err error
if colorBg { if colorBg {
char.OriginalColor, err = getColoredCharForTerm(uint8(r), uint8(g), uint8(b), chosenTable[tempInt], true) char.OriginalColor, err = getColoredCharForTerm(uint8(r), uint8(g), uint8(b), asciiChar, true)
} else { } else {
char.OriginalColor, err = getColoredCharForTerm(uint8(r), uint8(g), uint8(b), chosenTable[tempInt], false) char.OriginalColor, err = getColoredCharForTerm(uint8(r), uint8(g), uint8(b), asciiChar, false)
} }
if (colored || grayscale) && err != nil { if (colored || grayscale) && err != nil {
return nil, err return nil, err
@ -143,9 +144,9 @@ func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, grayscale, co
fcB := fontColor[2] fcB := fontColor[2]
if colorBg { if colorBg {
char.SetColor, err = getColoredCharForTerm(uint8(fcR), uint8(fcG), uint8(fcB), chosenTable[tempInt], true) char.SetColor, err = getColoredCharForTerm(uint8(fcR), uint8(fcG), uint8(fcB), asciiChar, true)
} else { } else {
char.SetColor, err = getColoredCharForTerm(uint8(fcR), uint8(fcG), uint8(fcB), chosenTable[tempInt], false) char.SetColor, err = getColoredCharForTerm(uint8(fcR), uint8(fcG), uint8(fcB), asciiChar, false)
} }
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -34,16 +34,10 @@ getting numeric data for ASCII character comparison.
The returned 2D AsciiPixel slice contains each corresponding pixel's values The returned 2D AsciiPixel slice contains each corresponding pixel's values
*/ */
func ConvertToAsciiPixels(img image.Image, dimensions []int, width, height int, flipX, flipY, full, isBraille, dither, noTermSizeComparison bool) ([][]AsciiPixel, error) { func ConvertToAsciiPixels(img image.Image, dimensions []int, width, height int, flipX, flipY, full, isBraille, dither bool) ([][]AsciiPixel, error) {
var smallImg image.Image smallImg, err := resizeImage(img, full, isBraille, dimensions, width, height)
var err error
if noTermSizeComparison {
smallImg, err = resizeImageNoTerm(img, isBraille, dimensions, width, height)
} else {
smallImg, err = resizeImage(img, full, isBraille, dimensions, width, height)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -45,16 +45,16 @@ func resizeImage(img image.Image, full, isBraille bool, dimensions []int, width,
var asciiWidth, asciiHeight int var asciiWidth, asciiHeight int
var smallImg image.Image var smallImg image.Image
terminalWidth, terminalHeight, err := winsize.GetTerminalSize()
if err != nil {
return nil, err
}
imgWidth := float64(img.Bounds().Dx()) imgWidth := float64(img.Bounds().Dx())
imgHeight := float64(img.Bounds().Dy()) imgHeight := float64(img.Bounds().Dy())
aspectRatio := imgWidth / imgHeight aspectRatio := imgWidth / imgHeight
if full { if full {
terminalWidth, _, err := winsize.GetTerminalSize()
if err != nil {
return nil, err
}
asciiWidth = terminalWidth - 1 asciiWidth = terminalWidth - 1
asciiHeight = int(float64(asciiWidth) / aspectRatio) asciiHeight = int(float64(asciiWidth) / aspectRatio)
asciiHeight = int(0.5 * float64(asciiHeight)) asciiHeight = int(0.5 * float64(asciiHeight))
@ -62,10 +62,6 @@ func resizeImage(img image.Image, full, isBraille bool, dimensions []int, width,
} else if (width != 0 || height != 0) && len(dimensions) == 0 { } else if (width != 0 || height != 0) && len(dimensions) == 0 {
// If either width or height is set and dimensions aren't given // If either width or height is set and dimensions aren't given
if width > terminalWidth-1 {
return nil, fmt.Errorf("set width must be lower than terminal width")
}
if width != 0 && height == 0 { if width != 0 && height == 0 {
// If width is set and height is not set, use width to calculate aspect ratio // If width is set and height is not set, use width to calculate aspect ratio
@ -88,17 +84,18 @@ func resizeImage(img image.Image, full, isBraille bool, dimensions []int, width,
asciiWidth = 1 asciiWidth = 1
} }
if asciiWidth > terminalWidth-1 {
return nil, fmt.Errorf("width calculated with aspect ratio exceeds terminal width")
}
} else { } else {
return nil, fmt.Errorf("both width and height can't be set. Use dimensions instead") return nil, fmt.Errorf("error: both width and height can't be set. Use dimensions instead")
} }
} else if len(dimensions) == 0 { } else if len(dimensions) == 0 {
// This condition calculates aspect ratio according to terminal height // This condition calculates aspect ratio according to terminal height
terminalWidth, terminalHeight, err := winsize.GetTerminalSize()
if err != nil {
return nil, err
}
asciiHeight = terminalHeight - 1 asciiHeight = terminalHeight - 1
asciiWidth = int(float64(asciiHeight) * aspectRatio) asciiWidth = int(float64(asciiHeight) * aspectRatio)
asciiWidth = int(2 * float64(asciiWidth)) asciiWidth = int(2 * float64(asciiWidth))
@ -111,69 +108,13 @@ func resizeImage(img image.Image, full, isBraille bool, dimensions []int, width,
} }
} else { } else {
// Else, set passed dimensions
asciiWidth = dimensions[0] asciiWidth = dimensions[0]
asciiHeight = dimensions[1] asciiHeight = dimensions[1]
} }
// Repeated despite being in cmd/root.go to maintain support for library // Because one braille character has 8 dots (4 rows and 2 columns)
//
// If there are passed dimensions, check whether the width exceeds terminal width
if len(dimensions) > 0 && !full {
if dimensions[0] > terminalWidth-1 {
return nil, fmt.Errorf("set width must be lower than terminal width")
}
}
if isBraille {
asciiWidth *= 2
asciiHeight *= 4
}
smallImg = imaging.Resize(img, asciiWidth, asciiHeight, imaging.Lanczos)
return smallImg, nil
}
func resizeImageNoTerm(img image.Image, isBraille bool, dimensions []int, width, height int) (image.Image, error) {
var asciiWidth, asciiHeight int
var smallImg image.Image
imgWidth := float64(img.Bounds().Dx())
imgHeight := float64(img.Bounds().Dy())
aspectRatio := imgWidth / imgHeight
if (width != 0 || height != 0) && len(dimensions) == 0 {
if width != 0 && height == 0 {
asciiWidth = width
asciiHeight = int(float64(asciiWidth) / aspectRatio)
asciiHeight = int(0.5 * float64(asciiHeight))
if asciiHeight == 0 {
asciiHeight = 1
}
} else if height != 0 && width == 0 {
asciiHeight = height
asciiWidth = int(float64(asciiHeight) * aspectRatio)
asciiWidth = int(2 * float64(asciiWidth))
if asciiWidth == 0 {
asciiWidth = 1
}
} else {
return nil, fmt.Errorf("error: both width and height can't be set. Use dimensions instead")
}
} else if len(dimensions) != 0 {
asciiWidth = dimensions[0]
asciiHeight = dimensions[1]
} else {
return nil, fmt.Errorf("error: at least one of width, height or dimensions should be passed for NoTermSizeComparison")
}
if isBraille { if isBraille {
asciiWidth *= 2 asciiWidth *= 2
asciiHeight *= 4 asciiHeight *= 4

View File

@ -1,6 +1,6 @@
name: ascii-image-converter name: ascii-image-converter
base: core18 base: core18
version: "1.10.0" version: "1.11.0"
summary: Convert images and gifs into ascii art summary: Convert images and gifs into ascii art
description: | description: |
ascii-image-converter is a command-line tool that converts images into ascii art and prints ascii-image-converter is a command-line tool that converts images into ascii art and prints