Home > Article > Backend Development > Golang image processing: how to perform color gradient and grayscale mapping of images
Golang image processing: How to perform color gradient and grayscale mapping of images
Introduction: With the development of digital media, image processing has become an indispensable part of our daily life. A missing part. In the Go language, we can use some libraries for image processing, such as github.com/disintegration/imaging. This article will introduce how to use this library to perform color gradient and grayscale mapping of images.
1. Import the library
First, we need to introduce the github.com/disintegration/imaging library into the Go project. You can use the following command to install:
go get -u github.com/disintegration/imaging
2. Color gradient
If we want to create a color in a picture The gradient effect can be achieved through the following steps:
1. Read the picture
Using the Open function of the imaging library, we can read a picture. The following code demonstrates how to read an image:
import ( "github.com/disintegration/imaging" ) func main() { srcImg, err := imaging.Open("input.jpg") if err != nil { panic(err) } }
2. Create a gradient image
Next, we can use the New function of the imaging library to create a new image as a gradient The target image for the effect. The following code demonstrates how to create a target image and specify its width and height:
dstImg := imaging.New(800, 600, color.NRGBA{R: 0, G: 0, B: 0, A: 0})
In this example, we create an 800x600 size image whose color is black (the RGB values are all 0).
3. Gradient effect
Continuing to use the functions of the imaging library, we can map the pixel colors of the source image to the target image in order to achieve the gradient effect. The following code demonstrates how to achieve the effect of linear gradient:
for y := 0; y < srcImg.Bounds().Dy(); y++ { for x := 0; x < srcImg.Bounds().Dx(); x++ { c := srcImg.At(x, y) r, g, b, a := c.RGBA() // 根据像素位置和颜色进行渐变计算 c = color.RGBA{ R: uint8(x * 255 / srcImg.Bounds().Dx()), G: uint8(y * 255 / srcImg.Bounds().Dy()), B: uint8(b / 65535 * 255), A: uint8(a / 65535 * 255), } dstImg.Set(x, y, c) } }
4. Save the image
Finally, we can use the Save function of the imaging library to save the target image to a file:
err = imaging.Save(dstImg, "output.jpg") if err != nil { panic(err) }
In this way, we have completed the color gradient processing of a picture.
3. Grayscale mapping
In addition to color gradients, we can also convert a picture to grayscale. The following code demonstrates how to implement grayscale mapping of images:
func grayscaleMapping() { srcImg, err := imaging.Open("input.jpg") if err != nil { panic(err) } // 新建一张灰度图像 dstImg := imaging.New(srcImg.Bounds().Dx(), srcImg.Bounds().Dy(), color.NRGBA{R: 0, G: 0, B: 0, A: 0}) for y := 0; y < srcImg.Bounds().Dy(); y++ { for x := 0; x < srcImg.Bounds().Dx(); x++ { c := srcImg.At(x, y) r, g, b, _ := c.RGBA() intensity := (r + g + b) / 3 // 根据像素灰度值映射为新的颜色 c = color.RGBA{R: uint8(intensity), G: uint8(intensity), B: uint8(intensity), A: 255} dstImg.Set(x, y, c) } } err = imaging.Save(dstImg, "output_grayscale.jpg") if err != nil { panic(err) } }
The above code averages the RGB value of each pixel of the source image, and then uses the result as the RGB value of the new pixel, thereby achieving grayscale mapping. degree mapping.
Conclusion:
By introducing the github.com/disintegration/imaging library, we can perform various image processing operations in the Go language. This article takes color gradient and grayscale mapping as examples to give specific code examples. Readers can perform more complex image processing operations on this basis based on their needs.
The above is the detailed content of Golang image processing: how to perform color gradient and grayscale mapping of images. For more information, please follow other related articles on the PHP Chinese website!