# Image Compressor — Shrink JPEG, PNG and WebP Images in Bulk (`ponderable_hydrometer/image-compressor`) Actor

Image compressor and optimizer: compress images in bulk by URL to reduce image size, keeping the format. JPEG with mozjpeg, PNG with palette quantisation, WebP and AVIF at your quality; optional max width. Reports size before and after and links each file.

- **URL**: https://apify.com/ponderable_hydrometer/image-compressor.md
- **Developed by:** [Ponderable Hydrometer](https://apify.com/ponderable_hydrometer) (community)
- **Categories:** Developer tools, Automation
- **Features:** Limited permissions
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $1.00 / 1,000 results

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Image Compressor — Shrink JPEG, PNG and WebP Images in Bulk

**Smaller images, same format, no quality cliff.** Feed a list of image links and get compressed files back, with the bytes saved for each. JPEG goes through mozjpeg, PNG through palette quantisation; WebP and AVIF are re-encoded at your quality. Use it to reduce image size for websites, newsletters and uploads.

### What you can do with it

- **Speed up a website** by shrinking product photos, banners and blog images before you upload them.
- **Reduce image size in bulk**: list as many image links as you need, each gets its own compressed file.
- **Resize while compressing**: set `maxWidth` to downscale large originals (images are never enlarged).
- **Measure the gain**: every row reports `originalBytes`, `bytes` and `savedPercent`.

### Input

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `imageUrls` | array | — | Direct image links. |
| `quality` | integer | `75` | 1–100. |
| `maxWidth` | integer | `0` | Downscale to this width; 0 keeps the size. |
| `keepOriginalIfLarger` | boolean | `true` | Return the original when compression would not help. |

Example: `{"imageUrls": ["https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg"], "quality": 70, "maxWidth": 1600}`

### What you get

One row per image: `imageUrl`, `fileUrl` (the compressed file in the run's key-value store), `format`, `width`, `height`, `originalBytes`, `bytes`, `savedPercent`, `keptOriginal`.

### Output sample

```json
{
  "imageUrl": "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg",
  "fileUrl": "https://api.apify.com/v2/key-value-stores/abc/records/image-1.jpg",
  "format": "jpeg",
  "width": 3000,
  "height": 2000,
  "originalBytes": 2145678,
  "bytes": 812345,
  "savedPercent": 62,
  "keptOriginal": false
}
```

### Use it from the API or an AI agent

Start a run and read the results back in one HTTP call:

```bash
curl -X POST "https://api.apify.com/v2/acts/ponderable_hydrometer~image-compressor/run-sync-get-dataset-items?token=<YOUR_API_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{"imageUrls": ["https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg"], "quality": 75}'
```

AI agents can call it through the Apify MCP server: `https://mcp.apify.com?tools=ponderable_hydrometer/image-compressor`.

### How it works

The Actor downloads each image, detects its real format and re-encodes it in the same format with the sharp library: mozjpeg for JPEG, palette quantisation for PNG, and the chosen quality for WebP and AVIF. The result is saved in the run's key-value store and `fileUrl` points to it. The image is rotated according to its orientation information.

### Limits and good to know

- The format never changes; to convert between formats use an image converter.
- Compression is lossy: PNG palette quantisation reduces the number of colours and JPEG, WebP and AVIF lose detail as `quality` drops. Try 70–80 first and compare.
- Metadata such as EXIF is not guaranteed to be kept.
- With `keepOriginalIfLarger` on, an image that would grow is returned unchanged (`keptOriginal: true`). When `maxWidth` is set the resized version is always returned.
- Files live in the run's key-value store and follow its retention.
- Animated GIFs and SVGs are not supported. An image that cannot be downloaded or decoded is skipped with a warning in the log and is not billed; if every image fails, the run ends as FAILED.

### FAQ

**Which formats are supported?** JPEG, PNG, WebP and AVIF, detected from the file content, not the link.

**Can it convert PNG to WebP?** No, the format stays the same. Use an image converter for that.

**Can I upload files instead of links?** No, images are fetched from public links.

### Pricing

Pay per result — **$1.00 per 1,000 results** (one image = one result), plus Apify's standard per-run start event ($0.00005 per GB of memory, so a fraction of a cent per run). No subscription.

# Actor input Schema

## `imageUrls` (type: `array`):

Direct links to JPEG, PNG, WebP or AVIF images.

## `quality` (type: `integer`):

1–100. Lower means smaller files.

## `maxWidth` (type: `integer`):

Scale larger images down to this width. 0 keeps the size.

## `keepOriginalIfLarger` (type: `boolean`):

Return the original file when the compressed one would be bigger.

## Actor input object example

```json
{
  "imageUrls": [
    "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg",
    "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/car-1.jpg",
    "https://raw.githubusercontent.com/xinntao/Real-ESRGAN/master/inputs/children-alpha.png"
  ],
  "quality": 75,
  "maxWidth": 0,
  "keepOriginalIfLarger": true
}
```

# Actor output Schema

## `results` (type: `string`):

All rows produced by this run, as JSON.

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "imageUrls": [
        "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg",
        "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/car-1.jpg",
        "https://raw.githubusercontent.com/xinntao/Real-ESRGAN/master/inputs/children-alpha.png"
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("ponderable_hydrometer/image-compressor").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "imageUrls": [
        "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg",
        "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/car-1.jpg",
        "https://raw.githubusercontent.com/xinntao/Real-ESRGAN/master/inputs/children-alpha.png",
    ] }

# Run the Actor and wait for it to finish
run = client.actor("ponderable_hydrometer/image-compressor").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "imageUrls": [
    "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/animal-1.jpg",
    "https://raw.githubusercontent.com/danielgatis/rembg/main/examples/car-1.jpg",
    "https://raw.githubusercontent.com/xinntao/Real-ESRGAN/master/inputs/children-alpha.png"
  ]
}' |
apify call ponderable_hydrometer/image-compressor --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,ponderable_hydrometer/image-compressor"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/68HVKIOcKRHVKWtHh/builds/HTLVQE81zHKmBVxwc/openapi.json
