How can I tell if an extensionless image is png or

2019-04-10 06:31发布

问题:

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?

回答1:

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


回答2:

Three methods:

  • Open a file in a Hex editor (or just a binary file viewer). PNG files start with 'PNG', .jpg files should have 'exif'or 'JFIF' somewhere in the beginning.
  • Use file command: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/file.1.html For example: file 'ysauyft87ggsa67fgeg&w=1600'
  • Use identify file like torazaburo wrote in the comments (part of imagemagick lib)