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.
- 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("锐化处理完成") }
In the above code, we first open an image file named "input.jpg" and use the image.Decode
function to decode it. Then, we created a gift
object, used the gift.Convolution
function to pass in the sharpening parameters, and then processed the image through the g.Draw
function. Finally, we create an output image file "output.jpg" and use the jpeg.Encode
function to encode the processed result into JPEG format and save it to the file.
- 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("模糊处理完成") }
In the above code, we are similar to the sharpening code. First, we open an input file named "input .jpg" image file and use the image.Decode
function to decode it. Then, we created a gift
object, used the gift.Blur
function to pass in the blur parameters, and then used the g.Draw
function to process the image. Finally, we create an output image file "output.jpg" and use the jpeg.Encode
function 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.
