title: Python App Engine uploaded image content-type
body: <p>I know that I can accept image uploads by having a form that POSTs to App Engine like so:</p>
  
  <pre><code>&lt;form action="/upload_received" enctype="multipart/form-data" method="post"&gt;
  &lt;div&gt;&lt;input type="file" name="img"/&gt;&lt;/div&gt;
  &lt;div&gt;&lt;input type="submit" value="Upload Image"&gt;&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Then in the Python code I can do something like</p>
  
  <pre><code>image = self.request.get("img")
  </code></pre>
  
  <p>But how can I figure out what the content-type of this image should be when later showing it to the user? It seems the most robust way would be to figure this out from the image data itself, but how to get that easily? I did not see anything suitable in the google.appengine.api images package.</p>
  
  <p>Should I just look for the magic image headers in my own code, or is there already a method for that somewhere?</p>
  
  <p>Edit:</p>
  
  <p>Here's the simplistic solution I ended up using, seems to work well enough for my purposes and avoids having to store the image type as a separate field in the data store:</p>
  
  <pre><code># Given an image, returns the mime type or None if could not detect.
  def detect_mime_from_image_data(self, image):
      if image[1:4] == 'PNG': return 'image/png'
      if image[0:3] == 'GIF': return 'image/gif'
      if image[6:10] == 'JFIF': return 'image/jpeg'
      return None
  </code></pre>