Skip to content

Commit

Permalink
Fix performance of countPages in thumbnail lambda (#2616)
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-sigurd authored Jan 18, 2022
1 parent c6e6d64 commit e8ff056
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
## Catalog, Lambdas
* [Changed] pkgselect: make directly invocable, always use execution role ([#2560](https://github.com/quiltdata/quilt/pull/2560))
* [Changed] Move package listing / querying to GraphQL ([#2552](https://github.com/quiltdata/quilt/pull/2552))
* [Changed] Fix performance of `countPages` in thumbnail lambda ([#2616](https://github.com/quiltdata/quilt/pull/2616)).
* [Fixed] Improve upload performance and stability, fix some hashing-related errors ([#2532](https://github.com/quiltdata/quilt/pull/2532))
* [Added] Echarts renderer ([#2382](https://github.com/quiltdata/quilt/pull/2382))
* [Added] Set height for `quilt_summarize.json` files ([#2474](https://github.com/quiltdata/quilt/pull/2474))
Expand Down
22 changes: 7 additions & 15 deletions lambdas/thumbnail/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import imageio
import numpy as np
import pdf2image
import requests
from aicsimageio import AICSImage, readers
from pdf2image import convert_from_bytes
Expand Down Expand Up @@ -276,24 +277,15 @@ def lambda_handler(request):
if input_ == "pdf":
set_pdf_env()
try:
kwargs = {
# respect width but not necessarily height to preserve aspect ratio
"size": (size[0], None),
"fmt": "JPEG",
}
if not count_pages:
kwargs["first_page"] = page
kwargs["last_page"] = page

pages = convert_from_bytes(
resp.content,
**kwargs,
# respect width but not necessarily height to preserve aspect ratio
size=(size[0], None),
fmt="JPEG",
first_page=page,
last_page=page,
)
num_pages = len(pages)
preview = pages[0]
if count_pages:
# shift 1-index to 0-index
preview = pages[page - 1]
except (
IndexError,
PDFInfoNotInstalledError,
Expand All @@ -308,7 +300,7 @@ def lambda_handler(request):
'thumbnail_size': preview.size,
}
if count_pages:
info['page_count'] = num_pages
info['page_count'] = pdf2image.pdfinfo_from_bytes(resp.content)["Pages"]

thumbnail_bytes = BytesIO()
preview.save(thumbnail_bytes, thumbnail_format)
Expand Down

0 comments on commit e8ff056

Please sign in to comment.