首页 > 文章列表 > Golang图片处理:学习如何添加水印和文字

Golang图片处理:学习如何添加水印和文字

水印 图片处理 Golang图片处理关键词:Golang 文字
482 2023-09-12

Golang图片处理:学习如何添加水印和文字

引言:
在现代数字化和社交媒体的时代,图片处理已经成为了一项重要的技能。无论是个人使用还是商务运营,添加水印和文字都是常见的需求。在本文中,我们将探讨使用Golang进行图片处理的方法,学习如何添加水印和文字。

背景:
Golang是一门开源的编程语言,以其简洁的语法、高效的性能和强大的并发能力而闻名。它已经成为许多开发者的首选语言之一。在Golang中,有一些强大的第三方库,使得图片处理变得容易而且高效。

添加水印:
添加水印是一种保护个人或商业图片版权的常见技术。下面是一个示例,展示了如何使用Golang添加水印到一张图片上:

package main

import (
    "image"
    "image/draw"
    "image/jpeg"
    "os"
)

func main() {
    // 打开原始图片
    file, err := os.Open("original.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        panic(err)
    }

    // 创建一个画布
    bounds := img.Bounds()
    canvas := image.NewRGBA(bounds)

    // 绘制原始图片到画布上
    draw.Draw(canvas, bounds, img, image.Point{}, draw.Src)

    // 添加水印
    watermark := image.NewRGBA(image.Rect(0, 0, 100, 50))
    draw.Draw(canvas, image.Rect(bounds.Dx()-100, bounds.Dy()-50, bounds.Dx(), bounds.Dy()), watermark, image.Point{}, draw.Src)

    // 保存处理后的图片
    output, err := os.Create("output.jpg")
    if err != nil {
        panic(err)
    }
    defer output.Close()

    // 编码保存到文件
    jpeg.Encode(output, canvas, nil)
}

以上代码首先打开了一张名为"original.jpg"的图片文件。然后将其解码为一个image.Image对象,接着创建了一个新的RGBA画布,并将原始图片绘制到画布上。最后,创建了一个100x50大小的水印,并将其绘制到画布的右下角。最终生成的带有水印的图片被保存为"output.jpg"。

添加文字:
添加文字是另一种常见的图片处理需求,它可以用来添加标题、描述或其他说明。下面是一个示例,展示了如何使用Golang在图片上添加文字:

package main

import (
    "image"
    "image/draw"
    "image/jpeg"
    "os"
    "github.com/golang/freetype"
    "github.com/golang/freetype/truetype"
    "golang.org/x/image/font"
)

func main() {
    // 打开原始图片
    file, err := os.Open("original.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        panic(err)
    }

    // 创建一个画布
    bounds := img.Bounds()
    canvas := image.NewRGBA(bounds)

    // 绘制原始图片到画布上
    draw.Draw(canvas, bounds, img, image.Point{}, draw.Src)

    // 添加文字
    fontBytes, err := os.ReadFile("font.ttf")
    if err != nil {
        panic(err)
    }
    font, err := freetype.ParseFont(fontBytes)
    if err != nil {
        panic(err)
    }

    context := freetype.NewContext()
    context.SetDPI(72)
    context.SetFont(font)
    context.SetFontSize(24)
    context.SetClip(bounds)
    context.SetDst(canvas)
    context.SetSrc(image.Black)

    pt := freetype.Pt(10, 10+int(context.PointToFixed(24)>>6))
    context.DrawString("Hello, Golang!", pt)

    // 保存处理后的图片
    output, err := os.Create("output.jpg")
    if err != nil {
        panic(err)
    }
    defer output.Close()

    // 编码保存到文件
    jpeg.Encode(output, canvas, nil)
}

以上代码与添加水印的示例类似,但我们使用了第三方库freetype来添加文字。首先打开了一张名为"original.jpg"的图片文件,然后解码为image.Image对象。接着创建了一个新的RGBA画布,并将原始图片绘制到画布上。然后,加载了一个TrueType字体文件并将其解析为freetype.Font对象。创建了一个freetype.Context对象,并设置了字体、字号等绘制参数。最后,调用了DrawString函数,在画布上添加了文字。最终生成的图片被保存为"output.jpg"。

结语:
Golang作为一门强大的编程语言,拥有丰富的第三方库和工具,使得图片处理变得简单而高效。本文介绍了如何使用Golang添加水印和文字到图片上,并提供了相应的代码示例。希望本文可以帮助读者学习如何使用Golang进行图片处理,并在实际应用中发挥作用。