Go: how to proportionally resize images

Go: how to proportionally resize images

Proportional resizing of images is a common task in software development, especially when working with applications that involve graphics manipulation or image management.

Proportional resizing of images is a common task in software development, especially when working with applications that involve graphics manipulation or image management. In Go, you can do this efficiently by using the image library along with draw and jpeg (or png, depending on the format of the images).

Preparing the environment

First, you need to install the github.com/nfnt/resize library to simplify the image resizing process. You can do this by running the following command:


go get -u github.com/nfnt/resize

Creating a resize function

Next, you can create a function in Go to perform proportional scaling of images. Here's an example of what a basic function might look like:


package main

import (
    "fmt"
    "image"
    "image/draw"
    _ "image/jpeg"
    "os"

    "github.com/nfnt/resize"
)

// ResizeImage proportionally with a new specified width
func ResizeImage(inputPath, outputPath string, newWidth uint) error {
    // Open the input image file
    inputFile, err := os.Open(inputPath)
    if err != nil {
        return err
    }
    defer inputFile.Close()

    // Decode the image
    img, _, err := image.Decode(inputFile)
    if err != nil {
        return err
    }

    // Calculate the new height while maintaining the aspect ratio
    newHeight := uint(float64(newWidth) / float64(img.Bounds().Dx()) * float64(img.Bounds().Dy()))

    // Resize the image
    resizedImage := resize.Resize(newWidth, newHeight, img, resize.Lanczos3)

    // Create a new file for the resized image
    outputFile, err := os.Create(outputPath)
    if err != nil {
        return err
    }
    defer outputFile.Close()
    return nil
}

func main() {
    // Example of using the function
    err := ResizeImage("input.jpg", "output.jpg", 300)
    if err != nil {
        fmt.Println("Error:", err)
    }
}

Explanation of the code

  1. Opening input file: The function opens the input image file using os.Open().

  2. Image decoding: The image is decoded using the image library.

  3. New size calculation: The new height is calculated based on the new width specified, maintaining the original aspect ratio.

  4. Resizing : The image is resized using the resize.Resize() function from the github.com/nfnt/resize library.

  5. Creating the output file: A new file is created for the resized image using os.Create().

  6. Encoding and saving: The resized image is encoded into the desired format (in this case, JPEG) and saved to the new file.

  7. Example usage: In the main() function, an example usage of the resize function is performed.

With this example, you now have a solid foundation for implementing proportional image scaling in Go. You can adapt this feature to your specific needs, such as managing multiple images or manipulating other file formats.