I have a PNG file but i can't tell if it's PNG-8 or PNG-24?

up vote 23 down vote accepted

The file command use in other answers is sometimes inconsistent. For example:

───────────────────────────────────────────────────────────────────────────────
rick@dell:~/Pictures$ file "256 colors.jpg"
256 colors.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 96x96, segment length 16, baseline, precision 8, 800x800, frames 3
───────────────────────────────────────────────────────────────────────────────
rick@dell:~/Pictures$ file "vnStat.png"
vnStat.png: PNG image data, 410 x 121, 8-bit/color RGB, non-interlaced

The first instance is likely a camera picture and the second instance I took using a screen capture.

The identify command, which my answer is based on, appears to work correctly in all instances. For example testing on the first instance above:

rick@dell:~/Pictures$ identify "256 colors.jpg"
256 colors.jpg JPEG 800x800 800x800+0+0 8-bit sRGB 45KB 0.000u 0:00.000

I have ImageMagick installed which allows me to use:

$ identify -verbose vnStat.png
Image: vnStat.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 410x121+0+0
  Units: Undefined
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 49610
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 12.0814 (0.0473779)
      standard deviation: 46.7032 (0.18315)
      kurtosis: 18.8998
      skewness: 4.49862
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 21.1804 (0.0830606)
      standard deviation: 57.5336 (0.225622)
      kurtosis: 6.68226
      skewness: 2.82613
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 14.2606 (0.0559237)
      standard deviation: 50.2969 (0.197243)
      kurtosis: 13.5573
      skewness: 3.85914
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 15.8408 (0.0621207)
      standard deviation: 51.7078 (0.202776)
      kurtosis: 11.8528
      skewness: 3.62638
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 410x121+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2017-01-24T20:04:50-07:00
    date:modify: 2017-01-24T20:04:50-07:00
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 2
    png:IHDR.color_type: 2 (Truecolor)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 410, 121
    png:sRGB: intent=0 (Perceptual Intent)
    png:text: 1 tEXt/zTXt/iTXt chunks were found
    signature: e9f31b79da6ce46cdea5da21ae8648496faa181b0621098aa2dbbdff4a9a4502
    Software: gnome-screenshot
  Artifacts:
    filename: vnStat.png
    verbose: true
  Tainted: False
  Filesize: 14.7KB
  Number pixels: 49.6K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org

For a summary report omit all parameters:

$ identify vnStat.png
vnStat.png PNG 410x121 410x121+0+0 8-bit sRGB 14.7KB 0.000u 0:00.000

Replace vnStat.png with your own .png file name and preface it with the path if necessary.


To understand PNG-24 vs PNG-32 this answer from SuperUser summarizes it:

If your image is 24 bit you will see:

Channel depth:
  red: 8-bit
  green: 8-bit
  blue: 8-bit

If your PNG image is 32 bit you will see:

Channel depth:
  red: 8-bit
  green: 8-bit
  blue: 8-bit
  alpha: 8-bit

However this answer is not complete because 8-bit color also shows 8 bits for Red, Green and Blue, only a color palette is used to limit the number of selections (like in good old Windows 2.0).


Examine these three pictures from Wikipedia Depth Article:

8_bit.png

File Name: 8_bit.png

Using:

identify -verbose  8_bit.png | grep colors
    png:PLTE.number_colors: 249

We see there are 249 colors used. The maximum possible with 8 bits is 256 colors.


4 bit

File Name: 4_bit.png

Using:

identify -verbose  4_bit.png | grep colors
    png:PLTE.number_colors: 16

We see there are 16 colors used which is the maximum color combinations possible with 4 bits.


2_bit.png

File Name: 2_bit.png

Using:

$ identify -verbose  2_bit.png | grep colors
    png:PLTE.number_colors: 4

We see there are 4 colors used which is the maximum color combinations possible with 2 bits.

32-bit PNG (8 bit per channel - w/ alpha, also called "24-bit PNG with 8-bits of alpha" or "PNG24 with alpha"):

$ file zyne_zyne.png: 
PNG image data, 64 x 64, 8-bit/color RGBA, non-interlaced

24-bit PNG (8 bit per channel - no alpha channel)

$ file 24.png
PNG image data, 1060 x 821, 8-bit/color RGB, non-interlaced

8-bit PNG (256 colors):

$ file 8bit.png
PNG image data, 1060 x 821, 8-bit colormap, non-interlaced
  • And the badge is mine :-) – Rinzwind Aug 7 '17 at 6:17
  • 1
    Palette PNGs do still have 8 bits per RGBA component, they're just limited to 256 palette entries. (Each pixel is an 8-bit or smaller index into the palette, rather than storing pixel data literally). It's not totally clear which this is. PNG24 is also 8 bits per component (not 24 bits per component). So this would be a better answer if you showed output from both kinds of PNG. – Peter Cordes Aug 7 '17 at 8:37
  • I misunderstood what you meant by "badge" 8 hours ago but now I do cause I woke up with a bronze and silver :) – WinEunuuchs2Unix Aug 7 '17 at 14:44
  • 1st to 10 badge is always the winner only trumped by 1st to 100 ;-) – Rinzwind Aug 7 '17 at 14:45
  • 1
    @evilSnobu> file is my primary file identification tool and I'm always amazed at how much it can tell even about exotic file formats. Granted, its implementation grew quite a bit since the 80s :) – spectras Aug 7 '17 at 21:49

If you have exiftool installed, you could do:

bash-4.3# exiftool continents_blank_l.png 
ExifTool Version Number         : 10.20
File Name                       : continents_blank_l.png
Directory                       : .
File Size                       : 170 kB
File Modification Date/Time     : 2016:07:05 07:59:28+05:30
File Access Date/Time           : 2017:08:07 00:35:15+05:30
File Inode Change Date/Time     : 2016:07:05 07:59:28+05:30
File Permissions                : rw-r--r--
File Type                       : PNG
File Type Extension             : png
MIME Type                       : image/png
Image Width                     : 1100
Image Height                    : 850
Bit Depth                       : 8
Color Type                      : RGB with Alpha
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Noninterlaced
Background Color                : 255 255 255
Pixels Per Unit X               : 3937
Pixels Per Unit Y               : 3937
Pixel Units                     : meters
Modify Date                     : 2016:07:05 02:29:28
Image Size                      : 1100x850
Megapixels                      : 0.935

The hacker's way: peek into the real bytes inside the IHDR PNG chunk.

 $ od -j 24 -N 2 -t u1 -An drivemount-applet_open.png
   8   2

The first number tells you the bitdepth. The second, the colour type (2 is for RGB images, that is, images with three channels, or three sample values per pixel).

Hence, in this case we have a RGB8 PNG (24 bits per pixel).

  • Isn't that PNG24 (8-bit per channel)? PNG8 is 8-bit colormap = 256 colors. – evilSnobu Aug 8 '17 at 18:25
  • 1
    Yes, RGB8 corresponds to PNG24. – leonbloy Aug 8 '17 at 18:29
  • Right. My bad. I read that as PNG8 not RGB8. Your answer is entirely correct. – evilSnobu Aug 8 '17 at 19:19

As an alternative to the above use of "file" which relies on file magic in the header of the PNG file, if working with PNG files, it is useful to have the "pngtools" package installed.

pnginfo can then be used to give full details of the properties of the PNG file including the BitDepth (Bits/Sample) (which should not to be confused with another property, Pixel Depth).

 file  Debian_Expo.png

 Debian_Expo.png: PNG image data, 1280 x 1024, 8-bit/color RGB, non-interlaced

 ---

 pnginfo Debian_Expo.png

 Image Width: 1280 Image Length: 1024
 Bitdepth (Bits/Sample): 8
 Channels (Samples/Pixel): 3
 Pixel depth (Pixel Depth): 24
 Colour Type (Photometric Interpretation): RGB 
 ...

The equivalent for JPEG files is jpeginfo, in its own package jpeginfo.

The format of the output of pnginfo makes it easier to extract the value of the BitDepth (as it is on a separate line) if it is being used in a shell script.

Should the file not actually be a PNG file, because eg it is a misnamed JPEG, then pnginfo will report "This file is not a valid PNG file".

As always the appropriate tool for the job is determined, amongst other things, by how often you are going to use the tool and how you wish to process the output.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.