# sharp

***

**1. Resize an Image:**

```js
const sharp = require('sharp');
sharp('input.jpg').resize(300, 200).toFile('output.jpg');
```

**2. Crop an Image:**

```js
sharp('input.jpg').extract({ width: 200, height: 100, left: 100, top: 50 }).toFile('output.jpg');
```

**3. Convert Image Format:**

```js
sharp('input.png').toFormat('jpeg').toFile('output.jpg');
```

**4. Add Watermark to Image:**

```js
const composite = require('sharp-composite');
composite([
  sharp('input.jpg'),
  sharp('watermark.png').resize(100, 100)
]).blend('over').toFile('output.jpg');
```

**5. Extract Image Metadata:**

```js
sharp('input.jpg').metadata().then(metadata => {
  console.log(metadata);
});
```

**6. Create a Grayscale Image:**

```js
sharp('input.jpg').grayscale().toFile('output.jpg');
```

**7. Convert Image to Base64:**

```js
sharp('input.jpg').toBuffer().then(buffer => {
  const base64 = buffer.toString('base64');
});
```

**8. Invert Image Colors:**

```js
sharp('input.jpg').negate().toFile('output.jpg');
```

**9. Rotate Image:**

```js
sharp('input.jpg').rotate(90).toFile('output.jpg');
```

**10. Flip Image Horizontally/Vertically:**

```js
sharp('input.jpg').flip().toFile('output.jpg');
sharp('input.jpg').flop().toFile('output.jpg');
```

**11. Resize and Pad Image:**

```js
sharp('input.jpg').resize(200, 100).background({ r: 255, g: 255, b: 255 }).toFile('output.jpg');
```

**12. Extract Image Frame:**

```js
sharp('input.gif').keepFrame(1).toFile('output.jpg');
```

**13. Create a Thumbnail:**

```js
sharp('input.jpg').resize(100).max().toFile('output.jpg');
```

**14. Resize Image with Aspect Ratio:**

```js
sharp('input.jpg').resize({
  width: 200,
  height: null,
  fit: 'cover'
}).toFile('output.jpg');
```

**15. Blur Image:**

```js
sharp('input.jpg').blur(10).toFile('output.jpg');
```

**16. Sharpen Image:**

```js
sharp('input.jpg').sharpen().toFile('output.jpg');
```

**17. Add Text to Image:**

```js
const draw = require('sharp-text');
draw('input.jpg', 'Hello, world').toFile('output.jpg');
```

**18. Draw Shapes on Image:**

```js
const draw = require('sharp-draw');
draw('input.jpg', {
  shapes: [{
    type: 'circle',
    x: 100,
    y: 100,
    r: 50,
    color: 'red'
  }]
}).toFile('output.jpg');
```

**19. Resize Image with Minimum Steps:**

```js
sharp('input.jpg').resizeWithoutEnlargement(300, 200).toFile('output.jpg');
```

**20. Resize Image with Percentage:**

```js
sharp('input.jpg').resize(75).toFile('output.jpg');
```

**21. Convert Image to TIFF:**

```js
sharp('input.jpg').toFormat('tiff').toFile('output.tiff');
```

**22. Create a Color Palette from Image:**

```js
const color = require('sharp-color');
color('input.jpg').palette(5).then(palette => {
  console.log(palette);
});
```

**23. Create a Histogram from Image:**

```js
const histogram = require('sharp-histogram');
histogram('input.jpg').then(histogram => {
  console.log(histogram);
});
```

**24. Pipe Sharp Output to Another Process:**

```js
const pipeline = require('util').pipeline;
pipeline(
  sharp('input.jpg').resize(300, 200),
  fs.createWriteStream('output.jpg')
);
```

**25. Batch Process Images:**

```js
const glob = require('glob');
const fsPromises = require('fs').promises;
const path = require('path');
const sharp = require('sharp');

async function batchProcessImages() {
  const files = await fsPromises.readdir('input');
  for (const file of files) {
    try {
      const outputPath = path.join('output', file);
      await sharp(`input/${file}`).resize(300, 200).toFile(outputPath);
      console.log(`Processed ${file}`);
    } catch {
      console.log(`Error processing ${file}`);
    }
  }
}
```
