Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Promise-based image perceptual hash calculation for node.
npm install imghash
:information_source: You can find the command-line interface here.
const imghash = require("imghash");
const hash1 = await imghash.hash("./path/to/file");
console.log(hash1); // "f884c4d8d1193c07"
// Custom hex length and result in binary
const hash2 = await imghash.hash("./path/to/file", 4, "binary");
console.log(hash2); // "1000100010000010"
To measure similarity between images you can use Hamming distance or Levenshtein Distance.
The following example uses the latter one:
const imghash = require("imghash");
const leven = require("leven");
const hash1 = await imghash.hash("./img1");
const hash2 = await imghash.hash("./img2");
const distance = leven(hash1, hash2);
console.log(`Distance between images is: ${distance}`);
if (distance <= 12) {
console.log("Images are similar");
} else {
console.log("Images are NOT similar");
}
.hash(filepath[, bits][, format])
Returns: ES6 Promise
, resolved returns hash string in specified format and length (eg. f884c4d8d1193c07
)
Parameters:
filepath
- path to the image (supported formats are png
and jpeg
) or Buffer
bits
(optional) - hash length [default: 8
]format
(optional) - output format [default: hex
].hashRaw(data, bits)
Returns: hex hash
Parameters:
data
- image data descriptor in form { width: [width], height: [height], data: [decoded image pixels] }
bits
- hash length.hexToBinary(s)
Returns: hex string, eg. f884c4d8d1193c07
.
Parameters:
s
- binary hash string eg. 1000100010000010
.binaryToHex(s)
Returns: hex string, eg. 1000100010000010
.
Parameters:
s
- hex hash string eg. f884c4d8d1193c07
imghash
takes advantage of block mean value based hashing method:
FAQs
Image perceptual hash calculation for node
We found that imghash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.