Created
September 11, 2016 14:11
-
-
Save aoisensi/1a6d37873ae1f5baa55f191021bdd72e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"flag" | |
"fmt" | |
"image/jpeg" | |
"io" | |
"io/ioutil" | |
"os" | |
) | |
var loop int | |
var quality int | |
func init() { | |
flag.IntVar(&loop, "i", 3, "loop times") | |
flag.IntVar(&quality, "q", 95, "quality") | |
} | |
func main() { | |
flag.Parse() | |
if flag.NArg() != 2 { | |
fmt.Println("need input and output") | |
return | |
} | |
f, err := os.Open(flag.Arg(0)) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer f.Close() | |
opt := &jpeg.Options{ | |
Quality: quality, | |
} | |
buf := new(bytes.Buffer) | |
io.Copy(buf, f) | |
for i := 0; i < loop; i++ { | |
img, err := jpeg.Decode(buf) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
buf.Reset() | |
if err := jpeg.Encode(buf, img, opt); err != nil { | |
fmt.Println(err) | |
return | |
} | |
} | |
if err := ioutil.WriteFile(flag.Arg(1), buf.Bytes(), os.ModePerm); err != nil { | |
fmt.Println(err) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment