<p>Is there a good way to see what format an image is, without having to read the entire file into memory? </p>

<p>Obviously this would vary from format to format (I'm particularly interested in TIFF files) but what sort of procedure would be useful to determine what kind of image format a file is without having to read through the entire file?</p>

<p><strong>BONUS</strong>: What if the image is a Base64-encoded string? Any reliable way to infer it before decoding it?</p>

[{'answer_id': 52750, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 5, 'selected': True, 'text': '<p>Most image file formats have unique bytes at the start. The unix <code>file</code> command looks at the start of the file to see what type of data it contains. See the Wikipedia article on <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files" rel="noreferrer">Magic numbers in files</a> and <a href="http://magicdb.org/" rel="noreferrer">magicdb.org</a>.</p>\n'}, {'answer_id': 52833, 'author': 'verix', 'author_id': 5342, 'author_profile': 'https://Stackoverflow.com/users/5342', 'pm_score': 1, 'selected': False, 'text': '<p>Either <code>file</code> on the *nix command-line or reading the initial bytes of the file. Most files come with a unique header in the first few bytes. For example, TIFF\'s header looks something like this: <pre>0x00000000: 4949 2a00 0800 0000</pre>\nFor more information on the TIFF file format specifically if you\'d like to know what those bytes stand for, go <a href="http://www.awaresystems.be/imaging/tiff/faq.html" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 58566, 'author': 'hamishmcn', 'author_id': 3590, 'author_profile': 'https://Stackoverflow.com/users/3590', 'pm_score': 0, 'selected': False, 'text': '<p>TIFFs will begin with either II or MM (Intel byte ordering or Motorolla).<br>\nThe TIFF 6 specification can be downloaded <a href="http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf" rel="nofollow noreferrer">here</a> and isn\'t too hard to follow</p>\n'}, {'answer_id': 99665, 'author': 'LarryF', 'author_id': 18518, 'author_profile': 'https://Stackoverflow.com/users/18518', 'pm_score': 3, 'selected': False, 'text': "<p>Sure there is.  Like the others have mentioned, most images start with some sort of 'Magic', which will always translate to some sort of Base64 data.  The following are a couple examples:</p>\n\n<p>A Bitmap will start with <code>Qk3</code></p>\n\n<p>A Jpeg will start with <code>/9j/</code></p>\n\n<p>A GIF will start with <code>R0l</code> (That's a zero as the second char).</p>\n\n<p>And so on.  It's not hard to take the different image types and figure out what they encode to.  Just be careful, as some have more than one piece of magic, so you need to account for them in your B64 'translation code'.</p>\n"}]
['https://Stackoverflow.com/questions/52739', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2577/']