github.com/go-oss/image@v0.1.1-0.20230517025328-001b78555e78/imageutil/exif_test.go (about)

     1  package imageutil
     2  
     3  import (
     4  	"image"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/corona10/goimagehash"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var testfiles = []string{
    14  	"../testdata/Landscape_1.jpg",
    15  	"../testdata/Landscape_2.jpg",
    16  	"../testdata/Landscape_3.jpg",
    17  	"../testdata/Landscape_4.jpg",
    18  	"../testdata/Landscape_5.jpg",
    19  	"../testdata/Landscape_6.jpg",
    20  	"../testdata/Landscape_7.jpg",
    21  	"../testdata/Landscape_8.jpg",
    22  	"../testdata/Portrait_1.jpg",
    23  	"../testdata/Portrait_2.jpg",
    24  	"../testdata/Portrait_3.jpg",
    25  	"../testdata/Portrait_4.jpg",
    26  	"../testdata/Portrait_5.jpg",
    27  	"../testdata/Portrait_6.jpg",
    28  	"../testdata/Portrait_7.jpg",
    29  	"../testdata/Portrait_8.jpg",
    30  	"../testdata/Large_Portrait_8.jpg",
    31  	"../testdata/Without_exif.jpg",
    32  }
    33  
    34  var imageHashes = []uint64{
    35  	1731642887319092348,
    36  	1731642887587527804,
    37  	1731642887587527804,
    38  	1731642887319092348,
    39  	1731642887319092348,
    40  	1731642887587527804,
    41  	1731642887587527804,
    42  	1731642887587527804,
    43  	18442266965844736254,
    44  	18442266965844728062,
    45  	18442266965844736254,
    46  	18442266965844736254,
    47  	18442266965844736254,
    48  	18442266965844728062,
    49  	18442266965844736254,
    50  	18442266965844736254,
    51  	308498827398019326,
    52  	308498827398019326,
    53  }
    54  
    55  func TestRemoveExif(t *testing.T) {
    56  	assert := assert.New(t)
    57  
    58  	for i, filename := range testfiles {
    59  		p, err := filepath.Abs(filename)
    60  		assert.NoError(err, filename)
    61  		file, err := os.Open(p)
    62  		assert.NoError(err, filename)
    63  
    64  		func(file *os.File) {
    65  			defer file.Close()
    66  			r, err := RemoveExif(file)
    67  			assert.NotNil(r, filename)
    68  			assert.NoError(err, filename)
    69  
    70  			img, _, err := image.Decode(r)
    71  			assert.NoError(err, filename)
    72  
    73  			hash, err := goimagehash.AverageHash(img)
    74  			assert.NoError(err)
    75  			expected := goimagehash.NewImageHash(imageHashes[i], goimagehash.AHash)
    76  			distance, err := hash.Distance(expected)
    77  			assert.NoError(err)
    78  			assert.True(distance < 8, "filename: distance=%d", distance)
    79  		}(file)
    80  	}
    81  }