Verify and Hand Off a PNG File Safely
Someone sends you a PNG that "won't open" and you need to know if the file itself is broken before you blame the tool. This guide walks through identifying a PNG, checking its signature and dimensions, and passing it to another tool without ever touching the original. Allow about ten minutes for one file. It uses the installed png(5) manual from libpng-dev version 1.6.43-5ubuntu0.6, which describes the file format itself, 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 hold indexed-colour, greyscale or truecolour data, with an optional alpha channel, and sample depths from 1 to 16 bits. It is designed to be streamed, and can carry integrity, gamma and chromaticity information. All of that shapes how an image gets interpreted, but none of it means a PNG should ever be edited as plain text.
1. Confirm the tools and input
Work from a directory where you can create a temporary copy. Reading an image and writing a report need no elevated privileges. Do not reach for sudo just because a file came from another user or a system directory; check the permissions first and pick a writable destination.
$ command -v file
/usr/bin/file
$ INPUT='/path/to/image.png'
$ test -r "$INPUT" && echo 'input is readable'
input is readable
Swap in the real path. Keep the variable quoted: a path with spaces is perfectly valid, and an unquoted shell variable can turn one input into several arguments.
Checkpoint
If the readability test prints nothing, stop and fix the path or permissions before going further. Do not make the file world-readable as a quick workaround. If access genuinely needs granting, use the narrowest permission or group change your machine allows, then undo it once the hand-off is done.
2. Identify the file before opening it
Ask file what it actually sees. This examines content rather than trusting a filename extension, so it will expose a renamed or truncated file:
$ file -- "$INPUT"
/path/to/image.png: PNG image data, 800 x 600, 8-bit/color RGBA, non-interlaced
Wording varies with the installed file database. Look for PNG, the dimensions, and the colour description. A name ending in .png proves nothing about the actual contents, and a genuine PNG can just as easily carry a different filename.
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 eight bytes are the signature every PNG starts with. This command changes nothing. If the bytes differ, stop treating the input as PNG and go investigate its source instead: a correct signature alone does not prove every later chunk is intact.
3. Preserve the original while making a work copy
Copy the file before converting, resizing or uploading anything. PNG is lossless, but the application you hand it to can still overwrite the file or drop 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 if the copy would land in the same directory as the source. If that destination already exists, check it before overwriting it blindly:
$ test -e ./image-work.png && echo 'destination already exists'
destination already exists
At that point pick another name, or move the existing file to a clearly labelled backup once you have confirmed it is actually the one you mean to replace. Moving a file is still a state change: do not delete that backup until the new result has been checked.
4. Hand the copy to an image tool
Feed the work copy, not the original, into an image viewer, converter or application that understands PNG. A PNG can carry an alpha channel and colour information beyond what you see on screen, 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, not something libpng-dev supplies. If it is missing, use whatever image reader you have installed, or fall back to what file already told you. Do not install a package inside a restricted production workflow without following that system's own change process.
When a conversion produces a replacement, write it to a new path first:
$ 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 output you want. Do not present an unverified conversion as a property of png(5) itself: the manual defines the format, applications decide which chunks and metadata they keep.
Common traps
- "data" instead of "PNG". If
filereports only "data", check the first eight bytes against the PNG signature. A mismatch usually means the extension is wrong or the transfer never delivered a real PNG. - Signature fine, decoder still fails. Keep the original, capture the decoder's error, and get a fresh copy from the producer. Do not try to repair binary bytes in a text editor.
- Image looks different after hand-off. Compare more than the filename: alpha channel, sample depth, gamma and chromaticity can all be flattened or converted by the receiving application. Keep the source PNG until the exported result has been checked in its final destination.
- Compression mistaken for quality loss. PNG itself is lossless, but converting it to another format can be lossy, and repeated opening and saving can quietly change metadata. For an exact hand-off, copy and verify the destination rather than exporting through an editor.
Done means
- Identified as PNG.
filereported the content as PNG, with dimensions recorded. - Verified the signature. The first eight bytes matched the PNG signature when a direct check was needed.
- Original untouched. The source file was never edited or overwritten.
- Used a work copy. A separate copy handled inspection and conversion.
- Accounted for metadata. Transparency, sample depth and colour metadata were checked against possible export changes.
- Kept a recovery path. Any temporary report or backup can be removed once verified, with the source still available.