生態君今天發現一個有意思的 Go 語言項目:bild,項目地址:https://github.com/anthonynsimon/bild,Star 數 3.3k+。使用純 Go 實現圖像處理,而且儘可能使用標準庫實現。
具體都實現了哪些功能呢?
因為該項目既可以當工具使用,也可以當庫使用。不管如何,先安裝它:
go install github.com/anthonynsimon/bild@latest在 $GOBIN 目錄下會有 bild 可執行程序(默認是 ~/go/bin 目錄下)。為了方便使用,請將該目錄加入 $PATH 中。
$ bild
A collection of parallel image processing algorithms in pure Go
Usage:
bild [command]
Available Commands:
adjust adjust basic image features like brightness or contrast
blend blend two images together
blur blur an image using the specified method
channel channel operations on images
effect apply effects on images
help Help about any command
histogram histogram operations on images
imgio i/o operations on images
noise noise generators
segment segment an image using the specified method
Flags:
-h, --help help for bild
Use "bild [command] --help" for more information about a command.
01 類似 Photoshop 「圖像—>調整」下的功能adjust:調整基本圖像功能,如亮度或對比度。
$ bild help adjust
adjust basic image features like brightness or contrast
Usage:
bild adjust [command]
Available Commands:
brightness adjust the relative brightness of an image
contrast adjust the relative contrast of an image
gamma adjust the gamma of an image
hue adjust the hue of an image
saturation adjust the saturation of an image
Flags:
-h, --help help for adjust
Use "bild adjust [command] --help" for more information about a command.對應的子命令功能是:
這些功能使用方法都可以通過 bild adjust xxx --help 查看。在官方文檔也有具體的圖片示例,看看對應的效果。
02 兩個圖片疊加即 blend 功能。
$ bild help blend
blend two images together
Usage:
bild blend [command]
Available Commands:
add
colorburn
colordodge
darken
difference
divide
exclusion
lighten
linearLight
linearburn
multiply
normal
opacity
overlay
screen
softlight
subtract
Flags:
-h, --help help for blend
Use "bild blend [command] --help" for more information about a command.並非簡單的疊加,有一對子命令可以使用。在官方文檔也有具體的圖片示例,看看對應的效果。
03 模糊使用過 PS 的同學,應該挺喜歡「高斯模糊」。
$ bild help blur
blur an image using the specified method
Usage:
bild blur [command]
Available Commands:
box apply box blur to an input image
gaussian apply gaussian blur to an input image
Flags:
-h, --help help for blur
Use "bild blur [command] --help" for more information about a command.其中 gaussian 就是高斯模糊。
其他更多功能,不一一列舉。
如果當做庫使用,如何做?
04 當做庫使用看官方給的一個例子:
package main
import (
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/transform"
)
func main() {
img, err := imgio.Open("input.jpg")
if err != nil {
fmt.Println(err)
return
}
inverted := effect.Invert(img)
resized := transform.Resize(inverted, 800, 800, transform.Linear)
rotated := transform.Rotate(resized, 45, nil)
if err := imgio.Save("output.png", rotated, imgio.PNGEncoder()); err != nil {
fmt.Println(err)
return
}
}大家如果對圖片處理感興趣,可以好好學習這個包。既是完整的可用項目,也是一個庫,可以自己進行更多封裝、擴展。覺得不錯,可以給該項目點個 Star。
文末閱讀原文可以直達項目首頁。
推薦閱讀
我為大家整理了一份從入門到進階的Go學習資料禮包,包含學習建議:入門看什麼,進階看什麼。關注公眾號 「polarisxu」,回復 ebook 獲取;還可以回復「進群」,和數萬 Gopher 交流學習。