Last active
February 21, 2018 20:43
-
-
Save daskol/7ab3ebbe49975e36b9114a3f9dda5b76 to your computer and use it in GitHub Desktop.
JPG to PNG converter in pure Go
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 ( | |
| "flag" | |
| "io" | |
| "log" | |
| "os" | |
| jpg "image/jpeg" | |
| "image/png" | |
| ) | |
| var in = flag.String("in", "", "Input JPEG image.") | |
| var out = flag.String("out", "", "Input JPEG image.") | |
| func JPEG2PNG(fin io.Reader, fout io.Writer) error { | |
| img, err := jpg.Decode(fin) | |
| if err != nil { | |
| return err | |
| } | |
| return png.Encode(fout, img) | |
| } | |
| func main() { | |
| flag.Parse() | |
| fin, err := os.Open(*in) | |
| if err != nil { | |
| log.Fatalf("failed to open input file: %s", err) | |
| } | |
| defer fin.Close() | |
| fout, err := os.Create(*out) | |
| if err != nil { | |
| log.Fatalf("failed to open output file: %s", err) | |
| } | |
| defer fout.Close() | |
| if err := JPEG2PNG(fin, fout); err != nil { | |
| log.Fatal(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment