golang image rotation

王林
Release: 2023-05-19 09:16:36
Original
780 people have browsed it

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"
)
Copy after login

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 {
    //处理错误
}
Copy after login

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)
Copy after login

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)
Copy after login

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)
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!