Go开源项目推荐:实现 Photoshop 的功能

Go语言精选

共 5665字,需浏览 12分钟

 ·

2021-09-13 20:15

生态君今天发现一个有意思的 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.

对应的子命令功能是:

  • brightness 亮度
  • contrast 对比度
  • gamma 伽马
  • hue 色阶
  • saturation 饱和度

这些功能使用方法都可以通过 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, 800800, transform.Linear)
    rotated := transform.Rotate(resized, 45nil)

    if err := imgio.Save("output.png", rotated, imgio.PNGEncoder()); err != nil {
        fmt.Println(err)
        return
    }
}

大家如果对图片处理感兴趣,可以好好学习这个包。既是完整的可用项目,也是一个库,可以自己进行更多封装、扩展。觉得不错,可以给该项目点个 Star。

文末阅读原文可以直达项目首页。



推荐阅读


福利

我为大家整理了一份从入门到进阶的Go学习资料礼包,包含学习建议:入门看什么,进阶看什么。关注公众号 「polarisxu」,回复 ebook 获取;还可以回复「进群」,和数万 Gopher 交流学习。

浏览 47
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报