title: Resize images before uploading to google cloud storage django
body: <p>I am trying to resize and compress images that are uploaded from my django app before it is uploaded to google cloud storage. I tried using firebase to resize any image that is uploaded automatically, but it gives giving resize error. I would like my django app to do this compression first before it is uploaded to google cloud. The standard pillow compression doesn't work on cloud storages. How can accomplish this?</p>
  
  <p>Take the view function  below, how would i make resize the image before upload?</p>
  
  <pre><code>if request.method == 'POST':
      form = GalleryForm(request.POST, request.FILES)
      if form.is_valid():
          image = form.save(commit=False)
          image.user = usr
          image.save()
          return HttpResponseRedirect(reverse('profile-detail', args=(), kwargs={'slug': slug}))
  </code></pre>
  
  <p>Right now, my app would upload the image (Whatever Size) to Google Cloud, then open the image, resize it, and overwrite the originally uploaded image. I feel this might put strain on my app processes eventually. I would like my app to just resize the image before uploading. Below is my code I use to resize images after uploading.</p>
  
  <pre><code>def save(self, *args, **kwargs):
      super().save(*args, **kwargs)
  
      img_read = storage.open(self.image.name, 'r')
      img = Image.open(img_read)
  
      if img.height &gt; 801 or img.width &gt; 534:
          output_size = (801, 534)
          img.thumbnail(output_size)
          in_mem_file = io.BytesIO()
          img.save(in_mem_file, format='JPEG')
          img_write = storage.open(self.image.name, 'w+')
          img_write.write(in_mem_file.getvalue())
          img_write.close()
  
      img_read.close() 
  </code></pre>
  
  <p>Another draw back to this would be that, the image has to be in 'JPEG' format for this to work. I am looking for a better way to get this done.</p>