title: How to put an image resized with Pillow into GridFS?
body: <p><strong>Scenario</strong></p>
  
  <p>I use a formData form to upload an image via ajax which is then added to a MongoDB GridFS database.  </p>
  
  <p>This was working:</p>
  
  <pre><code>my_image = request.files.my_image
  raw = my_image.file.read()
  fs.put(raw)
  </code></pre>
  
  <p><strong>Desired Behaviour</strong></p>
  
  <p>I want to resize the image with <a href="http://pillow.readthedocs.org/en/latest/index.html" rel="nofollow">Pillow</a> before adding to GridFS.  </p>
  
  <p><strong>What I Tried</strong></p>
  
  <p>I changed the above to:</p>
  
  <pre><code>my_image = request.files.my_image
  raw = Image.open(my_image.file.read())
  raw_resized = raw.resize((new_dimensions))
  fs.put(raw_resized)
  </code></pre>
  
  <p><strong>Actual Behaviour</strong></p>
  
  <p>I am now getting 500 errors. Tail shows:</p>
  
  <pre><code>TypeError: file() argument 1 must be encoded string without NULL bytes, not str
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>How do I properly handle the Pillow image object so that I can add it to GridFS?  </p>
  
  <p><strong>Troubelshooting</strong></p>
  
  <p>This is still unresolved, but I'm just adding my attempts to understand what is happening with file types etc at different stages of the process by using the interpretor:</p>
  
  <pre><code>&gt;&gt;&gt; my_image = open("my_great_image.jpg")
  &gt;&gt;&gt; my_image
  &lt;open file 'my_great_image.jpg', mode 'r' at 0x0259CF40&gt;
  &gt;&gt;&gt; type(my_image)
  &lt;type 'file'&gt;
  &gt;&gt;&gt; my_image_read = my_image.read()
  &gt;&gt;&gt; my_image_read
  # lots of image data
  &gt;&gt;&gt; type(my_image_read)
  &lt;type 'str'&gt;
  &gt;&gt;&gt; my_pil_image = Image.open("my_great_image.jpg")
  &gt;&gt;&gt; my_pil_image
  &lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x267 at 0x2760CD8&gt;
  &gt;&gt;&gt; type(my_pil_image)
  &lt;type 'instance'&gt;
  </code></pre>
  
  <p>So from this I think I can deduce that, originally, GridFS was accepting of the string version of the image generated from the <code>read()</code> method.  </p>
  
  <p>So I think I need to somehow make the Pillow image object a string in order to get it into GridFS.  </p>