import time import PIL import os import os.path from PIL import Image from PIL.Image import Resampling import threading Image.LOAD_TRUNCATED_IMAGES = True f = r'/path to gallery' basewidth = 2500 def folderssss(item): global running running += 1 print("start thread "+ str(running)) for file in os.listdir(f+"/"+item): if file.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')): if file == '.DS_Store': continue if file == 'Thumbs.db': continue f_img = f+"/"+item+"/"+file velikost_p = os.path.getsize(f_img) img = Image.open(f_img) wpercent = (basewidth / float(img.size[0])) hsize = int((float(img.size[1]) * float(wpercent))) if img.size[0] > basewidth: try: img = img.resize((basewidth, hsize), Resampling.LANCZOS) img.save(f_img) velikost_z = os.path.getsize(f_img) print(f_img + " - " + str(velikost_p) + " - " + str(velikost_z) + " = " + str(int((velikost_p-velikost_z)/1000000))+" MB") except IOError: print("chyba img "+ f_img) running -= 1 #img = img.resize((2296,1724)) running -= 1 print("end thread " + str(running)) running = 0 dirs = os.listdir( f ) for item in dirs: if item == '.DS_Store': continue example_thread = threading.Thread(target=folderssss, args=(item,)) example_thread.start() while running > 16: time.sleep(1)