# Optimize images from a Node.js script

> Learn how to optimize images from a Node.js script with sharp, converting a PNG to lossless webp and resizing or rotating it in just a few lines.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2023-01-20 | Topics: [Node.js](https://flaviocopes.com/tags/node/) | Canonical: https://flaviocopes.com/optimize-images-from-a-nodejs-script/

One tool I’ve been using to optimize images is [sharp](https://github.com/lovell/sharp).

```javascript
import sharp from 'sharp'
```

Here’s how I used it to convert images to [webp](https://flaviocopes.com/webp/):

```javascript
await sharp('image.png')
  .webp({ lossless: true })
  .toFile('image.webp')
```

It can do a lot more image processing stuff like rotate resize etc.

If you just need to resize or compress a couple of images without writing a script, I made a browser-based [image resizer tool](https://flaviocopes.com/tools/image-resizer/) — nothing gets uploaded anywhere.
