In Python, given a directory of full-size images, how can I generate thumbnails using more than one CPU core?

StackOverflow https://stackoverflow.com/questions/4066606

Question

I have a 16-core machine but my current resizing function only uses one core, which is really inefficient for a large directory of images.

def generateThumbnail(self, width, height):
     """
     Generates thumbnails for an image
     """
     im = Image.open(self._file)
     (detected_width,detected_height) = im.size

     #Get cropped box area
     bbox = self.getCropArea(detected_width, detected_height, width, height)

     #Crop to box area
     cropped_image = im.crop(bbox)

     #Resize to thumbnail
     cropped_image.thumbnail((width, height), Image.ANTIALIAS)

     #Save image
     cropped_image.save(self._path + str(width) + 'x' +
             str(height) + '-' + self._filename, "JPEG")

Any help would be greatly appreciated. Thank you.

Was it helpful?

Solution

This sounds like a good solution for the multiprocessing module, which uses the threading interface, but creates separate processes instead of threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top