Retry image downloads
This commit is contained in:
parent
1e84875a8f
commit
50b1f66c59
@ -99,7 +99,12 @@ def genImage(imageArray):
|
|||||||
return finalImg
|
return finalImg
|
||||||
|
|
||||||
def downloadImage(url):
|
def downloadImage(url):
|
||||||
return Image.open(BytesIO(get(url).content))
|
for i in range(3):
|
||||||
|
try:
|
||||||
|
return Image.open(BytesIO(get(url).content))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
def genImageFromURL(urlArray):
|
def genImageFromURL(urlArray):
|
||||||
# this method avoids storing the images in disk, instead they're stored in memory
|
# this method avoids storing the images in disk, instead they're stored in memory
|
||||||
@ -110,6 +115,8 @@ def genImageFromURL(urlArray):
|
|||||||
imageArray = [executor.submit(downloadImage, url) for url in urlArray]
|
imageArray = [executor.submit(downloadImage, url) for url in urlArray]
|
||||||
imageArray = [future.result() for future in imageArray]
|
imageArray = [future.result() for future in imageArray]
|
||||||
print(f"Images downloaded in: {timer() - start}s")
|
print(f"Images downloaded in: {timer() - start}s")
|
||||||
|
if (None in imageArray): # return none if any of the images failed to download
|
||||||
|
return None
|
||||||
start = timer()
|
start = timer()
|
||||||
finalImg = genImage(imageArray)
|
finalImg = genImage(imageArray)
|
||||||
print(f"Image generated in: {timer() - start}s")
|
print(f"Image generated in: {timer() - start}s")
|
||||||
@ -126,6 +133,8 @@ def lambda_handler(event, context):
|
|||||||
if not img.startswith("https://pbs.twimg.com"):
|
if not img.startswith("https://pbs.twimg.com"):
|
||||||
return {'statusCode':400,'body':'Invalid image URL'}
|
return {'statusCode':400,'body':'Invalid image URL'}
|
||||||
combined = genImageFromURL(images)
|
combined = genImageFromURL(images)
|
||||||
|
if (combined == None):
|
||||||
|
return {'statusCode':400,'body':'Failed to download image(s)'}
|
||||||
buffered = BytesIO()
|
buffered = BytesIO()
|
||||||
combined.save(buffered,format="JPEG",quality=60)
|
combined.save(buffered,format="JPEG",quality=60)
|
||||||
combined_str=base64.b64encode(buffered.getvalue()).decode('ascii')
|
combined_str=base64.b64encode(buffered.getvalue()).decode('ascii')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user