Skip to content

Commit

Permalink
Added --only-save and patch for #14
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoraiz committed Oct 6, 2021
1 parent 39918a9 commit f31ac04
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 214 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

#### --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

Display supported input formats.
Expand Down Expand Up @@ -462,9 +470,8 @@ func main() {
flags.FontFilePath = "./RobotoMono-Regular.ttf" // If file is in current directory
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)
// However, for this, one of flags.Width, flags.Height or flags.Dimensions must be set.
flags.NoTermSizeComparison = true
// Note: For environments where a terminal isn't available (such as web servers), you MUST
// specify atleast one of flags.Width, flags.Height or flags.Dimensions

// Conversion for an image
asciiArt, err := aic_package.Convert(filePath, flags)
Expand Down
40 changes: 24 additions & 16 deletions aic_package/convert_gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// positioned inside the original gif. This behavior isn't supported by this app
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)
}

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 {
fmt.Printf("Error: %v\n", err)
os.Exit(0)
Expand Down Expand Up @@ -236,25 +240,29 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes []byte, l
gif.EncodeAll(gifFile, outGif)

fmt.Printf(" \r")

fmt.Println("Saved " + fullPathName)
}

// Display the gif
loopCount := 0
for {
for i, asciiFrame := range asciiArtSet {
clearScreen()
fmt.Println(asciiFrame)
time.Sleep(time.Duration((time.Second * time.Duration(originalGif.Delay[i])) / 100))
}
if !onlySave {
loopCount := 0
for {
for i, asciiFrame := range asciiArtSet {
clearScreen()
fmt.Println(asciiFrame)
time.Sleep(time.Duration((time.Second * time.Duration(originalGif.Delay[i])) / 100))
}

// If gif is infinite loop
if originalGif.LoopCount == 0 {
continue
}
// If gif is infinite loop
if originalGif.LoopCount == 0 {
continue
}

loopCount++
if loopCount == originalGif.LoopCount {
break
loopCount++
if loopCount == originalGif.LoopCount {
break
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion aic_package/convert_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
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 {
return "", err
}
Expand All @@ -67,6 +67,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
saveImagePath,
imagePath,
urlImgName,
onlySave,
); err != nil {

return "", fmt.Errorf("can't save file: %v", err)
Expand All @@ -80,6 +81,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
imagePath,
saveTxtPath,
urlImgName,
onlySave,
); err != nil {

return "", fmt.Errorf("can't save file: %v", err)
Expand All @@ -89,5 +91,8 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
ascii := flattenAscii(asciiSet, colored || grayscale, false)
result := strings.Join(ascii, "\n")

if onlySave {
return "", nil
}
return result, nil
}
46 changes: 23 additions & 23 deletions aic_package/convert_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ import (
// Can be sent directly to ConvertImage() for default ascii art
func DefaultFlags() Flags {
return Flags{
Complex: false,
Dimensions: nil,
Width: 0,
Height: 0,
SaveTxtPath: "",
SaveImagePath: "",
SaveGifPath: "",
Negative: false,
Colored: false,
CharBackgroundColor: false,
Grayscale: false,
CustomMap: "",
FlipX: false,
FlipY: false,
Full: false,
FontFilePath: "",
FontColor: [3]int{255, 255, 255},
SaveBackgroundColor: [3]int{0, 0, 0},
Braille: false,
Threshold: 128,
Dither: false,
NoTermSizeComparison: false,
Complex: false,
Dimensions: nil,
Width: 0,
Height: 0,
SaveTxtPath: "",
SaveImagePath: "",
SaveGifPath: "",
Negative: false,
Colored: false,
CharBackgroundColor: false,
Grayscale: false,
CustomMap: "",
FlipX: false,
FlipY: false,
Full: false,
FontFilePath: "",
FontColor: [3]int{255, 255, 255},
SaveBackgroundColor: [3]int{0, 0, 0},
Braille: false,
Threshold: 128,
Dither: false,
OnlySave: false,
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ func Convert(filePath string, flags Flags) (string, error) {
braille = flags.Braille
threshold = flags.Threshold
dither = flags.Dither
noTermSizeComparison = flags.NoTermSizeComparison
onlySave = flags.OnlySave

// Declared at the start since some variables are initially used in conditional blocks
var (
Expand Down
7 changes: 6 additions & 1 deletion aic_package/create_ascii_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package aic_package

import (
"fmt"
"image"
"image/color"

Expand Down Expand Up @@ -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.
*/
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

Expand Down Expand Up @@ -138,5 +139,9 @@ func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImageP
return err
}

if onlySave {
fmt.Println("Saved " + fullPathName)
}

return dc.SavePNG(fullPathName)
}
10 changes: 8 additions & 2 deletions aic_package/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
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
saveAscii := flattenAscii(asciiSet, false, true)

Expand All @@ -46,7 +46,13 @@ func saveAsciiArt(asciiSet [][]imgManip.AsciiChar, imagePath, savePath, urlImgNa

// If path exists
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 {
return fmt.Errorf("save path %v does not exist", savePath)
}
Expand Down
54 changes: 25 additions & 29 deletions aic_package/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,32 @@ type Flags struct {
// is meant for braille art. Therefore, it will be ignored if Flags.Braille is false
Dither bool

// Set this to true to disable comparing ascii art size to terminal. However, at least
// one of Flags.Width, Flags.Height or Flags.Dimensions should be passed to keep it from
// throwing an error.
//
// 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
// If Flags.SaveImagePath, Flags.SaveTxtPath or Flags.SaveGifPath are set, then don't
// print on terminal
OnlySave bool
}

var (
dimensions []int
width int
height int
complex bool
saveTxtPath string
saveImagePath string
saveGifPath string
grayscale bool
negative bool
colored bool
colorBg bool
customMap string
flipX bool
flipY bool
full bool
fontPath string
fontColor [3]int
saveBgColor [3]int
braille bool
threshold int
dither bool
noTermSizeComparison bool
dimensions []int
width int
height int
complex bool
saveTxtPath string
saveImagePath string
saveGifPath string
grayscale bool
negative bool
colored bool
colorBg bool
customMap string
flipX bool
flipY bool
full bool
fontPath string
fontColor [3]int
saveBgColor [3]int
braille bool
threshold int
dither bool
onlySave bool
)
52 changes: 28 additions & 24 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ var (
braille bool
threshold int
dither bool
onlySave bool

// Root commands
rootCmd = &cobra.Command{
Use: "ascii-image-converter [image paths/urls]",
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.",

// Not RunE since help text is getting larger and seeing it for every error impacts user experience
Expand All @@ -68,28 +69,28 @@ var (
}

flags := aic_package.Flags{
Complex: complex,
Dimensions: dimensions,
Width: width,
Height: height,
SaveTxtPath: saveTxtPath,
SaveImagePath: saveImagePath,
SaveGifPath: saveGifPath,
Negative: negative,
Colored: colored,
CharBackgroundColor: colorBg,
Grayscale: grayscale,
CustomMap: customMap,
FlipX: flipX,
FlipY: flipY,
Full: full,
FontFilePath: fontFile,
FontColor: [3]int{fontColor[0], fontColor[1], fontColor[2]},
SaveBackgroundColor: [3]int{saveBgColor[0], saveBgColor[1], saveBgColor[2]},
Braille: braille,
Threshold: threshold,
Dither: dither,
NoTermSizeComparison: false,
Complex: complex,
Dimensions: dimensions,
Width: width,
Height: height,
SaveTxtPath: saveTxtPath,
SaveImagePath: saveImagePath,
SaveGifPath: saveGifPath,
Negative: negative,
Colored: colored,
CharBackgroundColor: colorBg,
Grayscale: grayscale,
CustomMap: customMap,
FlipX: flipX,
FlipY: flipY,
Full: full,
FontFilePath: fontFile,
FontColor: [3]int{fontColor[0], fontColor[1], fontColor[2]},
SaveBackgroundColor: [3]int{saveBgColor[0], saveBgColor[1], saveBgColor[2]},
Braille: braille,
Threshold: threshold,
Dither: dither,
OnlySave: onlySave,
}

for _, imagePath := range args {
Expand All @@ -106,7 +107,9 @@ var (
return
}
}
fmt.Println()
if !onlySave {
fmt.Println()
}
}
},
}
Expand Down Expand Up @@ -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().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().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().BoolP("help", "h", false, "Help for "+rootCmd.Name()+"\n")
Expand Down
Loading

0 comments on commit f31ac04

Please sign in to comment.