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.
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 theimage.Decode
function to decode it. Then, we created agift
object, used thegift.Convolution
function to pass in the sharpening parameters, and then processed the image through theg.Draw
function. Finally, we create an output image file "output.jpg" and use thejpeg.Encode
function to encode the processed result into JPEG format and save it to the file.
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 theimage.Decode
function to decode it. Then, we created agift
object, used thegift.Blur
function to pass in the blur parameters, and then used theg.Draw
function to process the image. Finally, we create an output image file "output.jpg" and use thejpeg.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!