|
1 | | -from pickletools import optimize |
2 | | -from turtle import down |
3 | | -from weakref import finalize |
4 | 1 | from PIL import Image, ImageOps, ImageFilter |
5 | 2 | from requests import get |
6 | 3 | from io import BytesIO |
7 | 4 | import base64 |
8 | | -from multiprocessing.pool import ThreadPool |
| 5 | +import concurrent.futures |
9 | 6 | from time import time as timer |
10 | 7 |
|
11 | 8 | # find the highest res image in an array of images |
@@ -42,7 +39,9 @@ def scaleImageIterable(args): |
42 | 39 |
|
43 | 40 | def scaleAllImagesToSameSize(imageArray,targetWidth,targetHeight,pad=True): # scale all images in the array to the same size, preserving aspect ratio |
44 | 41 | newImageArray = [] |
45 | | - newImageArray=[scaleImageIterable([image,targetWidth,targetHeight,pad]) for image in imageArray] |
| 42 | + with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: |
| 43 | + newImageArray = [executor.submit(scaleImageIterable, (image, targetWidth, targetHeight,pad)) for image in imageArray] |
| 44 | + newImageArray = [future.result() for future in newImageArray] |
46 | 45 | return newImageArray |
47 | 46 |
|
48 | 47 | def blurImage(image, radius): |
@@ -107,15 +106,16 @@ def genImageFromURL(urlArray): |
107 | 106 | # no cache means that they'll have to be downloaded again if the image is requested again |
108 | 107 | # TODO: cache? |
109 | 108 | start = timer() |
110 | | - imageArray = ThreadPool(8).map(downloadImage,urlArray) |
| 109 | + with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: |
| 110 | + imageArray = [executor.submit(downloadImage, url) for url in urlArray] |
| 111 | + imageArray = [future.result() for future in imageArray] |
111 | 112 | print(f"Images downloaded in: {timer() - start}s") |
112 | 113 | start = timer() |
113 | 114 | finalImg = genImage(imageArray) |
114 | 115 | print(f"Image generated in: {timer() - start}s") |
115 | 116 | return finalImg |
116 | 117 |
|
117 | 118 | def lambda_handler(event, context): |
118 | | - # TODO implement |
119 | 119 | images = event["queryStringParameters"].get("imgs","").split(",") |
120 | 120 | combined = genImageFromURL(images) |
121 | 121 | buffered = BytesIO() |
|
0 commit comments