def fromFile(self, filepath, hashingMetadata=None): t1 = time.time() img = None try: img = Image.open(filepath) # resizing the image proportionally to max 512px width and max 512px height img.thumbnail((512, 512)) except IOError as e: raise e t2 = time.time() readSeconds = t2 - t1 numCols, numRows = img.size buffer1 = MatrixUtil.allocateMatrixAsRowMajorArray(numRows, numCols) buffer2 = MatrixUtil.allocateMatrixAsRowMajorArray(numRows, numCols) buffer64x64 = MatrixUtil.allocateMatrix(64, 64) buffer16x64 = MatrixUtil.allocateMatrix(16, 64) buffer16x16 = MatrixUtil.allocateMatrix(16, 16) t1 = time.time() rv = self.fromImage(img, buffer1, buffer2, buffer64x64, buffer16x64, buffer16x16) t2 = time.time() if hashingMetadata is not None: hashingMetadata.imageHeightTimesWidth = numRows * numCols hashingMetadata.readSeconds = readSeconds hashingMetadata.hashSeconds = t2 - t1 return rv
def test_torben(self) -> None: numRows = 4 numCols = 8 matrix = MatrixUtil.allocateMatrix(numRows, numCols) for i in range(numRows): for j in range(numCols): matrix[i][j] = i + float((j * 0.01)) self.assertEqual(MatrixUtil.torben(matrix, numRows, numCols), 1.07)
def fromBufferedImage(self, img_bytes): try: img = Image.open(img_bytes) # resizing the image proportionally to max 512px width and max 512px height img.thumbnail((512, 512)) except IOError as e: raise e numCols, numRows = img.size buffer1 = MatrixUtil.allocateMatrixAsRowMajorArray(numRows, numCols) buffer2 = MatrixUtil.allocateMatrixAsRowMajorArray(numRows, numCols) buffer64x64 = MatrixUtil.allocateMatrix(64, 64) buffer16x64 = MatrixUtil.allocateMatrix(16, 64) buffer16x16 = MatrixUtil.allocateMatrix(16, 16) return self.fromImage(img, buffer1, buffer2, buffer64x64, buffer16x64, buffer16x16)
def pdqBuffer16x16ToBits(self, dctOutput16x16): """ Each bit of the 16x16 output hash is for whether the given frequency component is greater than the median frequency component or not. """ hash = Hash256() dctMedian = MatrixUtil.torben(dctOutput16x16, 16, 16) for i in range(16): for j in range(16): if dctOutput16x16[i][j] > dctMedian: hash.setBit(i * 16 + j) return hash
def dihedralFromFile(self, filename, hashingMetadata, dihFlags): t1 = time.time() img = None try: img = Image.open(filename) except IOError as e: raise e t2 = time.time() hashingMetadata.readSeconds = t2 - t1 numCols, numRows = img.size hashingMetadata.imageHeightTimesWidth = numRows * numCols buffer1 = MatrixUtil.allocateMatrixAsRowMajorArray(numRows, numCols) buffer2 = MatrixUtil.allocateMatrixAsRowMajorArray(numRows, numCols) buffer64x64 = MatrixUtil.allocateMatrix(64, 64) buffer16x64 = MatrixUtil.allocateMatrix(16, 64) buffer16x16 = MatrixUtil.allocateMatrix(16, 16) buffer16x16Aux = MatrixUtil.allocateMatrix(16, 16) t1 = time.time() rv = self.dihedralFromBufferedImage( img, buffer1, buffer2, buffer64x64, buffer16x64, buffer16x16, buffer16x16Aux, dihFlags, ) t2 = time.time() hashingMetadata.hashSeconds = t2 - t1 return rv