I made a small Python 3.x app for myself that resizes all the images from a folder by a certain given percentage.
The app suports multicore CPU, as in it splits the work done on as many threads as the CPU has.
The bottleneck here is the CPU, as my RAM memory remains 40% free and my HDD usage is 3% during runtime, but all CPU cores are near 100%.
Is there a way to process the images on the GPU? I think it would greatly improve performance as GPU have more than 4 cores.
Here is a bit of code on how the processing is done:
def worker1(file_list, percentage, thread_no):
"""thread class"""
global counter
save_dir = askdir_entry.get() + '/ResizeImage/'
for picture in file_list:
image = Image.open(picture, mode='r')
image_copy = image.copy()
(width, height) = image.size
filename = os.path.split(picture)[1]
image_copy.thumbnail((width * (int(percentage) / 100), height * (int(percentage) / 100)))
info_area.insert('end', '\n' + filename)
info_area.see(tkinter.END)
image_copy.save(save_dir + filename)
counter += 1
if counter % 3 == 0:
update_counter(1, thread_no)
update_counter(0, thread_no)
def resize():
global start_time
start_time = timeit.default_timer()
percentage = percentage_textbox.get()
if not percentage:
info_area.insert('end', 'Please write a percentage!')
return
askdir_entry.config(state='disabled')
percentage_textbox.config(state='disabled')
file_list = glob.glob(askdir_entry.get() + '/*.jp*g')
info_area.insert('end', 'Found ' + str(len(file_list)) + ' pictures.\n')
cpu = multiprocessing.cpu_count()
info_area.insert('end', 'Number of threads: ' + str(cpu))
info_area.insert('end', '\nResizing pictures..\n\n')
if not os.path.exists(askdir_entry.get() + '/ResizeImage'):
os.makedirs(askdir_entry.get() + '/ResizeImage')
counter_label.config(text='-')
for i in range(0, cpu):
file_list_chunk = file_list[int(i * len(file_list) / cpu):int((i + 1) * len(file_list) / cpu)]
threading.Thread(target=worker1, args=(file_list_chunk, percentage, i + 1)).start()