Skip to content

Instantly share code, notes, and snippets.

@phu54321
Last active October 15, 2020 02:34
Show Gist options
  • Save phu54321/fae0240580fea77759f81853d506f683 to your computer and use it in GitHub Desktop.
Save phu54321/fae0240580fea77759f81853d506f683 to your computer and use it in GitHub Desktop.
Snag duplicate remover
const fs = require('fs-extra')
const path = require('path')
const Jimp = require('jimp')
const dirnames = process.argv.slice(2)
;(async function () {
for (const dir of dirnames) {
console.log(`Processing ${dir}`)
const files = (await fs.readdir(dir)).filter(f => f.endsWith('.jpg'))
const imageHashes = await Promise.all(
files.map(filename => Jimp.read(path.join(dir, filename)).then(i => {
i = i.crop(0, 0, 1280, 954)
return i.pHash()
}))
)
const unlinkList = []
for (let i = 0; i < imageHashes.length; i++) {
for (let j = i - 1; j >= 0; j--) {
if (Jimp.compareHashes(imageHashes[i], imageHashes[j]) === 0) {
console.log(`Duplicate: ${files[i]} === ${files[j]}`)
unlinkList.push(files[i])
break
}
}
}
unlinkList.map(filename => fs.unlink(path.join(dir, filename)))
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment