-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.go
98 lines (85 loc) · 2.27 KB
/
images.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"image"
"os"
"runtime/debug"
"sync"
"time"
"github.com/corona10/goimagehash"
"github.com/vitali-fedulov/images/v2"
_ "golang.org/x/image/webp"
)
type Image struct {
fp string // file path
Mtime time.Time // file's last modification time
ImgHash []float32 // similarity hash from fedulov's images
ImgSize image.Point // width x height
Phash uint64
HashKind goimagehash.Kind
}
func tell_webp_iccp(err error) {
if fmt.Sprint(err) == "webp: invalid format" {
fmt.Fprintf(os.Stderr, " A webp format error?\n" +
" Check if your image has an ICC profile, and if it does - this could be related to\n" +
" https://github.com/golang/go/issues/60437#issuecomment-1563939784\n\n" +
" (It'll probably work fine if you convert it to png.)\n\n")
}
}
func makeImage(fp string) (img Image, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("likely upstream bug: %s\n%s",
au.Red(r), au.Index(210, debug.Stack()))
}
}()
pic, err := images.Open(fp)
if err != nil {
return Image{}, err
}
imgHash, imgSize := images.Hash(pic)
pHash, err := goimagehash.PerceptionHash(pic)
if err != nil {
return Image{}, err
}
hash := pHash.GetHash()
kind := pHash.GetKind()
// since it will return zero value anyway, error here does not actually matter
mtime, _ := statMtime(fp)
return Image{fp, mtime, imgHash, imgSize, hash, kind}, nil
}
func imageMaker(filesIn <-chan string, imagesOut chan<- Image) {
for fp := range filesIn {
img, err := makeImage(fp)
if err == nil {
imagesOut <- img
} else {
fmt.Fprintf(os.Stderr, "> %s - %s\n", au.Index(117, fp), au.Red(err))
tell_webp_iccp(err)
}
}
}
// makeImages takes file pathes and concurrently makes Images for them. They
// can be used later to find duplicates with FindDups
func makeImages(files []string) <-chan Image {
var wg sync.WaitGroup
filesIn := make(chan string)
imagesOut := make(chan Image, len(files))
for w := 1; w <= gThreads; w++ {
wg.Add(1)
go func() {
defer wg.Done()
imageMaker(filesIn, imagesOut)
}()
}
for _, fp := range files {
filesIn <- fp
}
close(filesIn)
go func() {
wg.Wait()
// processed everything, no new images will be sent
close(imagesOut)
}()
return imagesOut
}