Skip to content

Instantly share code, notes, and snippets.

@dipanshuchaubey
Last active May 25, 2021 15:10
Show Gist options
  • Save dipanshuchaubey/8e7b41e2072ffb264f2767d4fa363411 to your computer and use it in GitHub Desktop.
Save dipanshuchaubey/8e7b41e2072ffb264f2767d4fa363411 to your computer and use it in GitHub Desktop.
/**
* This script will traverse your filesystem and convert all
* Images to desired format.
*
* REQUIRED PARAMETERS
* 1. Source directory path.
* (This is the path from where images will be converted)
* 2. Output format. 'default = .jpg'
**/
const sharp = require('sharp')
const path = require('path')
const fs = require('fs')
async function ls(loc) {
const dir = await fs.promises.opendir(loc)
for await (const dirent of dir) {
const fromPath = path.join(loc, dirent.name)
const stat = await fs.promises.stat(fromPath)
if (stat.isFile()) {
const fileName = dirent.name.replace(/\.[^.]*$/, '') + '.jpg' // <== Change this to your desired format
sharp(fromPath).toFile(`./output/${fileName}`)
}
}
}
ls('/your/source/path/here').catch(console.error) // <== Enter source path here

Image Conversion

This script will traverse your filesystem and convert all Images to desired format.

REQUIRED PARAMETERS

  1. Source directory path. (This is the path from where images will be converted)

  2. Output format. default = .jpg

Note - All the converted images will be stored in a new directory in the source directory named as output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment