M README.md => README.md +3 -18
@@ 16,29 16,14 @@ package main
import (
"fmt"
"github.com/qeesung/image2ascii/convert"
- "image"
_ "image/jpeg"
_ "image/png"
- "log"
- "os"
)
func main() {
- f, err := os.Open("example/images/lufei.jpg")
- if err != nil {
- log.Fatal(err)
- }
-
- img, _, err := image.Decode(f)
- if err != nil {
- log.Fatal(err)
- }
-
- f.Close()
- asciiImage := convert.Image2ASCIIString(img, &convert.Options{
- Ratio: 0.2,
- })
- fmt.Print(asciiImage)
+ fmt.Print(convert.ImageFile2ASCIIString("example/images/baozou.jpg", &convert.Options{
+ Ratio: 0.5,
+ }))
}
```
M convert/convert.go => convert/convert.go +20 -2
@@ 3,10 3,12 @@ package convert
import (
"bytes"
- "github.com/qeesung/image2asicc/ascii"
- "github.com/qeesung/image2asicc/resize"
+ "github.com/qeesung/image2ascii/ascii"
+ "github.com/qeesung/image2ascii/resize"
"image"
"image/color"
+ "log"
+ "os"
)
type Options struct {
@@ 45,3 47,19 @@ func Image2ASCIIString(image image.Image, options *Options) string {
}
return buffer.String()
}
+
+// Convert a image file to ascii string
+func ImageFile2ASCIIString(imageFilename string, option *Options) string {
+ f, err := os.Open(imageFilename)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ img, _, err := image.Decode(f)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ f.Close()
+ return Image2ASCIIString(img, option)
+}