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

문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top