Created
March 23, 2021 07:39
-
-
Save recoilme/5eff4c28c9b2b982c08e2621deb81d68 to your computer and use it in GitHub Desktop.
progressibejpg.go
This file contains 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 ( | |
"bufio" | |
"log" | |
"os" | |
ljpg "github.com/pixiv/go-libjpeg/jpeg" | |
) | |
func main() { | |
// Decoding JPEG into image.Image | |
io, err := os.Open("lenna.jpg") | |
if err != nil { | |
log.Fatal(err) | |
} | |
img, err := ljpg.Decode(io, &ljpg.DecoderOptions{}) | |
if err != nil { | |
log.Fatalf("Decode returns error: %v\n", err) | |
} | |
// Encode JPEG | |
f, err := os.Create("outlenna.jpg") | |
if err != nil { | |
panic(err) | |
} | |
w := bufio.NewWriter(f) | |
if err := ljpg.Encode(w, img, &ljpg.EncoderOptions{Quality: 90, ProgressiveMode: true}); err != nil { | |
log.Printf("Encode returns error: %v\n", err) | |
return | |
} | |
w.Flush() | |
f.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment