Files referenced in the article:
Bulk Image Compression

If you're familiar with our platform then you may or may not know about the 3.6MB file limitation that we have imposed for uploads, which is only for singular uploads and not a batch of them. Even if you are only uploading one image at a time, a 3.6MB file is still going to take a few seconds to download and process, and that isn't very fun for anyone.

Compressing your files (without compromising resolution) has a lot of advantages, including:

  • Much better latency
  • Faster visual search, if applicable
  • Higher rates of success in batching, meaning that a full batch of 32 images is more likely to 100% succeed if all of its images are in the KB range

If a lot of your images are in the MB range then you'll definitely want to use something like the script below to compress and essentially re-size these images in bulk. This assumes that the files are in a local directory on your computer and it will resize and save them with the exact same names there.

Note that there are some commented lines by the "im_resized" variable that enable you to either append or overwrite the files in this folder. The default is to do an overwrite, but the choice is yours.

Requirements: os, sys, argparse, PIL modules
Optional: tqdm module

from PIL import Image
import os, sys
import argparse
from tqdm import tqdm # optional


def resize_image(path, max_size):
  if path[-1] != '/':
    path = path + '/'

  dirs = os.listdir(path)
  for item in tqdm(dirs):
    if os.path.isfile(path+item):
      try:
        im = Image.open(path+item)
        if im.height > max_size or im.width > max_size:
          f,e = os.path.splitext(path+item)

          # if width > height:
          if im.width > im.height:
            desired_width = max_size
            desired_height = im.height / (im.width/max_size)
       
          # if height > width:
          elif im.height > im.width:
            desired_height = max_size
            desired_width = im.width / (im.height/max_size)
       
          else:
            desired_height = max_size
            desired_width = max_size
       
          # convert back to integer
          desired_height = int(desired_height)
          desired_width = int(desired_width)
       
          im_resized = im.resize((desired_width, desired_height))
          # im_resized.save(f + '_resized.jpg', 'JPEG', quality=90) # doesn't overwrite file
          im_resized.save(f + e, 'JPEG', quality=80) # does overwrite file
          # im_resized.save('resized_' + f + '.jpg', 'JPEG', quality=100)

      except:
        continue


def main():
  parser = argparse.ArgumentParser(
    description='given a folder path, resize all images inside')
  parser.add_argument(
    '--path',
    type=str,
    help='full path to the target folder')
  parser.add_argument(
    '--max_size',
    type=float,
    help='the largest size that you want an image to be (in pixels)')

  args = parser.parse_args()

  resize_image(args.path, args.max_size)


if __name__ == '__main__':
  main()

Here's how to call the file:

python bulk_resize_images_in_folder.py --path='/your/full/image/directory' --max_size='512'

The parameters here:

  • path is the full path of your image folder
  • max_size is the maximum size that you want your images to be, in pixels. This example has 512 but you can tweak this to be a higher number if desired.
Did this answer your question?