From f3acaa07734a74d48b8e3e51863c57a7986b3eba Mon Sep 17 00:00:00 2001 From: Daniel Christian Schroeter <2836188+danielchristianschroeter@users.noreply.github.com> Date: Sun, 29 Jan 2023 22:28:14 +0100 Subject: [PATCH] add custom target watermarked image filename with suffix and customize target image hight and/or width --- README.md | 8 ++++++ main.go | 82 +++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index aa4d00e..08b8025 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 167cb71..bb55b5d 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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() { @@ -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) @@ -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 } @@ -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 @@ -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++ } }