Inspect and Safely Hand Off PNG Files on Linux
You will identify a PNG, check its signature and dimensions, and pass it to another tool without damaging the original. Allow about ten minutes for one file. This guide uses the installed png(5) manual from libpng-dev version 1.6.43-5ubuntu0.6. The manual describes the file format, not a service, daemon or editable Linux configuration file.
The route
Jump straight to the step you need, or tick off Done means at the end.
A PNG is a lossless raster image format. It can contain indexed-colour, greyscale or truecolour data, with an optional alpha channel. Sample depths range from 1 to 16 bits. It is designed to be streamed and can carry integrity, gamma and chromaticity information. Those properties affect how an image is interpreted, but they do not mean that a PNG should be edited as ordinary text.
1. Confirm the tools and input
Start in a working directory where you can create a temporary copy. Reading an image and writing a report normally need no elevated privileges. Do not use sudo merely because a file came from another user or a system directory; first check the permissions and choose a writable destination.
$ command -v file
/usr/bin/file
$ INPUT='/path/to/image.png'
$ test -r "$INPUT" && echo 'input is readable'
input is readable
Replace the placeholder with the real path. Keep the variable quoted. A path containing spaces is valid, and unquoted shell variables can turn one input into several arguments.
Checkpoint: if the readability test does not print anything, stop and fix the path or permissions. Do not make the file world-readable as a quick workaround. If access genuinely has to be granted, use the narrowest ordinary permission or group change approved for that machine, then undo it when the hand-off is complete.
2. Identify the file before opening it
Ask file what it sees. This examines the content rather than trusting the filename extension, so it can expose a renamed or incomplete file.
$ file -- "$INPUT"
/path/to/image.png: PNG image data, 800 x 600, 8-bit/color RGBA, non-interlaced
The wording varies with the installed file database. Look for PNG, the dimensions, and the colour description. A name ending in .png is not proof that the contents are PNG data. Conversely, a valid PNG may have another filename.
If you need to check the fixed PNG signature directly, read only the first eight bytes:
$ od -An -tx1 -N8 -- "$INPUT"
89 50 4e 47 0d 0a 1a 0a
Those bytes are the PNG signature. This command does not alter the file. If the bytes differ, stop treating the input as PNG and investigate its source. A correct signature alone does not prove that every later chunk is intact.
3. Preserve the original while making a work copy
Make a copy before converting, resizing or uploading anything. The PNG format is lossless, but a later application can still overwrite the file or discard metadata when it saves a new image.
$ cp --preserve=all -- "$INPUT" ./image-work.png
$ file -- ./image-work.png
./image-work.png: PNG image data, 800 x 600, 8-bit/color RGBA, non-interlaced
Use a different destination when the copy is in the same directory as the source. If the destination already exists, inspect it first rather than blindly replacing it:
$ test -e ./image-work.png && echo 'destination already exists'
destination already exists
At that point choose another name, or move the existing destination to a clearly named backup after checking that it is the file you intend to replace. Moving a file is still a state change. Do not delete the backup until the new result has been checked.
4. Hand the copy to an image tool
Use the work copy as the input to an image viewer, conversion utility or application that understands PNG. A PNG may contain an alpha channel and colour information beyond the visible pixels, so check the receiving tool's import and export settings if transparency or colour matching matters.
$ command -v identify
/usr/bin/identify
$ identify -- ./image-work.png
./image-work.png PNG 800x600 800x600+0+0 8-bit sRGB 12345B 0.000u 0:00.000
identify is an ImageMagick command and is not supplied by libpng-dev. If it is absent, use another installed image reader or retain the result from file. Do not install a package inside a restricted production workflow without following that system's change process.
When a conversion produces a replacement, write it to a new path first. For example, an ImageMagick operation can be staged outside the original:
$ identify -- ./image-work.png > ./image-work.report
$ test -s ./image-work.report && echo 'report written'
report written
The exact conversion command depends on the receiving tool and the desired output. Avoid presenting an unverified conversion as a property of png(5): the manual defines the format, while applications decide which chunks and metadata they preserve.
5. Diagnose the common traps
If file reports only "data", check the first eight bytes and compare them with the PNG signature. A mismatch usually means the extension is wrong or the transfer did not deliver a PNG. If the signature is correct but a decoder rejects the file, keep the original, capture the decoder's error, and obtain a fresh copy from the producer. Do not repair binary bytes in a text editor.
If an image looks different after hand-off, compare more than its filename. The original may have an alpha channel, a different sample depth, or gamma and chromaticity information. A receiving application can choose to flatten transparency or convert colour. Keep the source PNG until the exported result has been checked in its final destination.
Do not confuse compression with quality loss. PNG stores raster data losslessly, but converting it to another format can be lossy, and repeatedly opening and saving it can change metadata. If the requirement is an exact hand-off, copy the original and verify the destination rather than exporting it through an editor.
Done means
fileidentified the content as PNG, with dimensions recorded.- The first eight bytes matched the PNG signature when a direct check was needed.
- The original file was not edited or overwritten.
- A separate work copy was used for inspection or conversion.
- You accounted for transparency, sample depth, colour metadata and possible export changes.
- Any temporary report or backup can be removed after verification, and the source remains available for recovery.