github.com/go-oss/image@v0.1.1-0.20230517025328-001b78555e78/imageutil/fit_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  func TestFitCrop_HeightGreaterThanWidth(t *testing.T) {
    14  	assert := assert.New(t)
    15  
    16  	var file *os.File
    17  	{
    18  		filePath, err := filepath.Abs("../testdata/FitCrop_HeightGreaterThanWidth.jpg")
    19  		assert.NoError(err)
    20  		file, err = os.Open(filePath)
    21  		defer file.Close()
    22  		assert.NoError(err)
    23  	}
    24  
    25  	img, _, err := image.Decode(file)
    26  	assert.NoError(err)
    27  
    28  	img = FitCrop(img)
    29  
    30  	bounds := img.Bounds()
    31  	assert.Equal(int(690), bounds.Dx())
    32  	assert.Equal(int(690), bounds.Dy())
    33  
    34  	hash, err := goimagehash.AverageHash(img)
    35  	assert.NoError(err)
    36  	expected := goimagehash.NewImageHash(4111518241746910974, goimagehash.AHash)
    37  	distance, err := hash.Distance(expected)
    38  	assert.NoError(err)
    39  	assert.True(distance < 2, "filename: distance=%d", distance)
    40  }
    41  
    42  func TestFitCrop_WidthGreaterThanHeight(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	var file *os.File
    46  	{
    47  		filePath, err := filepath.Abs("../testdata/FitCrop_WidthGreaterThanHeight.jpg")
    48  		assert.NoError(err)
    49  		file, err = os.Open(filePath)
    50  		defer file.Close()
    51  		assert.NoError(err)
    52  	}
    53  
    54  	img, _, err := image.Decode(file)
    55  	assert.NoError(err)
    56  
    57  	img = FitCrop(img)
    58  
    59  	bounds := img.Bounds()
    60  	assert.Equal(int(1151), bounds.Dx())
    61  	assert.Equal(int(1151), bounds.Dy())
    62  
    63  	hash, err := goimagehash.AverageHash(img)
    64  	assert.NoError(err)
    65  	expected := goimagehash.NewImageHash(33776998211911551, goimagehash.AHash)
    66  	distance, err := hash.Distance(expected)
    67  	assert.NoError(err)
    68  	assert.True(distance < 2, "filename: distance=%d", distance)
    69  }
    70  
    71  func TestFitCrop_SameSizeWidthHeight(t *testing.T) {
    72  	assert := assert.New(t)
    73  
    74  	var file *os.File
    75  	{
    76  		filePath, err := filepath.Abs("../testdata/FitCrop_SameSizeWidthHeight.jpg")
    77  		assert.NoError(err)
    78  		file, err = os.Open(filePath)
    79  		defer file.Close()
    80  		assert.NoError(err)
    81  	}
    82  
    83  	img, _, err := image.Decode(file)
    84  	assert.NoError(err)
    85  
    86  	img = FitCrop(img)
    87  
    88  	bounds := img.Bounds()
    89  	assert.Equal(int(1150), bounds.Dx())
    90  	assert.Equal(int(1150), bounds.Dy())
    91  
    92  	hash, err := goimagehash.AverageHash(img)
    93  	assert.NoError(err)
    94  	expected := goimagehash.NewImageHash(1099511627775, goimagehash.AHash)
    95  	distance, err := hash.Distance(expected)
    96  	assert.NoError(err)
    97  	assert.True(distance < 2, "filename: distance=%d", distance)
    98  }