Home  >  Article  >  Backend Development  >  Golang image manipulation: how to mirror, rotate and flip images

Golang image manipulation: how to mirror, rotate and flip images

WBOY
WBOYOriginal
2023-08-25 22:31:441146browse

Golang image manipulation: how to mirror, rotate and flip images

Golang image manipulation: How to mirror, rotate and flip images

1. Introduction
Image processing is something we often encounter in many development scenarios One of the requirements. In Golang, we can use the image package to operate and process images. This article will focus on how to use Golang to mirror, rotate and flip images, and provide corresponding code examples.

2. Mirror operation
Mirroring a picture is to change the left and right layout of the picture. In Golang, you can use the Flip function of the draw package to implement mirroring operations.

The following is a sample code to mirror and save images.

package main

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

func main() {
    // 打开图片文件
    file, err := os.Open("input.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        panic(err)
    }

    // 创建一个新的画布
    flipImg := image.NewRGBA(img.Bounds())

    // 镜像操作
    draw.Draw(flipImg, flipImg.Bounds(), img, img.Bounds().Min, draw.FlipHorizontal)

    // 创建输出文件
    output, err := os.Create("output_flip.jpg")
    if err != nil {
        panic(err)
    }
    defer output.Close()

    // 保存为JPEG格式图片
    jpeg.Encode(output, flipImg, nil)
}

3. Rotation operation
Rotating the picture is to change the direction and angle of the picture. In Golang, you can use the Rotate function of the draw package to implement rotation operations.

The following is a sample code to rotate the image 90 degrees clockwise and save it.

package main

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

func main() {
    // 打开图片文件
    file, err := os.Open("input.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        panic(err)
    }

    // 创建一个新的画布,大小为旋转后的尺寸
    rotateImg := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dy(), img.Bounds().Dx()))

    // 旋转操作
    draw.Draw(rotateImg, rotateImg.Bounds(), img, img.Bounds().Min, draw.Rotate90)

    // 创建输出文件
    output, err := os.Create("output_rotate.jpg")
    if err != nil {
        panic(err)
    }
    defer output.Close()

    // 保存为JPEG格式图片
    jpeg.Encode(output, rotateImg, nil)
}

4. Flip operation
Flip operation on a picture is to change the top and bottom layout of the picture. In Golang, you can use the Flip function of the draw package to implement the flip operation.

The following is a sample code to flip the image and save it.

package main

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

func main() {
    // 打开图片文件
    file, err := os.Open("input.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        panic(err)
    }

    // 创建一个新的画布
    flipImg := image.NewRGBA(img.Bounds())

    // 翻转操作
    draw.Draw(flipImg, flipImg.Bounds(), img, img.Bounds().Min, draw.FlipVertical)

    // 创建输出文件
    output, err := os.Create("output_flip.jpg")
    if err != nil {
        panic(err)
    }
    defer output.Close()

    // 保存为JPEG格式图片
    jpeg.Encode(output, flipImg, nil)
}

In the above code example, we use the related functions of the image package to operate images. Among them, the image.Decode function is used to decode the image file and create an image object. Before operating, we first create a new canvas object (that is, create a new image object), and then use the functions of the draw package to perform operations such as mirroring, rotation, and flipping. Finally, use the Encode function of the jpeg package to save the processed image in JPEG format.

5. Summary
Through the introduction and code examples of this article, we have learned how to use Golang to mirror, rotate and flip images. In actual development, we can perform corresponding image processing operations according to specific needs to meet the needs of the project. At the same time, we can also combine with other image processing libraries, such as resize, crop, etc., to further complete more complex image operations.

I hope this article can be helpful to everyone in Golang image processing!

The above is the detailed content of Golang image manipulation: how to mirror, rotate and flip images. 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