Created
March 3, 2023 09:01
-
-
Save goodylili/5d022af4acb04744236a2685d239ef3a to your computer and use it in GitHub Desktop.
Converting PNG files to JPG
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 ( | |
"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