How to resize and scale an image in Go?

To resize and scale an image in Go, you can use the github.com/nfnt/resize package. Here's an example of resizing and scaling an image:

package main import ( "image" "image/jpeg" "log" "os" "github.com/nfnt/resize" ) func main() { // Open the image file file, err := os.Open("input.jpg") if err != nil { log.Fatal(err) } defer file.Close() // Decode the image file img, _, err := image.Decode(file) if err != nil { log.Fatal(err) } // Specify the desired width and height of the scaled image width := 800 height := 600 // Resize the image using the resize package resizedImage := resize.Resize(uint(width), uint(height), img, resize.Lanczos3) // Create the output file output, err := os.Create("output.jpg") if err != nil { log.Fatal(err) } defer output.Close() // Encode the resized image in JPEG format and save it to the output file err = jpeg.Encode(output, resizedImage, &jpeg.Options{Quality: 90}) if err != nil { log.Fatal(err) } log.Println("Image resized and scaled successfully") }

Make sure to replace "input.jpg" with the path to your input image file and "output.jpg" with the desired path for the output image file.

In this example, we use the github.com/nfnt/resize.Lanczos3 interpolation function for high-quality scaling. Additionally, we specify the quality of the output JPEG image using the jpeg.Options{Quality: 90}. You can adjust the quality value from 1 to 100 as per your requirement.