-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b299e18
commit cc93f73
Showing
7 changed files
with
191 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
go 1.21.3 | ||
|
||
use ./tool/imggen | ||
|
||
use ./tool/imgdiff |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# imgdiff |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/lusingander/tool/imgdiff | ||
|
||
go 1.21.3 | ||
|
||
require ( | ||
github.com/google/subcommands v1.2.0 | ||
github.com/logrusorgru/aurora/v4 v4.0.0 | ||
github.com/n7olkachev/imgdiff v1.0.2 | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= | ||
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= | ||
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= | ||
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= | ||
github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA= | ||
github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ= | ||
github.com/n7olkachev/imgdiff v1.0.2 h1:qVnJMhcDvsrB7KOcLXWW1lLBkNbvRzscwjvDfFf3Ddg= | ||
github.com/n7olkachev/imgdiff v1.0.2/go.mod h1:7tMX8V2Gp4x3QXnslCYBc/7amMQz/tALbvuMiBUz4d0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"image" | ||
"image/png" | ||
"io/fs" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/google/subcommands" | ||
"github.com/logrusorgru/aurora/v4" | ||
"github.com/n7olkachev/imgdiff/pkg/imgdiff" | ||
) | ||
|
||
type testCmd struct { | ||
baseDirPath string | ||
targetDirPath string | ||
outputDirPath string | ||
} | ||
|
||
func (*testCmd) Name() string { return "test" } | ||
|
||
func (*testCmd) Synopsis() string { return "Test image diff" } | ||
|
||
func (*testCmd) Usage() string { return "test -base <dir> -target <dir> -out <dir>\n" } | ||
|
||
func (cmd *testCmd) SetFlags(f *flag.FlagSet) { | ||
f.StringVar(&cmd.baseDirPath, "base", "", "base directory path") | ||
f.StringVar(&cmd.targetDirPath, "target", "", "target directory path") | ||
f.StringVar(&cmd.outputDirPath, "out", "", "output directory path") | ||
} | ||
|
||
func (cmd *testCmd) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { | ||
if err := cmd.run(); err != nil { | ||
log.Println(err) | ||
return subcommands.ExitFailure | ||
} | ||
return subcommands.ExitSuccess | ||
} | ||
|
||
func (cmd *testCmd) run() error { | ||
baseFiles, err := os.ReadDir(cmd.baseDirPath) | ||
if err != nil { | ||
return err | ||
} | ||
targetFiles, err := os.ReadDir(cmd.targetDirPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cleanOutputDir(cmd.outputDirPath); err != nil { | ||
return err | ||
} | ||
|
||
for _, baseFile := range baseFiles { | ||
if !strings.HasSuffix(baseFile.Name(), ".png") { | ||
continue | ||
} | ||
|
||
targetFile := findByName(baseFile, targetFiles) | ||
if targetFile == nil { | ||
fmt.Printf("%s: %s\n", baseFile.Name(), aurora.Blue("Not found")) | ||
continue | ||
} | ||
|
||
baseImage, err := openImage(cmd.baseDirPath, targetFile.Name()) | ||
if err != nil { | ||
return err | ||
} | ||
targetImage, err := openImage(cmd.targetDirPath, targetFile.Name()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
opts := &imgdiff.Options{ | ||
Threshold: 0.1, | ||
DiffImage: true, | ||
} | ||
result := imgdiff.Diff(baseImage, targetImage, opts) | ||
if result.Equal { | ||
fmt.Printf("%s: %s\n", baseFile.Name(), aurora.Green("Success")) | ||
} else { | ||
fmt.Printf("%s: %s\n", baseFile.Name(), aurora.Red("Failure")) | ||
if err := createOutputDir(cmd.outputDirPath); err != nil { | ||
return err | ||
} | ||
if err := outputImage(cmd.outputDirPath, baseFile.Name(), result.Image); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func findByName(target fs.DirEntry, es []fs.DirEntry) fs.DirEntry { | ||
for _, e := range es { | ||
if target.Name() == e.Name() { | ||
return e | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func cleanOutputDir(dir string) error { | ||
return os.RemoveAll(dir) | ||
} | ||
|
||
func createOutputDir(dir string) error { | ||
return os.MkdirAll(dir, os.ModePerm) | ||
} | ||
|
||
func outputImage(dir, name string, img image.Image) error { | ||
path := filepath.Join(dir, name) | ||
dst, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
if err := png.Encode(dst, img); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func openImage(dir, name string) (image.Image, error) { | ||
path := filepath.Join(dir, name) | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
img, _, err := image.Decode(file) | ||
return img, err | ||
} | ||
|
||
func main() { | ||
subcommands.Register(subcommands.HelpCommand(), "") | ||
subcommands.Register(subcommands.CommandsCommand(), "") | ||
subcommands.Register(subcommands.FlagsCommand(), "") | ||
subcommands.Register(&testCmd{}, "") | ||
flag.Parse() | ||
ctx := context.Background() | ||
os.Exit(int(subcommands.Execute(ctx))) | ||
} |