ForHosting KIT · Images

Convert image RGB pixels to grayscale with luminance weighting

Turn red, green and blue pixel values into a true gray weighted by how the eye sees brightness: pure red becomes 76, pure green 150 and pure blue 29, while a neutral 128 gray stays 128. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon

Convert image RGB samples to grayscale without uploading a file or depending on an image decoder. Supply an ordered array of pixels, each with red, green, and blue channel values, and receive the same number of pixels with all three output channels set to a luminance-weighted gray value. The conversion emphasizes green, then red, then blue to better reflect perceived brightness than a simple average. Every input channel is validated, so incomplete pixels, nonnumeric values, and values outside the standard zero-to-255 range fail clearly instead of producing misleading output.

Prepare complete RGB sample records

Channel values may be integers or fractional numbers, but each must be finite and fall between zero and 255 inclusive. The converter deliberately requires every channel on every record. Preserve the original pixel order when constructing the array.

A single request can contain up to one hundred thousand samples. Split larger rasters into deterministic chunks and concatenate their returned sample arrays in request order. This interface works on decoded samples rather than PNG, JPEG, or WebP bytes, so decode files before calling it and encode the returned RGB records afterward if a new image file is required.

Understand the luminance-weighted calculation

For each sample, the converter calculates gray as the nearest integer to 0.299 times red plus 0.587 times green plus 0.114 times blue. These coefficients sum to one, keep neutral input pixels neutral, and account for the fact that human vision generally perceives green light as contributing more brightness than red and substantially more than blue. A plain arithmetic mean would assign equal importance to all three channels and can make saturated blue pixels look too bright or saturated green pixels too dark.

After computing the weighted value, the converter rounds once to the nearest whole channel value. It then writes that same number into r, g, and b in the returned record. Equal output channels create a neutral RGB gray without introducing a color cast.

The response also includes the sample count and a formula string, making automated logs and test reports self-describing. The function is pure: it uses no network requests, random values, current time, external model, or mutable cross-request state. If a color-managed scientific workflow requires those operations, apply them before or after this narrowly defined conversion.

Use the result safely in image pipelines

On success, read the response samples array as a positional replacement for the input samples array. Each returned record has r, g, and b set to the computed gray, so consumers expecting ordinary three-channel RGB data can accept it without a separate grayscale pixel format. The count field provides a quick integrity check: compare it with the number of records submitted before rebuilding a row, tile, thumbnail, or complete raster.

Keep those values alongside your request rather than expecting the service to infer them from a flat sequence. Alpha is intentionally absent from the contract; if the source image has transparency, retain its alpha buffer independently and combine it with the converted channels during encoding. Validation errors are useful pipeline guards.

Treat missing-channel, nonfinite-number, range, and array-size failures as data-quality problems rather than retryable service errors. Correct the decoded buffer or chunking logic before calling again. Together those cases catch weight changes, rounding differences, channel-order mistakes, and accidental weakening of validation.

Build grayscale raster buffers

Convert decoded RGB samples into neutral three-channel pixels, then place them back into the original raster layout for encoding.

Which grayscale formula is used?

Each result is round(0.299 × r + 0.587 × g + 0.114 × b), and that value is assigned to all three output channels.

Are transparency and alpha preserved?

Alpha is outside this contract. Keep source alpha values separately and recombine them with the converted RGB channels afterward.

Why does pure blue turn so much darker than pure green?

Because the eye sees green as far brighter than blue, the formula gives green a weight of 0.587 and blue only 0.114. Pure green becomes 150, pure red 76 and pure blue 29.

Can I pass a PNG or JPEG file directly?

No. The conversion works on pixel values that are already decoded, so open the file first, send its red, green and blue values, and build a new image file from the gray result if you need one.

What happens if a pixel is missing one of its colors?

The conversion stops and says which pixel and which channel are missing. It never assumes the missing value is zero or copies another channel, because that would change the brightness and break the pixel order.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/image/grayscale-convert

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/image/grayscale-convert \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"samples":[{"r":255,"g":0,"b":0},{"r":0,"g":128,"b":255}]}'
{
  "samples": [
    {
      "r": 255,
      "g": 0,
      "b": 0
    },
    {
      "r": 0,
      "g": 128,
      "b": 255
    }
  ]
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "image.grayscale_convert",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_samples100000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Prepare complete RGB sample records

Send the pixels in a JSON field named samples. That field must be a nonempty array, and every array entry must be an object containing the canonical channel names r, g, and b. If a sample omits green, for example, the operation stops with an invalid_input error that identifies the sample index and missing channel. It never assumes that an omitted component is zero, copies another channel, or silently skips an incomplete pixel, because any of those choices could alter the apparent brightness and break the positional relationship between input and output.

Understand the luminance-weighted calculation

Consequently, identical JSON input always yields identical JSON output. This formula is a practical luma-style conversion for conventional RGB samples. It does not linearize gamma-encoded channels, manage ICC color profiles, apply HDR transfer functions, preserve alpha, or perform dithering.

Use the result safely in image pipelines

Because the endpoint only transforms samples, width, height, row stride, orientation, alpha, and file metadata remain the caller's responsibility. Browser use is free, while an API request costs $0.002. Useful tests include black and white endpoints, pure red, pure green, pure blue, an already-neutral gray, fractional channels, and a deliberately incomplete sample.

Create deterministic image fixtures

Generate exact expected grayscale samples for unit tests covering image filters, canvas utilities, and server-side pixel pipelines.

Check small pixel batches interactively

Paste representative RGB values in the browser before applying the same conversion through an automated API workflow.

What happens if a pixel is missing a channel?

The request fails with invalid_input and a message identifying the zero-based sample index and the missing r, g, or b channel.

Does this endpoint accept an image file?

No. It accepts already-decoded RGB sample records. Decode the image first, then encode the returned samples if you need a new file.

How much does an API request cost?

An API request costs $0.002. You can also run the same deterministic conversion in the browser for free.

Read the full KIT documentation →