0

I'm struggling to find a way to reduce an image file's size before uploading it to Google Cloud. I have tried encoding it with Base64, but without success.

The idea is to save storage space. The file is 3 MB and should be smaller. This is the code I currently have (without compressing):

def upload_to_bucket(blob_name, file_path, bucket_name):

    try:
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(bucket_name)
        blob = bucket.blob(blob_name)
        blob.upload_from_filename(file_path)

        print("successfully uploaded {} to {}".format(file_path, bucket_name))
        return True
    except Exception as e:
      print(e)
      return False

upload_to_bucket("QR2", 'QRcode_Scanner\whitecards\WC_QR_Scan.jpg', 'test_storage_whitecards')

If you need any additional information, please ask :)

Kwinten
  • 71
  • 7

1 Answers1

4

JPEG has compression built in. One side effect of this is that further compression with lossless algorithms (zip etc) is generally not possible.

You're best bet is to use the built-in compression JPEG has more aggressively. You can do this in python using Pillow to read the file and write it again with higher compression before uploading. This will degrade quality, but that is part of the quality/filesize trade-off.

Also FYI base64 is not compression, in fact it increases file sizes. Its goal is to allow sending binary files in ascii-only channels. GCS supports binary objects, so you don't need it.

David
  • 9,288
  • 1
  • 20
  • 52
  • png is lossless afaik. jpeg is not – jkr Nov 01 '21 at 21:05
  • thanks @jakub I have edited to focus on JPEG which is what was in the question – David Nov 01 '21 at 21:06
  • Thank you! I'll give this a go! However, does this mean I'll have to save a new version locally of the given file? If so, can I do this temporarily? (File read > Compress file > temp save file > send file to Cloud storage > delete temp file locally). – Kwinten Nov 02 '21 at 08:05
  • You can [re-compress it in memory](https://stackoverflow.com/a/30771751/3645370) and then use `blob.upload_from_string` to upload it. The downside to this is it requires storing the whole image in RAM. That's no problem if it's just 3MB, but if you are using a low-memory environment like a cloud function it and you might be getting very image files it could be a problem. – David Nov 03 '21 at 14:58