Home > Backend Development > Golang > How to pixelate and diffuse images using Golang

How to pixelate and diffuse images using Golang

王林
Release: 2023-08-21 09:25:53
Original
1626 people have browsed it

How to pixelate and diffuse images using Golang

How to use Golang to pixelate and diffuse images

Overview:
In the field of image processing, pixelation and diffusion are two commonly used techniques. Used to apply special effects to pictures. This article will introduce how to use Golang language to implement pixelation and diffusion processing of images, and provide corresponding code examples.

Pixelization processing:
Pixelization is an effect that reduces picture details and is represented by pixel blocks. It is often used in image processing to generate cartoon effects or simulate low-resolution pictures. The following is a code example using Golang to implement pixelation processing:

package main

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

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

    img, _, err := image.Decode(file)
    if err != nil {
        log.Fatal(err)
    }

    // 像素化处理
    bounds := img.Bounds()
    dx := bounds.Dx()
    dy := bounds.Dy()

    // 设置像素块大小
    blockSize := 10

    // 新建一张与原图相同大小的画布
    pixImg := image.NewRGBA(bounds)

    // 对每个像素块进行处理
    for x := 0; x < dx; x += blockSize {
        for y := 0; y < dy; y += blockSize {
            // 获取像素块的平均颜色值
            sumR, sumG, sumB := 0, 0, 0
            count := 0
            for i := x; i < x+blockSize && i < dx; i++ {
                for j := y; j < y+blockSize && j < dy; j++ {
                    r, g, b, _ := img.At(i, j).RGBA()
                    sumR += int(r >> 8)
                    sumG += int(g >> 8)
                    sumB += int(b >> 8)
                    count++
                }
            }
            avgR := uint8(sumR / count)
            avgG := uint8(sumG / count)
            avgB := uint8(sumB / count)

            // 将像素块填充为平均颜色
            for i := x; i < x+blockSize && i < dx; i++ {
                for j := y; j < y+blockSize && j < dy; j++ {
                    pixImg.Set(i, j, color.RGBA{avgR, avgG, avgB, 255})
                }
            }
        }
    }

    // 保存处理后的图片
    outFile, err := os.Create("output_pixelize.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer outFile.Close()

    jpeg.Encode(outFile, pixImg, nil)

    log.Println("Pixelization completed!")
}
Copy after login

Diffusion processing:
Diffusion is an effect that transfers pixel values ​​to surrounding pixels, which can be used to generate mosaic or blur effects in image processing . The following is a code example of using Golang to implement diffusion processing:

package main

import (
    "image"
    "image/color"
    "image/jpeg"
    "log"
    "math/rand"
    "os"
)

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

    img, _, err := image.Decode(file)
    if err != nil {
        log.Fatal(err)
    }

    // 扩散处理
    bounds := img.Bounds()
    dx := bounds.Dx()
    dy := bounds.Dy()

    // 扩散半径
    radius := 5

    // 新建一张与原图相同大小的画布
    diffuseImg := image.NewRGBA(bounds)

    // 对每个像素进行扩散处理
    for x := 0; x < dx; x++ {
        for y := 0; y < dy; y++ {
            // 获取当前像素的颜色
            r, g, b, a := img.At(x, y).RGBA()
            curColor := color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}

            // 随机选择周围像素进行扩散
            for i := -radius; i <= radius; i++ {
                for j := -radius; j <= radius; j++ {
                    // 避免处理超出图片范围的像素
                    if x+i >= 0 && x+i < dx && y+j >= 0 && y+j < dy {
                        // 获取周围像素的颜色
                        neighborColor := img.At(x+i, y+j)

                        // 将颜色传递给当前像素
                        if rand.Intn(radius*2) == 0 {
                            curColor = neighborColor
                        }
                    }
                }
            }

            // 将扩散后的像素填充到画布上
            diffuseImg.Set(x, y, curColor)
        }
    }

    // 保存处理后的图片
    outFile, err := os.Create("output_diffuse.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer outFile.Close()

    jpeg.Encode(outFile, diffuseImg, nil)

    log.Println("Diffusion completed!")
}
Copy after login

Summary:
This article introduces how to use Golang to pixelate and diffuse images, and provides corresponding code examples. By learning and mastering these two image processing techniques, we can implement various interesting special effects in the program and add more artistic effects to the pictures. I hope this article can be helpful to readers in their learning and practice of image processing.

The above is the detailed content of How to pixelate and diffuse images using Golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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