can convert any format of image
This commit is contained in:
49
src/index.ts
49
src/index.ts
@ -15,8 +15,7 @@ import path from 'path'
|
||||
async function filesList(Path: string): Promise<Array<string>> {
|
||||
const files: Array<string> = []
|
||||
const subFolders = await fs.readdir(Path)
|
||||
for await (const subFolder of subFolders)
|
||||
{
|
||||
for await (const subFolder of subFolders){
|
||||
const stats = await fs.stat(Path + '/' + subFolder)
|
||||
if (stats.isDirectory()) {
|
||||
files.push(...await filesList(Path + '/' + subFolder))
|
||||
@ -24,7 +23,6 @@ async function filesList(Path: string): Promise<Array<string>> {
|
||||
} else {
|
||||
files.push(Path + '/' + subFolder)
|
||||
}
|
||||
|
||||
}
|
||||
return files
|
||||
}
|
||||
@ -33,8 +31,8 @@ async function filesList(Path: string): Promise<Array<string>> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const files = await filesList(dirPath)
|
||||
console.log(files)
|
||||
// Filtre cool
|
||||
const imageFiles = (await files).filter((file) => file.endsWith('.jfif'))
|
||||
// Filtre
|
||||
// const imageFiles = (await files).filter((file) => file.endsWith('.jpg')) // .jfif | .png | .jpeg |
|
||||
const data = (await fs.readFile('./output.json')).toString('utf8')
|
||||
|
||||
let json: Record<string, string> = {}
|
||||
@ -45,44 +43,47 @@ async function filesList(Path: string): Promise<Array<string>> {
|
||||
console.log('handeling...')
|
||||
}
|
||||
|
||||
for await (const file of files) {
|
||||
|
||||
console.log('previous entries', json)
|
||||
|
||||
|
||||
for await (const file of imageFiles) {
|
||||
|
||||
const bFile = path.basename(file)
|
||||
const pName = path.dirname(file)
|
||||
const hash = crypto.createHash('sha256')
|
||||
hash.setEncoding('hex')
|
||||
const readI = await fs.readFile(`${file}`)
|
||||
hash.write(readI)
|
||||
hash.end()
|
||||
const sha256sum = hash.read()
|
||||
console.log('bfile = ' + bFile)
|
||||
|
||||
if (json[file] === sha256sum) {
|
||||
if (bFile.startsWith('.')) {
|
||||
continue
|
||||
}else 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)
|
||||
// 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 bFile = path.basename(file)
|
||||
const pName = path.dirname(file)
|
||||
console.log(bFile)
|
||||
const newFileName = pName + '/.' + bFile
|
||||
console.log('newfilename:' + newFileName)
|
||||
// save image with new file name
|
||||
await image.writeAsync(`${newFileName}`)
|
||||
console.log('Images have been inverted and rename ' + '.' + file)
|
||||
try{
|
||||
const image = await Jimp.read(readI)
|
||||
image.sepia()
|
||||
console.log(bFile)
|
||||
const newFileName = pName + '/.' + bFile
|
||||
console.log('newfilename:' + newFileName)
|
||||
// save image with new file name
|
||||
await image.writeAsync(`${newFileName}`)
|
||||
console.log('Images have been inverted and rename ' + '.' + file)
|
||||
}catch (err){
|
||||
console.log('this file is not an image')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})('./images')
|
||||
|
Reference in New Issue
Block a user