Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ 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-html

Similar to --save-img but it creates a HTML file with the name `<image-name>-ascii-art.html` in the directory path passed to the flag. Can save colored text, that can be copied from open html file.

Example for current directory:

```
ascii-image-converter [image paths/urls] --save-html .
```

#### --save-bg

> **Note:** This flag will be ignored if `--save-img` or `--save-gif` flags are not set
Expand Down Expand Up @@ -550,4 +560,4 @@ You can fork the project and implement any changes you want for a pull request.

## License

[Apache-2.0](https://github.com/TheZoraiz/ascii-image-converter/blob/master/LICENSE.txt)
[Apache-2.0](https://github.com/TheZoraiz/ascii-image-converter/blob/master/LICENSE.txt)
16 changes: 16 additions & 0 deletions aic_package/convert_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes, pipe
}
}

// Save ascii art as .html file before printing it, if --save-html flag is passed
if saveHtmlPath != "" {
if err := createHtmlToSave(
asciiSet,
colored,
saveHtmlPath,
imagePath,
urlImgName,
saveBgColor,
onlySave,
); err != nil {

return "", fmt.Errorf("can't save file: %v", err)
}
}

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

Expand Down
2 changes: 2 additions & 0 deletions aic_package/convert_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func DefaultFlags() Flags {
SaveTxtPath: "",
SaveImagePath: "",
SaveGifPath: "",
SaveHtmlPath: "",
Negative: false,
Colored: false,
CharBackgroundColor: false,
Expand Down Expand Up @@ -90,6 +91,7 @@ func Convert(filePath string, flags Flags) (string, error) {
saveTxtPath = flags.SaveTxtPath
saveImagePath = flags.SaveImagePath
saveGifPath = flags.SaveGifPath
saveHtmlPath = flags.SaveHtmlPath
negative = flags.Negative
colored = flags.Colored
colorBg = flags.CharBackgroundColor
Expand Down
102 changes: 102 additions & 0 deletions aic_package/create_ascii_html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright © 2021 Zoraiz Hassan <hzoraiz8@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package aic_package

import (
"fmt"
"os"
"strings"

imgManip "github.com/TheZoraiz/ascii-image-converter/image_manipulation"
)

/*
Creates and saves an HTML file containing the passed ascii art. The HTML file will contain the ascii art as spans with inline color style if [colored] is true
*/
func createHtmlToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveHtmlPath, imagePath, urlImgName string, saveBgColor [4]int, onlySave bool) error {
var builder strings.Builder

backgroundColor := fmt.Sprintf("#%02x%02x%02x", saveBgColor[0], saveBgColor[1], saveBgColor[2])

// Start the HTML file with the necessary headers and styles
builder.WriteString(fmt.Sprintf(`<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ASCII Art</title>
<style>
body {
background-color: %s;
}
span {
display: inline-block;
white-space: pre;
font-family: monospace;
}
</style>
</head><body><pre>`, backgroundColor))

for _, line := range asciiArt {
for _, asciiChar := range line {
// Extract the RGB values from the RgbValue field
rgb := asciiChar.RgbValue
// Convert the character and color into an HTML span
if colored {
builder.WriteString(fmt.Sprintf(
"<span style=\"color:rgb(%d,%d,%d)\">%s</span>",
rgb[0], rgb[1], rgb[2], asciiChar.Simple,
))
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for and apply fontColor flag/global variable as well. You can check it's existing implementation at aic_package/util.go line 105

builder.WriteString(fmt.Sprintf(
"<span>%s</span>",
asciiChar.Simple,
))
}
}
// Add a line break after each line
builder.WriteString("<br>")
}

// End the HTML document
builder.WriteString(`</pre></body></html>`)


saveFileName, err := createSaveFileName(imagePath, urlImgName, "-ascii-art.html")
if err != nil {
return err
}

savePathLastChar := string(saveHtmlPath[len(saveHtmlPath)-1])

// Check if path is closed with appropriate path separator (depending on OS)
if savePathLastChar != string(os.PathSeparator) {
saveHtmlPath += string(os.PathSeparator)
}

// If path exists
if _, err := os.Stat(saveHtmlPath); !os.IsNotExist(err) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reuse getFullSavePath() function to check and retrieve save path

err := os.WriteFile(saveHtmlPath+saveFileName, []byte(builder.String()), 0666)
if err != nil {
return err
} else if onlySave {
fmt.Println("Saved " + saveHtmlPath + saveFileName)
}
return nil
} else {
return fmt.Errorf("save path %v does not exist", saveHtmlPath)
}
}
4 changes: 4 additions & 0 deletions aic_package/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Flags struct {
// Path to save ascii art .png file
SaveImagePath string

// Path to save ascii art .html file
SaveHtmlPath string

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

Expand Down Expand Up @@ -111,6 +114,7 @@ var (
complex bool
saveTxtPath string
saveImagePath string
saveHtmlPath string
saveGifPath string
grayscale bool
negative bool
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
height int
saveTxtPath string
saveImagePath string
saveHtmlPath string
saveGifPath string
negative bool
formatsTrue bool
Expand Down Expand Up @@ -76,6 +77,7 @@ var (
SaveTxtPath: saveTxtPath,
SaveImagePath: saveImagePath,
SaveGifPath: saveGifPath,
SaveHtmlPath: saveHtmlPath,
Negative: negative,
Colored: colored,
CharBackgroundColor: colorBg,
Expand Down Expand Up @@ -161,7 +163,8 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&saveImagePath, "save-img", "s", "", "Save ascii art as a .png file\nFormat: <image-name>-ascii-art.png\nImage will be saved in passed path\n(pass . for current directory)\n")
rootCmd.PersistentFlags().StringVar(&saveTxtPath, "save-txt", "", "Save ascii art as a .txt file\nFormat: <image-name>-ascii-art.txt\nFile will be saved in passed path\n(pass . for current directory)\n")
rootCmd.PersistentFlags().StringVar(&saveGifPath, "save-gif", "", "If input is a gif, save it as a .gif file\nFormat: <gif-name>-ascii-art.gif\nGif will be saved in passed path\n(pass . for current directory)\n")
rootCmd.PersistentFlags().IntSliceVar(&saveBgColor, "save-bg", nil, "Set background color for --save-img\nand --save-gif flags\nPass an RGBA value\ne.g. --save-bg 255,255,255,100\n(Defaults to 0,0,0,100)\n")
rootCmd.PersistentFlags().StringVar(&saveHtmlPath, "save-html", "", "Save ascii art as a .html file\nFormat: <image-name>-ascii-art.html\nFile will be saved in passed path\n(pass . for current directory)\n")
rootCmd.PersistentFlags().IntSliceVar(&saveBgColor, "save-bg", nil, "Set background color for --save-img\nand --save-gif \nand --save-html flags\nPass an RGBA value\ne.g. --save-bg 255,255,255,100\n(Defaults to 0,0,0,100)\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")
Expand Down
4 changes: 2 additions & 2 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func checkInputAndFlags(args []string) bool {
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")
if (saveTxtPath == "" && saveImagePath == "" && saveGifPath == "" && saveHtmlPath == "") && onlySave {
fmt.Printf("Error: you need to supply one of --save-img, --save-txt, --save-gif or --save-html for using --only-save\n\n")
return true
}

Expand Down