Skip to content

Commit

Permalink
add custom target watermarked image filename with suffix and customiz…
Browse files Browse the repository at this point in the history
…e target image hight and/or width
  • Loading branch information
danielchristianschroeter committed Jan 29, 2023
1 parent ca8fca8 commit f3acaa0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ Check out examples/original_images and examples/watermarked_images directory to
Source directory of original images
-targetDirectory string
Target directory for watermarked images
-targetWaterkaredImageFilename string
Rename all target files to the spercified filename, if set targetWaterkaredImageExtension is required
-targetWaterkaredImageFilenameSuffix string
Dynamic Suffix for the filename definied in targetWaterkaredImageFilename added to every target file. Allowed values are 3DIGITSCOUNT (3 digits enumeration count) or RAND (random 6 digits number) (default "3DIGITSCOUNT")
-targetWaterkaredImageHight int
Resize target watermarked image hight, if targetWaterkaredImageWidth is empty aspect ratio will be presvered
-targetWaterkaredImageWidth int
Resize target watermarked image width, if targetWaterkaredImageHight is empty aspect ratio will be presvered
-watermarkImageFile string
Path and name of the watermark png image file
-watermarkIncreasement float
Expand Down
82 changes: 61 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package main

import (
"flag"
"fmt"
"image"
"image/color"
"image/jpeg"
_ "image/png"
"log"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/disintegration/imaging"
Expand All @@ -23,13 +26,17 @@ var version = "development"

// Global variables used for command line flags
var (
sourceDirectory string
targetDirectory string
watermarkImageFile string
watermarkIncreasement float64
watermarkOpacity float64
watermarkMarginRight int
watermarkMarginBottom int
sourceDirectory string
targetDirectory string
watermarkImageFile string
watermarkIncreasement float64
watermarkOpacity float64
watermarkMarginRight int
watermarkMarginBottom int
targetWaterkaredImageWidth int
targetWaterkaredImageHight int
targetWaterkaredImageFilename string
targetWaterkaredImageFilenameSuffix string
)

func init() {
Expand All @@ -41,9 +48,13 @@ func init() {
flag.Float64Var(&watermarkOpacity, "watermarkOpacity", 0.5, "Opacity/Transparency of the watermark image file")
flag.IntVar(&watermarkMarginRight, "watermarkMarginRight", 20, "Margin to the right edge for the watermark")
flag.IntVar(&watermarkMarginBottom, "watermarkMarginBottom", 20, "Margin to the bottom edge for the watermark")
flag.IntVar(&targetWaterkaredImageWidth, "targetWaterkaredImageWidth", 0, "Resize target watermarked image width, if targetWaterkaredImageHight is empty aspect ratio will be presvered")
flag.IntVar(&targetWaterkaredImageHight, "targetWaterkaredImageHight", 0, "Resize target watermarked image hight, if targetWaterkaredImageWidth is empty aspect ratio will be presvered")
flag.StringVar(&targetWaterkaredImageFilename, "targetWaterkaredImageFilename", "", "Rename all target files to the spercified filename, if set targetWaterkaredImageExtension is required")
flag.StringVar(&targetWaterkaredImageFilenameSuffix, "targetWaterkaredImageFilenameSuffix", "3DIGITSCOUNT", "Dynamic Suffix for the filename definied in targetWaterkaredImageFilename added to every target file. Allowed values are 3DIGITSCOUNT (3 digits enumeration count) or RAND (random 6 digits number)")
}

func processImage(file *os.File, targetDirectory string, watermarkImageFile string, watermarkIncreasement float64, watermarkMarginRight int, watermarkMarginBottom int, watermarkOpacity float64) error {
func processImage(count int, file *os.File, targetDirectory string, watermarkImageFile string, watermarkIncreasement float64, watermarkMarginRight int, watermarkMarginBottom int, watermarkOpacity float64, targetWaterkaredImageWidth int, targetWaterkaredImageHight int, targetWaterkaredImageFilename string, targetWaterkaredImageFilenameSuffix string) error {
var orientationInt uint16
// Load the watermark png image file using image.Decode
watermarkFile, err := os.Open(watermarkImageFile)
Expand Down Expand Up @@ -122,38 +133,62 @@ func processImage(file *os.File, targetDirectory string, watermarkImageFile stri
// no adjustment needed
}

// Get the width and the height of our image.
// Get the width and the height of our image
photoWidth := img.Bounds().Dx()
photoHeight := img.Bounds().Dy()

// Get the width and the height of our watermark.
// Get the width and the height of our watermark
watermarkWidth := watermark.Bounds().Dx()
watermarkHeight := watermark.Bounds().Dy()

// Increase the dimensions of the watermark by a given percentage.
// Increase the dimensions of the watermark by a given percentage
resizedWidth := int(float64(watermarkWidth) * (float64(watermarkIncreasement) / 100.0))
resizedHeight := int(float64(watermarkHeight) * (float64(watermarkIncreasement) / 100.0))
resizedWatermark := image.NewRGBA(image.Rect(0, 0, resizedWidth, resizedHeight))
draw.NearestNeighbor.Scale(resizedWatermark, resizedWatermark.Bounds(), watermark, watermark.Bounds(), draw.Over, nil)

// Figure out the dstX value.
// Figure out the dstX value
dstX := photoWidth - resizedWidth - watermarkMarginRight

// Figure out the dstY value.
// Figure out the dstY value
dstY := photoHeight - resizedHeight - watermarkMarginBottom

// Overlay the watermark onto the photo image with a specified opacity/transparency
dst := imaging.Overlay(img, resizedWatermark, image.Pt(dstX, dstY), watermarkOpacity)

// Save the new image
outFile, err := os.Create(targetDirectory + filepath.Base(file.Name()))
if err != nil {
log.Fatalf("Failed to os.Create of file %+v Error: %+v", targetDirectory+filepath.Base(file.Name()), err)
// Resize the image
if targetWaterkaredImageWidth != 0 || targetWaterkaredImageHight != 0 {
dst = imaging.Resize(img, targetWaterkaredImageWidth, targetWaterkaredImageHight, imaging.Lanczos)
}
defer outFile.Close()
jpeg.Encode(outFile, dst, nil)

log.Println("Saved watermarked image to " + targetDirectory + filepath.Base(file.Name()))
if targetWaterkaredImageFilename != "" && targetWaterkaredImageFilenameSuffix != "" {
// Save the new image with a custom filename and a dynamic suffix
var Suffix string
switch targetWaterkaredImageFilenameSuffix {
case "3DIGITSCOUNT":
Suffix = fmt.Sprintf("%03d", count)
case "RAND":
Suffix = strconv.Itoa(rand.Intn(999999))
}
outFile, err := os.Create(targetDirectory + targetWaterkaredImageFilename + Suffix + filepath.Ext(file.Name()))
if err != nil {
log.Fatalf("Failed to os.Create of file %+v Error: %+v", targetDirectory+targetWaterkaredImageFilename+Suffix+filepath.Ext(file.Name()), err)
}
defer outFile.Close()
jpeg.Encode(outFile, dst, nil)

log.Println("Saved watermarked image to " + targetDirectory + targetWaterkaredImageFilename + Suffix + filepath.Ext(file.Name()))
} else {
// Save the new image with the same filename
outFile, err := os.Create(targetDirectory + filepath.Base(file.Name()))
if err != nil {
log.Fatalf("Failed to os.Create of file %+v Error: %+v", targetDirectory+filepath.Base(file.Name()), err)
}
defer outFile.Close()
jpeg.Encode(outFile, dst, nil)

log.Println("Saved watermarked image to " + targetDirectory + filepath.Base(file.Name()))
}

return nil
}
Expand Down Expand Up @@ -194,8 +229,12 @@ func main() {
log.Fatal(err)
}

// Set a count
count := 1

// Loop through the images in the source directory
for _, file := range files {

// Skip files that start with a dot (e.g. .DS_Store file)
if strings.HasPrefix(file.Name(), ".") {
continue
Expand All @@ -221,9 +260,10 @@ func main() {
}

// Process the image
if err := processImage(f, targetDirectory, watermarkImageFile, watermarkIncreasement, watermarkMarginRight, watermarkMarginBottom, watermarkOpacity); err != nil {
if err := processImage(count, f, targetDirectory, watermarkImageFile, watermarkIncreasement, watermarkMarginRight, watermarkMarginBottom, watermarkOpacity, targetWaterkaredImageWidth, targetWaterkaredImageHight, targetWaterkaredImageFilename, targetWaterkaredImageFilenameSuffix); err != nil {
log.Printf("Failed to processImage of file %+v Error: %v", sourceDirectory+file.Name(), err)
continue
}
count++
}
}

0 comments on commit f3acaa0

Please sign in to comment.