From bc12009269bd9cb001140d5fc903d01740b83620 Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Sat, 20 Oct 2018 19:49:57 +0800 Subject: [PATCH] add new method to convert a image from filename directly --- README.md | 21 +++------------------ convert/convert.go | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fa1adfb..6c5edb6 100644 --- a/README.md +++ b/README.md @@ -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, + })) } ``` diff --git a/convert/convert.go b/convert/convert.go index 818b1d9..22c7446 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -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) +} -- 2.45.2