title: Accessing data using Pillow corrupts it on response in Django Rest Framework (The file you uploaded was either not an image or a corrupted image.)
body: <p>I want to extract the <strong>format</strong> and <strong>mode</strong> parameter from the uploaded image within the Serializer and dynamically update my fields. Here is the code ...</p>
  
  <pre><code>class ImageDataSerializer(serializers.ModelSerializer):
  class Meta:
      model = models.ImageData
      exclude = ['height','width']
  </code></pre>
  
  <p>And in my view</p>
  
  <pre><code>    serializer = serializer(data=request.data,partial=True)
      serializer.is_valid(raise_exception=True)
      obj = serializer.save(user=request.user,extension="PNG",image_type="RGB")     
      return Response(serializer.data) 
  </code></pre>
  
  <p>This works perfectly. I am sending my <strong>InMemeoryUploadedFile</strong> instance as my data and the serializer does its job saving it to the database. However, I would like to determine the extension and image_type automatically using the Pillow library.
  This is what I have tried so far ... </p>
  
  <pre><code>class ImageDataSerializer(serializers.ModelSerializer):
  def __init__(self,*args,**kwargs):
      super(ImageDataSerializer,self).__init__(*args,**kwargs)
      myimage = self.initial_data['data']
      with Image.open(myimage) as myimage:
          self.fields['extension'].initial = myimage.format
          self.fields['image_type'].initial = myimage.mode
      # Update the extension and image_type initial values
  class Meta:
      model = models.ImageData
      exclude = ['height','width']
  </code></pre>
  
  <p>What happens is my image file get corrupted and in the response I am getting the message <strong>"Upload a valid image. The file you uploaded was either not an image or a corrupted image."</strong>
  I have also tried determining the extension and mode within the view and pass it to request.data dictionary but accessing the image file once using Pillow.Image.open() is later corrupting it.  </p>