How can I tell if an extensionless image is png or jpeg
Asked Answered
D

5

11

A while ago I saved some images generated by a web service so the file names are e.g 'ysauyft87ggsa67fgeg&w=1600'. I can open and manipulate these images OK, I'd just like to know what encoding they are (it's almost certainly png or jpeg). I've tried 'get info' in OSX, but it thinks the files are just text (even though it generates thumbnails correctly and I can view the images in preview). If I serve the images on a server they are delivered as 'application/octet-stream'.

How can I easily determine whether they are png or jpeg via the shell?

Deadlock answered 27/12, 2014 at 18:11 Comment(3)
imagemagick's identify?Interlay
Do you have the file command on Os X ?Lindseylindsley
If you have a windows machine at hand, this might be related/interesting: #59010Afc
F
34

The "magic code" for a

  • PNG file should begin with 89 50 4E 47 0D 0A 1A 0A
  • JPEG files begin with FFD8 and end with FFD9
Fbi answered 27/12, 2014 at 18:20 Comment(1)
vi and Windows Notepad did not show the hex values (it showed characters instead), but less showed the hex values.Cheekpiece
S
20

Three methods:

Saarinen answered 27/12, 2014 at 18:14 Comment(0)
F
1

In Imagemagick, you can use the "magick number" %m to get the suffix.

magick lena barn -format "%m\n" info:

PNG
JPEG

If on IM 6, use convert in place of magick

Flathead answered 29/6, 2024 at 15:30 Comment(0)
C
0
head -c 20 thefile

head -c 20 *

This will print out the first 20 characters of a single file or all the files in the directory.

JFIF means JPEG File Interchange Format

PNG means PNG

WEBPVP8 means WebP

Cheekpiece answered 29/6, 2024 at 6:58 Comment(0)
P
0

Let's create an 8x8 black image and save it without extensions in PNG, TIFF, JPEG, TGA and GIF formats using ImageMagick:

magick -size 8x8 xc: -write PNG:a -write TIFF:b -write JPEG:c -write TGA:d GIF:e

Look at the files we made:

-rw-r--r--  1 mark  staff       296 29 Jun 11:09 a    # extensionless PNG
-rw-r--r--  1 mark  staff       386 29 Jun 11:09 b    # extensionless TIFF
-rw-r--r--  1 mark  staff       160 29 Jun 11:09 c    # extensionless JPEG
-rw-r--r--  1 mark  staff        82 29 Jun 11:09 d    # extensionless TGA
-rw-r--r--  1 mark  staff        48 29 Jun 11:09 e    # extensionless GIF

Now run the file command on all the files:

file a b c d e

Output

a: PNG image data, 8 x 8, 1-bit grayscale, non-interlaced
b: TIFF image data, little-endian, direntries=15, height=8, bps=16, compression=none, PhotometricIntepretation=BlackIsZero, orientation=upper-left, width=8
c: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 8x8, components 1
d: Targa image data - Mono 8 x 8 x 8 - top
e: GIF image data, version 89a, 8 x 8

Or, as you are on macOS, you can use the built-in sips. Here I get the format and the dimensions (for added fun):

sips -1 -g format -g pixelWidth -g pixelHeight a b c d e

Output

a|  format: png|  pixelWidth: 8|  pixelHeight: 8|
b|  format: tiff|  pixelWidth: 8|  pixelHeight: 8|
c|  format: jpeg|  pixelWidth: 8|  pixelHeight: 8|
d|  format: tga|  pixelWidth: 8|  pixelHeight: 8|
e|  format: gif|  pixelWidth: 8|  pixelHeight: 8|

Or, you can look at the first few byes of the files with xxd like this:

for f in ? ; do xxd "$f" | head -1 ; done

Output

00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR     # This one is PNG
00000000: 4949 2a00 8800 0000 ffff ffff ffff ffff  II*.............     # This one is a TIFF, but "MM" is TIFF in Motorola order rather than II for Intel order
00000000: ffd8 ffe0 0010 4a46 4946 0001 0100 0001  ......JFIF......     # This is a JPEG
00000000: 0000 0300 0000 0000 0000 0000 0800 0800  ................     # This is tough!
00000000: 4749 4638 3961 0800 0800 f000 00ff ffff  GIF89a..........     # This is a GIF
Pantomime answered 29/6, 2024 at 10:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.