-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathprocesspool_thumbnails.py
53 lines (39 loc) · 1.55 KB
/
processpool_thumbnails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
####
# This sample is published as part of the blog article at www.toptal.com/blog
# Visit www.toptal.com/blog and subscribe to our newsletter to read great posts
####
import logging
from pathlib import Path
from time import time
from functools import partial
from concurrent.futures import ProcessPoolExecutor
from PIL import Image
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def create_thumbnail(size, path):
"""
Creates a thumbnail of an image with the same name as image but with _thumbnail appended before the
suffix.
>>> create_thumbnail((128, 128), 'image.jpg')
A new thumbnail image is created with the name image_thumbnail.jpg
:param size: A tuple of the width and height of the image
:param path: The path to the image file
:return: None
"""
path = Path(path)
name = path.stem + '_thumbnail' + path.suffix
thumbnail_path = path.with_name(name)
image = Image.open(path)
image.thumbnail(size)
image.save(thumbnail_path)
def main():
ts = time()
# Partially apply the create_thumbnail method setting the size to 128x128 and returning a function of a single
# argument
thumbnail_128 = partial(create_thumbnail, (128, 128))
# Create the executor in a with block so shut down is called when the block is exited
with ProcessPoolExecutor() as executor:
executor.map(thumbnail_128, Path('images').iterdir())
logging.info('Took %s', time() - ts)
if __name__ == '__main__':
main()