To add watermarks and copyright information to images in Go, you can use the github.com/disintegration/imaging
library. Here's an example of how to add a watermark and copyright text to an image:
package main
import (
"image"
"image/color"
"image/draw"
"image/font"
"image/jpeg"
"log"
"os"
"github.com/disintegration/imaging"
"golang.org/x/image/font/basicfont"
)
func main() {
// Open the original image file
file, err := os.Open("input.jpg")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Decode the image file
img, _, err := image.Decode(file)
if err != nil {
log.Fatal(err)
}
// Create a new image with the same size as the original image
watermarkedImg := imaging.New(img.Bounds().Dx(), img.Bounds().Dy(), color.Transparent)
// Copy the original image to the new image
draw.Draw(watermarkedImg, watermarkedImg.Bounds(), img, image.Point{}, draw.Src)
// Add the watermark image
watermark, err := imaging.Open("watermark.png")
if err != nil {
log.Fatal(err)
}
// Resize the watermark image to a desired size
watermark = imaging.Resize(watermark, 200, 0, imaging.Lanczos)
// Position the watermark image at the bottom right corner of the main image
pos := image.Pt(watermarkedImg.Bounds().Dx()-watermark.Bounds().Dx()-10, watermarkedImg.Bounds().Dy()-watermark.Bounds().Dy()-10)
watermarkedImg = imaging.Overlay(watermarkedImg, watermark, pos, 1)
// Add the copyright text
copyrightFontFace := basicfont.Face7x13
copyrightFontSize := 16
copyright := "Copyright © 2022 Your Name"
PutLabel(watermarkedImg, 10, 10+int(font.MeasureString(basicfont.Face7x13, "A").Ceil()), copyright, copyrightFontFace, copyrightFontSize, color.White)
// Save the watermarked image
output, err := os.Create("output.jpg")
if err != nil {
log.Fatal(err)
}
defer output.Close()
// Encode the watermarked image as JPEG
err = jpeg.Encode(output, watermarkedImg, &jpeg.Options{Quality: 100})
if err != nil {
log.Fatal(err)
}
}
func PutLabel(dst draw.Image, x, y int, label string, face font.Face, size int, color color.Color) {
d := &font.Drawer{
Dst: dst,
Src: image.NewUniform(color),
Face: face,
Dot: fixedPoint26_6(x, y),
}
d.DrawString(label)
}
func fixedPoint26_6(x, y int) fixed.Point26_6 {
return fixed.Point26_6{
X: fixed.Int26_6(x * 64),
Y: fixed.Int26_6(y * 64),
}
}
In this example, we first open the original image file using os.Open
and decode it into an image.Image
using image.Decode
. We then create a new blank image with the same size as the original image using imaging.New
and copy the original image onto the new image using draw.Draw
.
Next, we open the watermark image file using imaging.Open
and resize it to a desired size using imaging.Resize
. We then position the watermark image at the bottom right corner of the main image using imaging.Overlay
.
To add the copyright text, we use a custom PutLabel
function that draws the text on the watermarked image. This function uses the basicfont.Face7x13
font face from golang.org/x/image/font/basicfont
. You can customize the font face and size according to your preference.
Finally, we save the watermarked image as a JPEG file using jpeg.Encode
. You can change the output format or adjust the quality according to your needs.
Make sure to replace the "input.jpg"
, "watermark.png"
, and "output.jpg"
file paths with your own file paths.