Skip to content

Commit

Permalink
Add scaling for watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
icheered committed Sep 13, 2022
1 parent 35ab4c6 commit 2d20a41
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 8 deletions.
24 changes: 16 additions & 8 deletions WaterMarker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"strings"
"sync"
"time"

"github.com/nfnt/resize"
)

func main() {
Expand All @@ -28,11 +30,12 @@ func main() {
fmt.Println("For help: run the program from command line with the -h flag")
fmt.Println("Having issues? Please let me know at [email protected]")
fmt.Println("")
watermarkOpacity, watermarkLocation, watermarkFile, sourceDir, targetDir, force := getParameters()
watermarkOpacity, watermarkLocation, watermarkScale, watermarkFile, sourceDir, targetDir, force := getParameters()

fmt.Println("Using following parameters:")
fmt.Printf("- Opacity: %d\n", watermarkOpacity)
fmt.Printf("- Location: %s\n", watermarkLocation)
fmt.Printf("- Scale: %s\n", watermarkScale)
fmt.Printf("- Watermark: %s\n", watermarkFile)
fmt.Printf("- Source directory: %s\n", sourceDir)
fmt.Printf("- Target directory: %s\n", targetDir)
Expand Down Expand Up @@ -80,7 +83,7 @@ func main() {
wg.Add(len(files))
start := time.Now()
for _, file := range files {
go func(file os.FileInfo, watermark image.Image, mask image.Image, watermarkLocation string, sourceDir string, targetDir string) {
go func(file os.FileInfo, watermark image.Image, mask image.Image, watermarkLocation string, watermarkScale float64, sourceDir string, targetDir string) {
defer wg.Done()
if !(strings.HasSuffix(file.Name(), ".jpg")) && !(strings.HasSuffix(file.Name(), ".jpeg")) {
fmt.Printf("Skipping photo '%s' because it is not a .jpg or .jpeg\n", file.Name())
Expand All @@ -90,7 +93,10 @@ func main() {
srcImage := openImage(path.Join(sourceDir, file.Name()), "jpeg")

imgSize := srcImage.Bounds()
wmSize := watermark.Bounds()

scaledWatermark := resize.Resize(0, uint(watermarkScale*float64(imgSize.Dy())), watermark, resize.NearestNeighbor)

wmSize := scaledWatermark.Bounds()
canvas := image.NewRGBA(imgSize)
var watermarkOffset image.Point
if watermarkLocation == "left" {
Expand All @@ -100,10 +106,10 @@ func main() {
}

draw.Draw(canvas, imgSize, srcImage, image.Point{0, 0}, draw.Src)
draw.DrawMask(canvas, imgSize.Add(watermarkOffset), watermark, image.Point{0, 0}, mask, image.Point{0, 0}, draw.Over)
draw.DrawMask(canvas, imgSize.Add(watermarkOffset), scaledWatermark, image.Point{0, 0}, mask, image.Point{0, 0}, draw.Over)

saveImage(canvas, targetDir, file.Name())
}(file, watermark, mask, watermarkLocation, sourceDir, targetDir)
}(file, watermark, mask, watermarkLocation, watermarkScale, sourceDir, targetDir)
}
wg.Wait()
elapsed := time.Since(start)
Expand All @@ -123,9 +129,10 @@ func getFiles(dir string) []os.FileInfo {
return files
}

func getParameters() (int, string, string, string, string, bool) {
paramOpacity := flag.Int("opacity", 60, "Watermark opacity between 0 and 100")
func getParameters() (int, string, float64, string, string, string, bool) {
paramOpacity := flag.Int("opacity", 70, "Watermark opacity between 0 and 100")
paramLocation := flag.String("location", "right", "Location of watermark [left, right]")
paramScale := flag.Float64("scale", 0.2, "Specify the size of the watermark as a portion of the image (between 0 and 1)")
paramWatermark := flag.String("watermark", "watermark.png", "Name of PNG image to be used as watermark")
paramSourceDir := flag.String("source", "photos", "Source directory (location to find un-watermarked photos)")
paramTargetDir := flag.String("target", "watermarked", "Target directory (location to put watermarked photos")
Expand All @@ -134,12 +141,13 @@ func getParameters() (int, string, string, string, string, bool) {
flag.Parse()
opacity := *paramOpacity
location := *paramLocation
scale := *paramScale
watermark := *paramWatermark
sourceDir := *paramSourceDir
targetDir := *paramTargetDir
force := *paramForce

return opacity, location, watermark, sourceDir, targetDir, force
return opacity, location, scale, watermark, sourceDir, targetDir, force
}

func saveImage(img image.Image, pname, fname string) error {
Expand Down
Binary file modified bin/WaterMarker_Linux
Binary file not shown.
Binary file modified bin/WaterMarker_MacOS
Binary file not shown.
Binary file modified bin/WaterMarker_Windows.exe
Binary file not shown.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/kinsey40/pbar v0.0.0-20190815161936-21f8229eaa8a // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/kinsey40/pbar v0.0.0-20190815161936-21f8229eaa8a h1:Sn0FUsz+HHYf2ZwvGTmak6WtuG6FFoUpCYZ/bMC4W4I=
github.com/kinsey40/pbar v0.0.0-20190815161936-21f8229eaa8a/go.mod h1:QwUrOchRIYL3j2lQBls246ox3I5JdnVlbsdVqrvg6kI=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
Expand Down
Binary file modified watermark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2d20a41

Please sign in to comment.