images now converted sepia + checksum
This commit is contained in:
81
src/index.ts
81
src/index.ts
@ -1,25 +1,60 @@
|
||||
import Jimp from 'jimp';
|
||||
import fs from 'fs';
|
||||
/* eslint-disable max-depth */
|
||||
/* eslint-disable complexity */
|
||||
import Jimp from 'jimp'
|
||||
import fs from 'fs/promises'
|
||||
import crypto from 'crypto'
|
||||
|
||||
const invertColors = async (folder: string) => {
|
||||
try {
|
||||
|
||||
const files = fs.readdirSync(folder);
|
||||
// Filtre cool
|
||||
const imageFiles = files.filter(file => file.match(/\.(jfif)$/));
|
||||
for (const file of imageFiles) {
|
||||
// read image
|
||||
const image = await Jimp.read(`${folder}/${file}`);
|
||||
image.invert();
|
||||
// change name
|
||||
const newFileName = '.' + file;
|
||||
// save image with new file name
|
||||
await image.writeAsync(`${folder}/${newFileName}`);
|
||||
console.log("Images have been inverted and rename " + '${newFileName}')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
;(async (folder: string) => {
|
||||
|
||||
invertColors('./images');
|
||||
|
||||
const files = await fs.readdir(folder)
|
||||
// Filtre cool
|
||||
const imageFiles = files.filter((file) => !file.startsWith('.'))
|
||||
const data = (await fs.readFile('./output.json')).toString('utf8')
|
||||
|
||||
let json: Record<string, string> = {}
|
||||
try{
|
||||
json = JSON.parse(data)
|
||||
|
||||
}catch{
|
||||
console.log('handeling...')
|
||||
}
|
||||
|
||||
|
||||
console.log('previous entries', json)
|
||||
|
||||
|
||||
for await (const file of imageFiles) {
|
||||
|
||||
const hash = crypto.createHash('sha256')
|
||||
hash.setEncoding('hex')
|
||||
// the text that you want to hash
|
||||
const readI = await fs.readFile(`${folder}/${file}`)
|
||||
hash.write(readI)
|
||||
hash.end()
|
||||
const sha256sum = hash.read()
|
||||
|
||||
if (json[file] === sha256sum) {
|
||||
console.log('This picture has already been turned to sepia')
|
||||
continue
|
||||
}
|
||||
|
||||
// Modification des données
|
||||
json[file] = sha256sum
|
||||
|
||||
console.log('current entries', json)
|
||||
|
||||
// Ré-écriture dans le fichier output
|
||||
await fs.writeFile('output.json', JSON.stringify(json))
|
||||
|
||||
const image = await Jimp.read(readI)
|
||||
// read image
|
||||
image.sepia()
|
||||
// change name
|
||||
const newFileName = '.' + file
|
||||
// save image with new file name
|
||||
await image.writeAsync(`${folder}/${newFileName}`)
|
||||
console.log('Images have been inverted and rename ' + '.' + file)
|
||||
}
|
||||
|
||||
})('./images')
|
||||
|
Reference in New Issue
Block a user