Skip to content

Commit

Permalink
Added --save-bg and --font-color flag
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoraiz committed Aug 9, 2021
1 parent d11ab9f commit 1c8b009
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 274 deletions.
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,31 +333,42 @@ Saves the passed GIF as an ascii art GIF with the name `<image-name>-ascii-art.g
<img src="https://raw.githubusercontent.com/TheZoraiz/ascii-image-converter/master/example_gifs/save.gif">
</p>

#### --save-bg

> **Note:** This flag will be ignored if `--save-img` or `--save-gif` flags are not set
This flag takes an RGB value that sets the background color in saved png and gif files.

```
ascii-image-converter [image paths/urls] -s . --save-bg 255,255,255 # For white background
```

#### --font

> **Note:** This flag will be ignored if `--save-img` or `--save-gif` flags are not set
This flag takes path to a font .ttf file that will be used to set font in saved .png or .gif files if `--save-img` or `--save-gif` flags are set.
This flag takes path to a font .ttf file that will be used to set font in saved png or gif files.

```
ascii-image-converter [image paths/urls] -s . --font /path/to/font-file.ttf
```

#### --formats
#### --font-color

Display supported input formats.
This flag takes an RGB value that sets the font color in saved png and gif files as well as displayed ascii art in terminal.

```
ascii-image-converter --formats
ascii-image-converter [image paths/urls] -s . --font-color 0,0,0 # For black font color
```

<br>
#### --formats

You can combine flags as well. Following command outputs colored and negative ascii art, flips ascii art horizontally and vertically, with fixed 60 by 30 character dimensions, custom defined ascii characters " .-=+#@" and saves a generated image and .txt file in current directory as well.
Display supported input formats.

```
ascii-image-converter [image paths/urls] -Cnxyd 60,30 -m " .-=+#@" -s . --save-txt .
ascii-image-converter --formats
```

<br>

## Library Usage
Expand Down Expand Up @@ -389,20 +400,23 @@ func main() {
// This part is optional.
// You can directly pass default flags variable to Convert() if you wish.
// For clarity, all flags are covered in this example, but you can use specific ones.
flags.Complex = true // Use complex character set
flags.Complex = false // Use complex character set
flags.Dimensions = []int{50, 25} // 50 by 25 ascii art size
flags.SaveTxtPath = "." // Save generated text in same directory
flags.SaveImagePath = "." // Save generated PNG image in same directory
flags.SaveGifPath = "." // If gif was provided, save ascii art gif in same directory
flags.Negative = true // Ascii art will have negative color-depth
flags.Colored = true // Keep colors from original image. This overrides flags.Grayscale
flags.Grayscale = true // Returns grayscale ascii art
flags.SaveGifPath = "." // If gif is provided, save ascii art gif in same directory
flags.Negative = false // Ascii art will have negative color-depth
flags.Colored = true // Keep colors from original image. This overrides flags.Grayscale and flags.FontColor
flags.Grayscale = false // Returns grayscale ascii art. This overrides flags.FontColor
flags.CustomMap = " .-=+#@" // Starting from darkest to brightest shades. This overrides flags.Complex
flags.FlipX = true // Flips ascii art horizontally
flags.FlipY = true // Flips ascii art vertically
flags.Full = true // Display ascii art that fills the terminal width. This overrides flags.Dimensions
flags.FlipX = false // Flips ascii art horizontally
flags.FlipY = false // Flips ascii art vertically
flags.Full = false // Display ascii art that fills the terminal width. This overrides flags.Dimensions
flags.FontFilePath = "./RobotoMono-Regular.ttf" // File path to font .ttf file for saved png and gif files. Ignored if flags.SaveImagePath or flags.SaveGifPath are not set
flags.FontColor = [3]int{0, 0, 0} // Font color for terminal and saved png and gif files.
flags.SaveBackgroundColor = [3]int{255, 255, 255} // Font color for saved png and gif files. Ignored if flags.SaveImagePath or flags.SaveGifPath are not set.

// For an image
// Conversion for an image
asciiArt, err := aic_package.Convert(filePath, flags)
if err != nil {
fmt.Println(err)
Expand Down
4 changes: 2 additions & 2 deletions aic_package/convert_gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes []byte, l
os.Exit(0)
}

asciiCharSet := imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, customMap)
asciiCharSet := imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, customMap, fontColor)
gifFramesSlice[i].asciiCharSet = asciiCharSet
gifFramesSlice[i].delay = originalGif.Delay[i]

ascii := flattenAscii(asciiCharSet, colored || grayscale)
ascii := flattenAscii(asciiCharSet, colored || grayscale, false)

asciiArtSet[i] = strings.Join(ascii, "\n")

Expand Down
4 changes: 2 additions & 2 deletions aic_package/convert_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
return "", err
}

asciiSet := imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, customMap)
asciiSet := imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, customMap, fontColor)

// Save ascii art as .png image before printing it, if --save-img flag is passed
if saveImagePath != "" {
Expand Down Expand Up @@ -77,7 +77,7 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt
}
}

ascii := flattenAscii(asciiSet, colored || grayscale)
ascii := flattenAscii(asciiSet, colored || grayscale, false)
result := strings.Join(ascii, "\n")

return result, nil
Expand Down
109 changes: 19 additions & 90 deletions aic_package/convert_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,100 +36,27 @@ import (
"github.com/golang/freetype/truetype"
)

type Flags struct {
// Set dimensions of ascii art. Accepts a slice of 2 integers
// e.g. []int{60,30}.
// This overrides Flags.Width and Flags.Height
Dimensions []int

// Set width of ascii art while calculating height from aspect ratio.
// Setting this along with Flags.Height will throw an error
Width int

// Set height of ascii art while calculating width from aspect ratio.
// Setting this along with Flags.Width will throw an error
Height int

// Use set of 69 characters instead of the default 10
Complex bool

// Path to save ascii art .txt file
SaveTxtPath string

// Path to save ascii art .png file
SaveImagePath string

// Path to save ascii art .gif file, if gif is passed
SaveGifPath string

// Invert ascii art character mapping as well as colors
Negative bool

// Keep colors from the original image. This uses the True color codes for
// the terminal and will work on saved .png and .gif files as well.
// This overrides Flags.Grayscale
Colored bool

// Keep grayscale colors from the original image. This uses the True color
// codes for the terminal and will work on saved .png and .gif files as well
Grayscale bool

// Pass custom ascii art characters as a string.
// This overrides Flags.Complex
CustomMap string

// Flip ascii art horizontally
FlipX bool

// Flip ascii art vertically
FlipY bool

// Use terminal width to calculate ascii art size while keeping aspect ratio.
// This overrides Flags.Dimensions, Flags.Width and Flags.Height
Full bool

// File path to a font .ttf file to use when saving ascii art gif or png file.
// This will be ignored if Flags.SaveImagePath or Flags.SaveImagePath are not set
FontFilePath string
}

var (
dimensions []int
width int
height int
complex bool
saveTxtPath string
saveImagePath string
saveGifPath string
grayscale bool
negative bool
colored bool
customMap string
flipX bool
flipY bool
full bool
fontPath string
)

// Return default configuration for flags.
// 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,
Grayscale: false,
CustomMap: "",
FlipX: false,
FlipY: false,
Full: false,
FontFilePath: "",
Complex: false,
Dimensions: nil,
Width: 0,
Height: 0,
SaveTxtPath: "",
SaveImagePath: "",
SaveGifPath: "",
Negative: false,
Colored: false,
Grayscale: false,
CustomMap: "",
FlipX: false,
FlipY: false,
Full: false,
FontFilePath: "",
FontColor: [3]int{255, 255, 255},
SaveBackgroundColor: [3]int{0, 0, 0},
}
}

Expand Down Expand Up @@ -159,6 +86,8 @@ func Convert(filePath string, flags Flags) (string, error) {
flipY = flags.FlipY
full = flags.Full
fontPath = flags.FontFilePath
fontColor = flags.FontColor
saveBgColor = flags.SaveBackgroundColor

// Declared at the start since some variables are initially used in conditional blocks
var (
Expand Down
23 changes: 16 additions & 7 deletions aic_package/create_ascii_gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ func createGifFrameToSave(asciiArt [][]imgManip.AsciiChar, img image.Image, colo

dc := gg.NewContext(x, y)

// Set image background as black
dc.SetRGB(0, 0, 0)
// Set image background
dc.SetRGB(
float64(saveBgColor[0])/255,
float64(saveBgColor[1])/255,
float64(saveBgColor[2])/255,
)
dc.Clear()

dc.DrawImage(tempImg, 0, 0)
Expand Down Expand Up @@ -109,12 +113,17 @@ func createGifFrameToSave(asciiArt [][]imgManip.AsciiChar, img image.Image, colo

for _, char := range line {

r := uint8(char.RgbValue[0])
g := uint8(char.RgbValue[1])
b := uint8(char.RgbValue[2])

if colored {
// Simple put, dc.SetColor() sets color for EACH character before printing it
// dc.SetColor() sets color for EACH character before printing it
r := uint8(char.RgbValue[0])
g := uint8(char.RgbValue[1])
b := uint8(char.RgbValue[2])
dc.SetColor(color.RGBA{r, g, b, 255})

} else {
r := uint8(fontColor[0])
g := uint8(fontColor[1])
b := uint8(fontColor[2])
dc.SetColor(color.RGBA{r, g, b, 255})
}

Expand Down
23 changes: 16 additions & 7 deletions aic_package/create_ascii_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImageP

dc := gg.NewContext(imgWidth, imgHeight)

// Set image background as black
dc.SetRGB(0, 0, 0)
// Set image background
dc.SetRGB(
float64(saveBgColor[0])/255,
float64(saveBgColor[1])/255,
float64(saveBgColor[2])/255,
)
dc.Clear()

dc.DrawImage(tempImg, 0, 0)
Expand All @@ -93,12 +97,17 @@ func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImageP

for _, char := range line {

r := uint8(char.RgbValue[0])
g := uint8(char.RgbValue[1])
b := uint8(char.RgbValue[2])

if colored {
// Simply put, dc.SetColor() sets color for EACH character before printing it
// dc.SetColor() sets color for EACH character before printing it
r := uint8(char.RgbValue[0])
g := uint8(char.RgbValue[1])
b := uint8(char.RgbValue[2])
dc.SetColor(color.RGBA{r, g, b, 255})

} else {
r := uint8(fontColor[0])
g := uint8(fontColor[1])
b := uint8(fontColor[2])
dc.SetColor(color.RGBA{r, g, b, 255})
}

Expand Down
13 changes: 10 additions & 3 deletions aic_package/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func saveAsciiArt(asciiSet [][]imgManip.AsciiChar, imagePath, savePath, urlImgName string) error {
// To make sure uncolored ascii art is the one saved as .txt
saveAscii := flattenAscii(asciiSet, false)
saveAscii := flattenAscii(asciiSet, false, true)

saveFileName, err := createSaveFileName(imagePath, urlImgName, "-ascii-art.txt")
if err != nil {
Expand Down Expand Up @@ -75,15 +75,22 @@ func createSaveFileName(imagePath, urlImgName, label string) (string, error) {

// flattenAscii flattens a two-dimensional grid of ascii characters into a one dimension
// of lines of ascii
func flattenAscii(asciiSet [][]imgManip.AsciiChar, colored bool) []string {
func flattenAscii(asciiSet [][]imgManip.AsciiChar, colored, toSaveTxt bool) []string {
var ascii []string

for _, line := range asciiSet {
var tempAscii []string

for i := 0; i < len(line); i++ {
if toSaveTxt {
tempAscii = append(tempAscii, line[i].Simple)
continue
}

if colored {
tempAscii = append(tempAscii, line[i].Colored)
tempAscii = append(tempAscii, line[i].OriginalColor)
} else if fontColor != [3]int{255, 255, 255} {
tempAscii = append(tempAscii, line[i].SetColor)
} else {
tempAscii = append(tempAscii, line[i].Simple)
}
Expand Down
Loading

0 comments on commit 1c8b009

Please sign in to comment.