Convert World Maps with pnmmercator Without Losing Your Source
You will finish with a repeatable way to turn a latitude-longitude world map into a Mercator image, reverse that conversion, and check the output without overwriting the input. The examples use pnmmercator from Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1.
The route
Jump straight to the step you need, or tick off Done means at the end.
- 1. Confirm the installed command
- 2. Check the source map before converting
- 3. Convert rectangular projection to Mercator
- 4. Choose between blended and exact input colours
- 5. Convert Mercator back to a rectangular map
- 6. Use standard input and diagnostics when needed
- 7. Replace an existing result only after checking it
Allow about fifteen minutes. You need a readable Netpbm image whose columns cover -180 to +180 degrees longitude and whose rows cover -90 to +90 degrees latitude. The command accepts standard input, so the examples also work in a pipeline. No elevated privileges are needed unless your input or output directory is restricted.
Safety: shell redirection with > truncates an existing destination before the converter starts. Use a new output name while testing. Keep the source image until you have checked the result.
1. Confirm the installed command
Check the executable and Netpbm version. This is read-only:
$ command -v pnmmercator
/usr/bin/pnmmercator
$ pnmmercator --version
pnmmercator: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pnmmercator: Built from source dated 2024-03-31 09:09:47
...
The version output includes build details that can vary. The useful checkpoint is the Netpbm version. If pnmmercator --version is unavailable on another release, use your package manager's query command and read man pnmmercator.
2. Check the source map before converting
The input is not an arbitrary photograph. It must be a rectangular projection with rows representing latitude from -90 to 90 degrees and columns representing longitude from -180 to +180 degrees. It is often twice as wide as it is high, but that ratio is not required.
Ask the installed tools what they see:
$ pamfile /path/to/world-map.ppm
/path/to/world-map.ppm: PPM raw, 3600 by 1800 maxval 255
$ test -r /path/to/world-map.ppm && echo readable
readable
Your file may be PPM, PGM, PBM or PAM, and pamfile may describe it differently. Confirm the dimensions and format using a tool that is installed on your system. A map with cropped poles or a different geographic extent is not suitable just because it looks like a world map.
Checkpoint
Record the original path and dimensions. Do not delete or replace the source during this guide.
3. Convert rectangular projection to Mercator
Give the source as the final argument and redirect standard output to a fresh file:
$ pnmmercator /path/to/world-map.ppm > /path/to/world-map-mercator.ppm
$ printf 'exit status: %s\n' "$?"
exit status: 0
$ pamfile /path/to/world-map-mercator.ppm
/path/to/world-map-mercator.ppm: PPM raw, 3600 by 3600 maxval 255
The command writes a PPM image, and the converted image is twice the height of the input. Its width is unchanged. The exact pamfile wording and maxval can vary, so the important checks are a successful exit status, a non-empty output, and the expected dimensions.
Mercator stretches regions towards the poles. The final few degrees above 85 degrees north and below 85 degrees south cannot be represented usefully, so edge rows can show banding. That is a property of this projection, not evidence that the command failed.
4. Choose between blended and exact input colours
By default, pnmmercator interpolates between adjacent source rows. This reduces visible bands, but it blends colours and can soften sharp boundaries. If preserving only colours already present in the input matters more than smoothness, use -nomix:
$ pnmmercator -nomix /path/to/world-map.ppm > /path/to/world-map-mercator-nomix.ppm
$ pamfile /path/to/world-map-mercator-nomix.ppm
/path/to/world-map-mercator-nomix.ppm: PPM raw, 3600 by 3600 maxval 255
The trade-off is visible banding near the top and bottom. For a high-quality result, start with a high-resolution source and reduce the converted image afterwards with a separate Netpbm scaling tool:
$ pamscale -width 1800 /path/to/world-map-mercator.ppm > /path/to/world-map-mercator-small.ppm
pnmmercator does not resize images itself. Treat scaling as a separate step and verify that step separately.
5. Convert Mercator back to a rectangular map
Use -inverse when the input is already a Mercator map:
$ pnmmercator -inverse /path/to/world-map-mercator.ppm > /path/to/world-map-rectangular.ppm
$ pamfile /path/to/world-map-rectangular.ppm
/path/to/world-map-rectangular.ppm: PPM raw, 3600 by 1800 maxval 255
The inverse output has half the input height. The Mercator image contains no reliable information close to the poles, so the top output rows are copied from the row corresponding to about 85 degrees north; the bottom rows are treated the same way. Do not use the inverse result as if it recovered detail that the Mercator source never contained.
6. Use standard input and diagnostics when needed
With no filename, the command reads the image from standard input. This lets you connect it to another Netpbm command:
$ pamcut -left 0 -right 3600 /path/to/world-map.ppm | pnmmercator > /path/to/piped-mercator.ppm
Only use a pipeline when the first command really produces the geographic input that pnmmercator expects. A successful pipeline can still produce a misleading map if an earlier command changed the extent.
For progress information, add -verbose. For more detailed latitude, longitude and Mercator conversion diagnostics, use -vverbose:
$ pnmmercator -verbose /path/to/world-map.ppm > /path/to/verbose-mercator.ppm
Diagnostic text may be written to standard error, and its exact wording is version-specific. It does not change the image. If the command reports that it cannot read a Netpbm magic number, inspect the source path and file type rather than adding sudo; elevated privileges do not turn a non-image into a valid image.
7. Replace an existing result only after checking it
When you are ready to replace a previous output, write to a temporary name first and move it into place only after validation:
$ pnmmercator /path/to/world-map.ppm > /path/to/world-map-mercator.ppm.new
$ pamfile /path/to/world-map-mercator.ppm.new
$ mv /path/to/world-map-mercator.ppm.new /path/to/world-map-mercator.ppm
The mv command changes the destination name. It does not alter the source image. If conversion or checking fails, leave the old output in place and remove only the incomplete .new file after confirming its exact path.
Done means
- The installed Netpbm version and source image dimensions are known.
- A rectangular world map was converted to an output twice its original height.
- You chose interpolation or
-nomixdeliberately. -inversewas used only for a Mercator input, with polar information treated as unavailable.- Every output was checked before replacing an existing file, and the original source remains intact.