Compress image to a file size
Compress an image to fit under a target file size in KB, such as 100 KB or 2 MB, for upload limits and forms.
Input
Result
Choose a file and press Run.
Files you upload and the results are stored for one hour, then deleted. Anyone who has the link can open them, so don't upload anything private.
Use it from code or an agent
# 1. Upload the file. The reply ends with a line "ref <id>/f1": that is the file reference.
curl -T sample.jpg https://setget.net/drop/
# 2. Run the tool with that reference. "wait" holds the reply for up to 30 seconds while the job runs.
curl -s https://setget.net/v1/tools/image-compress-to-size \
-H 'Content-Type: application/json' \
-d '{"input":{"image":"DROP_ID/f1","target_kb":10},"wait":30}'
# 3. The reply is {"tool", "ms", "result", "files": [{"name", "size", "url", "ref"}], "job"}.
# Download a result file from its "url", or pass its "ref" to another tool.
# Still running after the wait? The reply is 202 with "job": follow it with
# curl -s https://setget.net/v1/jobs/JOB_ID?wait=30
import json, urllib.parse, urllib.request
BASE = "https://setget.net"
def call(method, path, body=None, headers=None):
request = urllib.request.Request(BASE + path, body, headers or {}, method=method)
return json.load(urllib.request.urlopen(request))
def upload(path):
"""Step 1: upload a file and get its reference."""
name = path.replace("\\", "/").rsplit("/", 1)[-1]
with open(path, "rb") as handle:
reply = call("PUT", "/drop/" + urllib.parse.quote(name), handle.read(), {"Accept": "application/json"})
return reply["file"]["ref"]
# Step 2: run the tool with the reference.
body = {"input": {'image': upload("sample.jpg"), 'target_kb': 10}, "wait": 30}
reply = call("POST", "/v1/tools/image-compress-to-size", json.dumps(body).encode(), {"Content-Type": "application/json"})
# Step 3: a job that is still running comes back as {"job": ...}. Wait for it.
while "result" not in reply and reply.get("job", reply).get("status") in ("queued", "running"):
reply = call("GET", "/v1/jobs/" + reply.get("job", reply)["id"] + "?wait=30")
if "error" in reply:
raise SystemExit(reply["error"]["message"])
print(reply["result"])
for file in reply["files"]: # result files: download from "url", or pass "ref" to another tool
print(file["name"], file["size"], file["url"], file["ref"])
const BASE = "https://setget.net";
// Step 1: upload a file and get its reference. file: a File or Blob
// (in Node: new Blob([fs.readFileSync(path)])).
async function upload(file, name) {
const response = await fetch(`${BASE}/drop/${encodeURIComponent(name)}`, {
method: "PUT", headers: {Accept: "application/json"}, body: file,
});
return (await response.json()).file.ref;
}
// Step 2: run the tool with the reference.
let reply = await (await fetch(`${BASE}/v1/tools/image-compress-to-size`, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({input: {"image":await upload(file, "sample.jpg"),"target_kb":10}, wait: 30}),
})).json();
// Step 3: a job that is still running comes back as {job}. Wait for it.
while (!reply.result && ["queued", "running"].includes((reply.job || reply).status)) {
reply = await (await fetch(`${BASE}/v1/jobs/${(reply.job || reply).id}?wait=30`)).json();
}
if (reply.error) throw new Error(reply.error.message);
console.log(reply.result);
for (const file of reply.files) console.log(file.name, file.size, file.url, file.ref);
1. Upload. Tool: drop
Arguments:
{
"action": "create",
"files": [
{
"name": "sample.jpg",
"size": "<bytes>"
}
]
}
PUT each file's bytes to its "upload.url" with the listed headers, and keep its "ref".
With a shell, one command does both: curl -T FILE https://setget.net/drop/
2. Run. Tool: run_tool
Arguments:
{
"slug": "image-compress-to-size",
"input": {
"image": "DROP_ID/f1",
"target_kb": 10
}
}
3. The answer holds "result" and "files" (each with "url" and "ref").
If it holds a "job" that is still running instead, follow it. Tool: job
Arguments:
{
"action": "wait",
"id": "JOB_ID"
}
{
"input": {
"image": "DROP_ID/f1",
"target_kb": 10
},
"wait": 30
}
This is a file tool, so a call takes three steps: upload the file to get a reference, run the tool with that reference, then read the result and download its files. Uploads and results are public by link and expire after one hour.
Endpoint: POST /v1/tools/image-compress-to-size. Over MCP: run_tool with slug: "image-compress-to-size".
Machine-readable: input schema, Markdown. Connect an agent.
Inputs
| Name | Type | Required | What it does |
|---|---|---|---|
image |
file | yes | The image to compress. In the API: a file reference such as DROP_ID/f1. Accepts: image. |
target_kb |
number | yes | Largest allowed file size in kilobytes. 1 MB is 1000 KB. From 0.1 to 50000. |
format |
string | no | Format of the result. Leave empty to keep JPG, WebP or AVIF; other images become JPG, or WebP when they have transparency. One of: jpg, webp, avif. |
Examples
Fit a photo under 10 KB
{
"image": "sample.jpg",
"target_kb": 10
}Press the example button above to run it on the sample file.
Under 4 KB as WebP
{
"image": "sample.png",
"target_kb": 4,
"format": "webp"
}Press the example button above to run it on the sample file.
Questions
- How does it reach the size?
- It looks for the highest quality that fits. If even a low quality is too big, it makes the image smaller in pixels and tries again. The result reports the quality and scale used.
- Is the result always under the target?
- Yes. A kilobyte is counted as 1000 bytes, so the file also passes limits that count 1024 bytes per KB. If the target cannot be reached you get an error instead of an oversized file.
- What happens to transparency?
- WebP and AVIF keep it. JPG cannot, so transparent areas turn white.
Related tools
- Compress imageMake an image file smaller by re-encoding it at a chosen quality, with an optional maximum width.
image-compress - Resize imageResize an image to a width, a height or both, keeping its proportions, cropping to fill, or stretching.
image-resize - Convert image formatConvert an image between PNG, JPG, WebP, AVIF, GIF, BMP, TIFF and ICO. Animated GIF and WebP stay animated.
image-convert - Add a text watermarkAdd a text watermark to an image in a corner, in the centre or tiled across it, with adjustable opacity and size.
image-watermark - Add padding or a borderAdd padding, margin or a coloured border around an image, the same on every side or set per side.
image-pad - Add text to an imageWrite text on an image at a position you choose, with size, colour, an outline or a background box. Multi-line.
image-text-overlay