Skip to content

Instantly share code, notes, and snippets.

@goodylili
Created March 3, 2023 09:01
Show Gist options
  • Save goodylili/5d022af4acb04744236a2685d239ef3a to your computer and use it in GitHub Desktop.
Save goodylili/5d022af4acb04744236a2685d239ef3a to your computer and use it in GitHub Desktop.
Converting PNG files to JPG
package main
import (
"image/jpeg"
"image/png"
"os"
"strings"
)
func main() {
// Open the PNG file
pngFile, err := os.Open("input.png")
if err != nil {
panic(err)
}
defer pngFile.Close()
// Decode the PNG image
pngImg, err := png.Decode(pngFile)
if err != nil {
panic(err)
}
// Create a new JPG file
jpgFile, err := os.Create(strings.TrimSuffix("input.png", ".png") + ".jpg")
if err != nil {
panic(err)
}
defer jpgFile.Close()
// Encode the PNG image as a JPG image
err = jpeg.Encode(jpgFile, pngImg, nil)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment