Home  >  Article  >  Backend Development  >  Golang image processing: learn how to perform color inversion and hue separation of images

Golang image processing: learn how to perform color inversion and hue separation of images

WBOY
WBOYOriginal
2023-08-26 13:40:541524browse

Golang image processing: learn how to perform color inversion and hue separation of images

Golang Image Processing: Learn how to perform color inversion and tone separation of images

Introduction:
With the development of digital image processing technology, how to perform image processing Performing various special effects has become an important skill. In Golang, we can use its powerful image processing library to achieve various image processing effects. This article will focus on how to use Golang to perform color inversion and tone separation of images.

  1. Picture color inversion

Color inversion is a simple and common image processing effect. With color inversion, we can swap the normal color of a picture with its opposite, creating a completely different effect.

Code example:

package main

import (
    "fmt"
    "image"
    "image/color"
    "image/jpeg"
    "log"
    "os"
)

func main() {
    // 读取示例图片
    file, err := os.Open("example.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    // 解码图片
    img, err := jpeg.Decode(file)
    if err != nil {
        log.Fatal(err)
    }

    // 获取图片宽高
    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    // 创建新图片
    newImg := image.NewRGBA(bounds)

    // 图片颜色反转
    for i := 0; i < height; i++ {
        for j := 0; j < width; j++ {
            // 获取像素点颜色
            oldColor := img.At(j, i)
            r, g, b, _ := oldColor.RGBA()

            // 计算反转后的颜色
            newR, newG, newB := 255-r, 255-g, 255-b

            // 设置新像素点颜色
            newColor := color.RGBA{uint8(newR), uint8(newG), uint8(newB), 0xff}
            newImg.Set(j, i, newColor)
        }
    }

    // 创建保存结果的文件
    resultFile, err := os.Create("result.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer resultFile.Close()

    // 保存结果图片
    if err := jpeg.Encode(resultFile, newImg, &jpeg.Options{100}); err != nil {
        log.Fatal(err)
    }

    fmt.Println("图片颜色反转完成")
}
  1. Picture tone separation

Color separation is a common special effects processing method, by converting a color picture into a color image containing only An image with a small number of primary colors can create a distinctive visual effect.

Code example:

package main

import (
    "fmt"
    "image"
    "image/color"
    "image/jpeg"
    "log"
    "os"
)

func main() {
    // 读取示例图片
    file, err := os.Open("example.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    // 解码图片
    img, err := jpeg.Decode(file)
    if err != nil {
        log.Fatal(err)
    }

    // 获取图片宽高
    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    // 创建新图片
    newImg := image.NewRGBA(bounds)

    // 图片色调分离
    for i := 0; i < height; i++ {
        for j := 0; j < width; j++ {
            // 获取像素点颜色
            oldColor := img.At(j, i)
            r, g, b, _ := oldColor.RGBA()

            // 计算色调分离后的颜色
            newR := (r/65535)*65535/2 + 32767
            newG := (g/65535)*65535/2 + 32767
            newB := (b/65535)*65535/2 + 32767

            // 设置新像素点颜色
            newColor := color.RGBA{uint8(newR), uint8(newG), uint8(newB), 0xff}
            newImg.Set(j, i, newColor)
        }
    }

    // 创建保存结果的文件
    resultFile, err := os.Create("result.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer resultFile.Close()

    // 保存结果图片
    if err := jpeg.Encode(resultFile, newImg, &jpeg.Options{100}); err != nil {
        log.Fatal(err)
    }

    fmt.Println("图片色调分离完成")
}

Summary:
This article introduces how to use Golang to perform color inversion and tone separation of images. Through code examples, we learned how to read and decode images, perform pixel-level operations on images, and finally generate the resulting image. I hope that by studying this article, readers can become more familiar with the relevant knowledge of Golang image processing and master the ability to implement various image special effects processing.

The above is the detailed content of Golang image processing: learn how to perform color inversion and hue separation of 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