Golang image processing: How to sharpen and blur images

WBOY
Release: 2023-08-27 13:39:20
Original
1404 people have browsed it

Golang image processing: How to sharpen and blur images

Golang image processing: How to sharpen and blur images

Introduction:
In many application scenarios, we need to perform some special effects processing on images , such as sharpening and blurring. As an efficient programming language, Golang also provides a wealth of library functions to implement image processing functions. This article will introduce how to use Golang to sharpen and blur images, and attach detailed code examples.

  1. Sharpening Picture Processing
    Sharpening is a method of enhancing the edges and details of an image, making it look clearer and more distinct. In Golang, we can use the third-party library "github.com/disintegration/gift" to achieve image sharpening.

First, we need to install the library, which can be installed using the following command:
go get github.com/disintegration/gift

Next, we can use the following Code to achieve image sharpening:

package main import ( "fmt" "image" "image/jpeg" "os" "github.com/disintegration/gift" ) func main() { // 打开图片文件 file, err := os.Open("input.jpg") if err != nil { fmt.Println("打开图片文件失败:", err) return } defer file.Close() // 解码图片 img, _, err := image.Decode(file) if err != nil { fmt.Println("解码图片失败:", err) return } // 创建礼物对象 g := gift.New( gift.Convolution([]float32{ -1, -1, -1, -1, 9, -1, -1, -1, -1, }, false, false, false, 0.0), ) // 创建输出图片 dst := image.NewRGBA(g.Bounds(img.Bounds())) // 使用礼物对象处理图片 g.Draw(dst, img) // 创建输出图片文件 outFile, err := os.Create("output.jpg") if err != nil { fmt.Println("创建输出图片文件失败:", err) return } defer outFile.Close() // 编码输出图片文件 err = jpeg.Encode(outFile, dst, nil) if err != nil { fmt.Println("编码输出图片文件失败:", err) return } fmt.Println("锐化处理完成") }
Copy after login

In the above code, we first open an image file named "input.jpg" and use theimage.Decodefunction to decode it. Then, we created agiftobject, used thegift.Convolutionfunction to pass in the sharpening parameters, and then processed the image through theg.Drawfunction. Finally, we create an output image file "output.jpg" and use thejpeg.Encodefunction to encode the processed result into JPEG format and save it to the file.

  1. Blurred Picture Processing
    Blurring is a method of reducing the details and edges of a picture, making it look softer and blurry. In Golang, we can also use the "github.com/disintegration/gift" library to achieve blurring of images.

The following is a sample code for image blur processing using Golang:

package main import ( "fmt" "image" "image/jpeg" "os" "github.com/disintegration/gift" ) func main() { // 打开图片文件 file, err := os.Open("input.jpg") if err != nil { fmt.Println("打开图片文件失败:", err) return } defer file.Close() // 解码图片 img, _, err := image.Decode(file) if err != nil { fmt.Println("解码图片失败:", err) return } // 创建礼物对象 g := gift.New( gift.Blur(5), ) // 创建输出图片 dst := image.NewRGBA(g.Bounds(img.Bounds())) // 使用礼物对象处理图片 g.Draw(dst, img) // 创建输出图片文件 outFile, err := os.Create("output.jpg") if err != nil { fmt.Println("创建输出图片文件失败:", err) return } defer outFile.Close() // 编码输出图片文件 err = jpeg.Encode(outFile, dst, nil) if err != nil { fmt.Println("编码输出图片文件失败:", err) return } fmt.Println("模糊处理完成") }
Copy after login

In the above code, we are similar to the sharpening code. First, we open an input file named "input .jpg" image file and use theimage.Decodefunction to decode it. Then, we created agiftobject, used thegift.Blurfunction to pass in the blur parameters, and then used theg.Drawfunction to process the image. Finally, we create an output image file "output.jpg" and use thejpeg.Encodefunction to encode the processed result into JPEG format and save it to the file.

Conclusion:
This article introduces how to use Golang to sharpen and blur images, and provides detailed code examples. By studying these sample codes, we can better understand Golang image processing methods and techniques, and provide guidance and help for us to process images in practical applications. I hope this article can inspire and help readers.

The above is the detailed content of Golang image processing: How to sharpen and blur images. 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
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!