-1

I'm resizing my image for my thesis on Google colab. But there's a huge time difference when I'm using tqdm and when I'm not using it.

Here's the code when I'm using tqdm:

import glob
import os
from tqdm import tqdm
import math
from PIL import Image

new_width = 224
file = r'path\\*.jpeg'
images = glob.glob(file)
for i in tqdm(range(len(images))):
  for image in images:
    img = Image.open(image)
    width,height = img.size
    if width >= new_width:
      new = img.resize((224,224))
    else:
      new = img
    
    new.save('path'+os.path.basename(image))

And here's when I'm not using tqdm:

import glob 
import os
import math 
from PIL import Image

new_width = 224
file = r'path\\*.jpeg'
images = glob.glob(file)
for image in images:
  img = Image.open(image)
  width,height = img.size
  if width >= new_width:
    new = img.resize((224,224))
  else:
    new = img
    
  new.save('path\\'+os.path.basename(image))

The result with tqdm takes more than 1 hour but when I'm not using it it only take like 10 seconds. What is wrong with my code?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

In the first code you have two nested loops instead of a single loop, therefore each image is processed N times instead of only once, where N is the number of images. The total runtime is proportional to N2 instead of N.

You are supposed to just wrap the existing loop with tdqm(), not add another loop:

images = glob.glob(file)
for image in tdqm(images):
    img = Image.open(image)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65