Created
November 28, 2024 20:34
-
-
Save Luna-devv/554d483aca59a4eed354d2c946029f38 to your computer and use it in GitHub Desktop.
Add a watermark on images using golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This code was written using go 1.23 | |
* | |
* There are some changes you have to do, like chaging paths and file type extensions. | |
* | |
* go mod init | |
* go get github.com/fogleman/gg | |
*/ | |
package main | |
import ( | |
"fmt" | |
"image" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"github.com/fogleman/gg" | |
) | |
func addWatermark(inputPath, outputPath, watermarkText string) error { | |
imgFile, err := os.Open(inputPath) | |
if err != nil { | |
return fmt.Errorf("failed to open input image: %w", err) | |
} | |
defer imgFile.Close() | |
img, _, err := image.Decode(imgFile) | |
if err != nil { | |
return fmt.Errorf("failed to decode input image: %w", err) | |
} | |
imgHeight := float64(img.Bounds().Dy()) | |
dc := gg.NewContextForImage(img) | |
// change font path | |
if err := dc.LoadFontFace("/home/luna/.local/share/fonts/ComicSansMS3.ttf", 60); err != nil { | |
return fmt.Errorf("failed to load font: %w", err) | |
} | |
dc.SetRGB(1, 1, 1) | |
dc.SetHexColor("#FFFFFF99") | |
padding := 22 | |
_, textHeight := dc.MeasureString(watermarkText) | |
dc.DrawString(watermarkText, float64(padding), imgHeight-textHeight+float64(padding/2)) | |
if err := dc.SavePNG(outputPath); err != nil { | |
return fmt.Errorf("failed to save output image: %w", err) | |
} | |
return nil | |
} | |
func addWatermarkForAllFiles(watermarkText string) error { | |
outputDir := "./output" | |
if _, err := os.Stat(outputDir); os.IsNotExist(err) { | |
if err := os.Mkdir(outputDir, 0755); err != nil { | |
return fmt.Errorf("failed to create output directory: %w", err) | |
} | |
} | |
files, err := ioutil.ReadDir(".") | |
if err != nil { | |
return fmt.Errorf("failed to read current directory: %w", err) | |
} | |
for _, file := range files { | |
// change file type if needed | |
if strings.HasSuffix(file.Name(), ".png") { | |
inputPath := file.Name() | |
outputPath := filepath.Join(outputDir, file.Name()) | |
fmt.Printf("Processing: %s -> %s\n", inputPath, outputPath) | |
if err := addWatermark(inputPath, outputPath, watermarkText); err != nil { | |
return fmt.Errorf("failed to add watermark to %s: %w", inputPath, err) | |
} | |
} | |
} | |
fmt.Println("Processing completed for all PNG files.") | |
return nil | |
} | |
func main() { | |
// the watermark to add | |
watermarkText := "@mwlica, lunish.nl" | |
if err := addWatermarkForAllFiles(watermarkText); err != nil { | |
log.Fatalf("Error processing files: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment