Guide
How to Convert PNG to PGM (Portable Graymap)
Turn PNG images into PGM grayscale images for computer vision, 3D printing, and scientific tools that expect PNM formats.
By Sorawi Tools Team · Published July 13, 2026 · Updated July 27, 2026
What Is a PGM File?
PGM stands for Portable Graymap, and it is part of the Netpbm family of image formats that also includes PBM (Portable Bitmap) for black-and-white images and PPM (Portable Pixmap) for color. The defining trait of the whole family is simplicity. A PGM file is a short text header followed by the raw pixel values, with no compression, no color profiles, no thumbnails, and no metadata of any kind. That makes it almost trivially easy to read or write in any programming language, which is why the format has survived for decades in places that care more about direct pixel access than about file size. A PGM stores a grayscale image. Each pixel holds a single value that describes its brightness, normally an integer from 0 (black) to 255 (white). Some files use larger ranges such as 0 to 1023 or 0 to 4095 for high bit depth work. Because there is exactly one value per pixel, a PGM is always more compact than an equivalent color file and, more importantly, its layout is completely predictable: the bytes in the image data map directly onto the pixels in left-to-right, top-to-bottom order. PGM comes in two variants. The ASCII variant starts with the magic number P2 and writes every pixel as a readable decimal number separated by whitespace. The binary variant starts with P5 and stores each pixel as a raw byte. The P5 form is the one you will encounter in practice because it is smaller and faster to parse, and it is the format produced by the PNG to PGM converter on this site. You will rarely open a PGM in a photo editor, and your operating system may not preview it at all. That is normal. PGM is an interchange and processing format aimed at software, not a display format aimed at people.
How PNG to PGM Conversion Works
Converting a PNG into a PGM is not just a matter of deleting a channel. A PNG stores three color values per pixel, one each for red, green, and blue, plus an optional alpha value for transparency. A PGM stores exactly one brightness value per pixel. The conversion must therefore collapse three channels into one in a way that preserves how bright the image appears to the human eye. The standard technique is to compute a weighted average called luminance. The eye does not treat the three primaries equally: green looks brightest, red is in the middle, and blue looks darkest. The weights recommended by the ITU-R BT.601 standard are 0.299 for red, 0.587 for green, and 0.114 for blue. So the formula is luminance = (0.299 x red) + (0.587 x green) + (0.114 x blue). A neutral gray pixel with RGB values of (128, 128, 128) converts to 128. A pure red pixel, RGB (255, 0, 0), converts to roughly 76, which matches the fact that solid red appears comparatively dark on a monitor. The luminance result is almost never a whole number, so the converter rounds it to the nearest integer and clamps it into the 0 to 255 range of the output format. Because the whole calculation is done in the browser, the process is also completely private: your image never leaves your device. Transparency needs special handling. A fully transparent pixel has no visible color, so converters typically flatten the image onto a background, usually white, before applying the luminance weights. If your PNG contains transparency, dark content on transparent areas can come out lighter than you expect in the PGM because it is blended with white first. Flattening onto black instead produces the opposite effect, so choose a background that matches how you intend to use the file. One pleasant detail: if your source PNG is already a true grayscale image, the conversion is lossless. Every pixel value is preserved exactly, and you can round-trip between PNG and PGM without any quality loss.
How to Convert a PNG to PGM
The whole process takes a few seconds and runs entirely inside your browser. There is no upload, no server-side processing, no account, and nothing is stored anywhere except the file you choose to download. The PNG to PGM converter decodes the pixel data from your image, applies the luminance weights described above, and writes a P5 binary PGM file that any Netpbm-compatible tool can read. Before you convert, decide whether the PGM will be consumed by a tool that expects a particular bit depth or orientation. If you are unsure, the defaults are correct: a binary P5 file with 8-bit values. That covers virtually every use case, from computer vision pipelines to 3D printing height maps.
- 1Open the PNG to PGM converter on this site.
- 2Drag and drop your PNG file onto the upload area, or click to browse and select it.
- 3Check the live grayscale preview to confirm the result looks correct.
- 4Download the .pgm file and use it in your target application or pipeline.
Real-World Uses for PGM Files
PGM is not a format you will see in design work, but it is a quiet workhorse in image processing. OpenCV, the most popular computer vision library, can read PGM files directly with no extra plugins. Researchers and engineers therefore use PGM as a neutral exchange format between custom processing steps: one script writes a PGM, the next reads it, and nobody has to fight over PNG decoding details. OCR is another regular consumer. Preprocessing pipelines for optical character recognition commonly convert a scan to grayscale, then to PGM or PBM, before running binarization algorithms such as Otsu's method. Because PGM gives you raw pixel access, these algorithms are easy to write and debug against the format. Depth maps in some robotics and dataset projects are also stored as grayscale images and exchanged as PGM. 3D printing is a more surprising use. A PGM file can encode a height map: brightness values represent height, and slicing software converts the map into an actual physical surface. Model railroad trackbeds, PCB milling masks, and lithophanes all get produced this way. The scientific imaging world leans on the format too. ImageJ, Fiji, and a great deal of astronomy and microscopy tooling speak Netpbm fluently, and the 16-bit range available in PGM is enough for many scientific sensors. In all of these cases the reason is the same: PGM gives you the pixels, plainly and predictably, with nothing hidden. That is exactly why a simple browser conversion is so useful when you need to feed a PNG into a tool that only understands Netpbm.
Understanding the PGM File Structure
If you ever need to read or write PGM by hand, the structure is small enough to memorize. A binary PGM file looks like this: the five characters P5, a whitespace character, the width, a whitespace character, the height, a whitespace character, the maximum value, and exactly one more whitespace character, typically a newline. After that header, the pixel data follows in a single block. As a concrete example, a 3 by 2 binary PGM with a maximum value of 255 contains the header P5, then the dimensions 3 and 2, then 255, then a newline, followed by six bytes holding the pixel values in left-to-right, top-to-bottom order. Because the magic number and dimensions are ASCII, you can read the header even when the pixel data is binary. The maximum value, called maxval, determines how many bytes each pixel takes. If maxval is 255 or less, each pixel is one byte. If maxval is greater than 255, each pixel is two bytes, stored in big-endian order with the high byte first. The standard warning applies here: when two-byte pixels are involved, endianness differences between systems can scramble values, so matching the byte order is part of reading the file correctly. Note that PGM rows are not padded. Unlike BMP, which pads each row to a four-byte boundary, a PGM stores its pixels back to back, so you can compute the file size exactly as the header size plus width times height times bytes per pixel. This predictability is the whole point of the format.
Common Mistakes to Avoid
One of the most common mistakes when converting a PNG to PGM is using a plain average of the three color channels instead of a luminance-weighted average. Averaging RGB (255, 0, 0) gives you 85, but luminance gives you 76, and the difference between the two approaches is visible across an entire image: naive averaging makes red content too bright and blue content too dark compared with what your eyes expect. Always use the standard 0.299, 0.587, and 0.114 weights. Forgetting how transparency is handled is a second classic error. If your PNG has fully transparent regions and the converter flattens onto white, the resulting PGM will have bright pixels where you expected black. If you are producing a mask or height map, make sure the background flattening matches your intent before you run the conversion. Mixing up the P2 and P5 variants is another trap. The P5 binary format is what nearly all tools expect, but if a pipeline reads your file as ASCII and hits raw binary bytes, it will either fail or produce garbage. Confirm which variant your downstream tool wants. Similarly, do not expect a PGM to store color. It is grayscale by definition; if you need color, the correct Netpbm format is PPM. Finally, remember that PGM is uncompressed. A 10-megapixel grayscale image is roughly 10 MB on disk, so PGM is for processing, not for distribution. If your goal is a small file for the web, convert to PNG or WebP instead.
PGM vs. PPM: Choosing the Right Netpbm Format
When the Netpbm family is involved, the choice between PGM and PPM is usually straightforward: if you need color, choose PPM; if grayscale is fine, choose PGM. PPM follows the same header philosophy but stores three values per pixel, red, green, and blue, using the magic numbers P3 for ASCII and P6 for binary. The byte cost triples compared with PGM for the same dimensions. The generic term PNM is worth knowing. Some libraries and command-line tools accept any of the Netpbm formats interchangeably under the PNM name, and many file readers expose a single PNM decoder that detects the magic number and dispatches accordingly. If you see a tool that advertises PNM support, it almost certainly reads PBM, PGM, and PPM all together. Within PGM itself, prefer the binary P5 variant unless you have a specific reason to use ASCII. The ASCII P2 format can be convenient for debugging because you can open the file in a text editor and read the values directly, but it is roughly three times larger and slower to parse. Production pipelines should use P5. If you are converting from a source that is already grayscale, PGM is lossless and the natural choice. If your source is a color image and you plan to use it in a color context downstream, PPM keeps your options open. And if the final destination is the web, neither Netpbm format is appropriate; you want PNG, JPEG, or WebP, which the same set of tools can produce.
PNG to PGM Converter
Convert PNG images into PGM grayscale files (P5 binary format) used by classic Unix and scientific tools.
