Home  >  Article  >  Backend Development  >  golang image rotation

golang image rotation

王林
王林Original
2023-05-19 09:16:36831browse

Golang is a rapidly growing programming language commonly used in large-scale web applications and cloud computing. In terms of image processing, Golang provides an easy-to-use and efficient API, which makes image rotation very simple.

Below, we will demonstrate how to use Golang to implement image rotation. Suppose we have a JPEG image named "image.jpg" that needs to be rotated 90 degrees. Next we will use Golang to complete this task.

First, we need to import the "image" and "image/jpeg" libraries that come with Golang.

import (
    "image"
    "image/jpeg"
)

Next, we need to open and decode the image file.

path := "image.jpg"
file, err := os.Open(path)
if err != nil {
    //处理错误
}
defer file.Close()

img, err := jpeg.Decode(file)
if err != nil {
    //处理错误
}

Now, we can implement the rotation function by rotating the image matrix. In Golang, this goal can be achieved through the "draw" package. We need to create a new image first, then copy the original image to this new image and rotate it.

newImg := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dy(), img.Bounds().Dx()))

//拷贝原图像到新图像
draw.Draw(newImg, newImg.Bounds(), img, img.Bounds().Min, draw.Src)

//旋转新图像
rotatedImg := imaging.Rotate(newImg, 90, color.Transparent)

Finally, we need to save the rotated image to a file. We save the rotated image as "rotatedImage.jpg".

outputFile, err := os.Create("rotatedImage.jpg")
if err != nil {
    //处理错误
}
defer outputFile.Close()

//将旋转后的图像保存到文件中
jpeg.Encode(outputFile, rotatedImg, nil)

Complete sample code:

package main

import (
    "image"
    "image/color"
    "image/draw"
    "image/jpeg"
    "os"

    "github.com/disintegration/imaging"
)

func main() {
    path := "image.jpg"
    file, err := os.Open(path)
    if err != nil {
        //处理错误
    }
    defer file.Close()

    img, err := jpeg.Decode(file)
    if err != nil {
        //处理错误
    }

    newImg := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dy(), img.Bounds().Dx()))

    //拷贝原图像到新图像
    draw.Draw(newImg, newImg.Bounds(), img, img.Bounds().Min, draw.Src)

    //旋转新图像
    rotatedImg := imaging.Rotate(newImg, 90, color.Transparent)

    outputFile, err := os.Create("rotatedImage.jpg")
    if err != nil {
        //处理错误
    }
    defer outputFile.Close()

    //将旋转后的图像保存到文件中
    jpeg.Encode(outputFile, rotatedImg, nil)
}

After running the above code, we will get a 90-degree rotated image, saved as "rotatedImage.jpg".

Summary:

In this article, we show how to use Golang to achieve image rotation. We used the "image" and "image/jpeg" libraries that come with Golang, and used the "draw" package to rotate the image. Through this simple example, we can better understand Golang's powerful capabilities in image processing.

The above is the detailed content of golang image rotation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn