Skip to content

Commit

Permalink
rename watermarkIncreasement to watermarkScaleFactor, throw a notice …
Browse files Browse the repository at this point in the history
…instead of an error if Orientation exif tag not found, keep quailty of watermark
  • Loading branch information
danielchristianschroeter committed Sep 17, 2023
1 parent 6af7d05 commit 3a2d1ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The application supports various command-line parameters for customization. Here
Resize the target watermarked image to the specified width (in pixels). Aspect ratio will be preserved if 'targetWatermarkedImageHeight' is empty.
-watermarkImageFile string
Specify the path and name of the watermark PNG image file (required).
-watermarkIncreasement float
-watermarkScaleFactor float
Set the scale factor for the watermark image (in percentage). (default 100)
-watermarkMarginBottom int
Set the margin from the bottom edge for the watermark (in pixels). (default 20)
Expand Down
34 changes: 18 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
exif "github.com/dsoprea/go-exif/v3"
exifcommon "github.com/dsoprea/go-exif/v3/common"
"github.com/gabriel-vasile/mimetype"
"golang.org/x/image/draw"
)

// Used for build version information
Expand All @@ -30,7 +29,7 @@ var (
sourceDirectory string
targetDirectory string
watermarkImageFile string
watermarkIncreasement float64
watermarkScaleFactor float64
watermarkOpacity float64
watermarkMarginRight int
watermarkMarginBottom int
Expand All @@ -46,7 +45,7 @@ func init() {
flag.StringVar(&sourceDirectory, "sourceDirectory", "", "Set the source directory for original images (required).")
flag.StringVar(&targetDirectory, "targetDirectory", "", "Set the target directory for watermarked images (required).")
flag.StringVar(&watermarkImageFile, "watermarkImageFile", "", "Specify the path and name of the watermark PNG image file (required).")
flag.Float64Var(&watermarkIncreasement, "watermarkIncreasement", 100.0, "Set the scale factor for the watermark image (in percentage).")
flag.Float64Var(&watermarkScaleFactor, "watermarkScaleFactor", 100.0, "Set the scale factor for the watermark image (in percentage).")
flag.Float64Var(&watermarkOpacity, "watermarkOpacity", 0.5, "Set the opacity/transparency of the watermark image (0.0 to 1.0).")
flag.IntVar(&watermarkMarginRight, "watermarkMarginRight", 20, "Set the margin from the right edge for the watermark (in pixels).")
flag.IntVar(&watermarkMarginBottom, "watermarkMarginBottom", 20, "Set the margin from the bottom edge for the watermark (in pixels).")
Expand Down Expand Up @@ -133,15 +132,15 @@ func main() {
}

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

func processImage(count int, file *os.File, targetDirectory string, watermarkImageFile string, watermarkIncreasement float64, watermarkMarginRight int, watermarkMarginBottom int, watermarkOpacity float64, targetWatermarkedImageMaxDimension int, targetWatermarkedImageWidth int, targetWatermarkedImageHeight int, targetWatermarkedImageFilename string, targetWatermarkedImageFilenameSuffix string) error {
func processImage(count int, file *os.File, targetDirectory string, watermarkImageFile string, watermarkScaleFactor float64, watermarkMarginRight int, watermarkMarginBottom int, watermarkOpacity float64, targetWatermarkedImageMaxDimension int, targetWatermarkedImageWidth int, targetWatermarkedImageHeight int, targetWatermarkedImageFilename string, targetWatermarkedImageFilenameSuffix string) error {
var orientationInt uint16
// Load the watermark png image file using image.Decode
watermarkFile, err := os.Open(watermarkImageFile)
Expand Down Expand Up @@ -172,14 +171,16 @@ func processImage(count int, file *os.File, targetDirectory string, watermarkIma
}
results, err := index.RootIfd.FindTagWithName("Orientation")
if err != nil {
log.Fatalf("Failed to index.RootIfd.FindTagWithName in file %+v Error: %+v", file.Name(), err)
log.Printf("Notice: Orientation tag not found in file %+v", file.Name())
// Handle this case as needed, or continue with your code
} else {
orientation, err := results[0].Value()
if err != nil {
log.Fatalf("Failed to extract value of results from exif data in file %+v Error: %+v", file.Name(), err)
}
orientationSlice := orientation.([]uint16)
orientationInt = orientationSlice[0]
}
orientation, err := results[0].Value()
if err != nil {
log.Fatalf("Failed to extract value of results from exif data in file %+v Error: %+v", file.Name(), err)
}
orientationSlice := orientation.([]uint16)
orientationInt = orientationSlice[0]
} else {
log.Printf("Notice: No exif data found for file %+v", file.Name())
}
Expand Down Expand Up @@ -225,10 +226,11 @@ func processImage(count int, file *os.File, targetDirectory string, watermarkIma
watermarkHeight := watermark.Bounds().Dy()

// 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)
resizedWidth := int(float64(watermarkWidth) * (float64(watermarkScaleFactor) / 100.0))
resizedHeight := int(float64(watermarkHeight) * (float64(watermarkScaleFactor) / 100.0))

// Resize the watermark using Lanczos interpolation
resizedWatermark := imaging.Resize(watermark, resizedWidth, resizedHeight, imaging.Lanczos)

// Resize the watermarked image if specified
if targetWatermarkedImageMaxDimension > 0 {
Expand Down

0 comments on commit 3a2d1ab

Please sign in to comment.