Go開源項(xiàng)目推薦:實(shí)現(xiàn) Photoshop 的功能
生態(tài)君今天發(fā)現(xiàn)一個(gè)有意思的 Go 語言項(xiàng)目:bild,項(xiàng)目地址:https://github.com/anthonynsimon/bild,Star 數(shù) 3.3k+。使用純 Go 實(shí)現(xiàn)圖像處理,而且盡可能使用標(biāo)準(zhǔn)庫實(shí)現(xiàn)。
具體都實(shí)現(xiàn)了哪些功能呢?
因?yàn)樵擁?xiàng)目既可以當(dāng)工具使用,也可以當(dāng)庫使用。不管如何,先安裝它:
go install github.com/anthonynsimon/bild@latest
在 $GOBIN 目錄下會(huì)有 bild 可執(zhí)行程序(默認(rèn)是 ~/go/bin 目錄下)。為了方便使用,請(qǐng)將該目錄加入 $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 「圖像—>調(diào)整」下的功能
adjust:調(diào)整基本圖像功能,如亮度或?qū)Ρ榷取?/p>
$ 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.
對(duì)應(yīng)的子命令功能是:
brightness 亮度 contrast 對(duì)比度 gamma 伽馬 hue 色階 saturation 飽和度
這些功能使用方法都可以通過 bild adjust xxx --help 查看。在官方文檔也有具體的圖片示例,看看對(duì)應(yīng)的效果。
02 兩個(gè)圖片疊加
即 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.
并非簡(jiǎn)單的疊加,有一對(duì)子命令可以使用。在官方文檔也有具體的圖片示例,看看對(duì)應(yīng)的效果。
03 模糊
使用過 PS 的同學(xué),應(yīng)該挺喜歡「高斯模糊」。
$ 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 就是高斯模糊。
其他更多功能,不一一列舉。
如果當(dāng)做庫使用,如何做?
04 當(dāng)做庫使用
看官方給的一個(gè)例子:
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
}
}
大家如果對(duì)圖片處理感興趣,可以好好學(xué)習(xí)這個(gè)包。既是完整的可用項(xiàng)目,也是一個(gè)庫,可以自己進(jìn)行更多封裝、擴(kuò)展。覺得不錯(cuò),可以給該項(xiàng)目點(diǎn)個(gè) Star。
文末閱讀原文可以直達(dá)項(xiàng)目首頁。
推薦閱讀
