Skip to content

Instantly share code, notes, and snippets.

@Wang-Cankun
Created February 14, 2024 13:49
Show Gist options
  • Save Wang-Cankun/0e0b9667a1b7eb49ab3614e3b732d400 to your computer and use it in GitHub Desktop.
Save Wang-Cankun/0e0b9667a1b7eb49ab3614e3b732d400 to your computer and use it in GitHub Desktop.
Node.js Script for Resizing and Converting PNG Images
// npm install jimp
const Jimp = require('jimp')
const outputSizes = [
{ name: 'android-chrome-192x192.png', width: 192, height: 192 },
{ name: 'android-chrome-512x512.png', width: 512, height: 512 },
{ name: 'apple-touch-icon.png', width: 180, height: 180 },
{ name: 'favicon-16x16.png', width: 16, height: 16 },
{ name: 'favicon-32x32.png', width: 32, height: 32 },
{ name: 'favicon.ico', width: 32, height: 32 },
]
async function resizeImage(inputFile) {
try {
const image = await Jimp.read(inputFile)
const resizePromises = outputSizes.map((size) => {
return image
.clone()
.resize(size.width, size.height)
.writeAsync(`./output/${size.name}`)
})
await Promise.all(resizePromises)
console.log('All images have been resized successfully.')
} catch (err) {
console.error('Error resizing images:', err)
}
}
resizeImage('biocypher_logo_raw.png')
// Run:
// node convert.js
@Wang-Cankun
Copy link
Author

How to run:

  1. Suppose you have Node.js and npm are installed on your system.
  2. Create and enter the working directory, for example mkdir image-resizer. The raw png image biocypher_logo_raw.png should put into this folder
  3. Install the necessary Jimp packages with async support: npm install jimp.
  4. Using a text editor, create a new file called convert.js in your project directory. Paste the provided JavaScript code into convert.js.
  5. Run the script in the command line: node convert.js. The resized images should be in the outputfolder

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