From 0bcad08a91b575a0c8ec62fa26047e2fbf165a3a Mon Sep 17 00:00:00 2001 From: vincent d warmerdam Date: Thu, 5 Oct 2023 12:30:31 +0200 Subject: [PATCH] use a context manager --- prodigy_pdf/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prodigy_pdf/__init__.py b/prodigy_pdf/__init__.py index 5391d9f..9ff4b38 100644 --- a/prodigy_pdf/__init__.py +++ b/prodigy_pdf/__init__.py @@ -11,9 +11,9 @@ def page_to_image(page: pdfium.PdfPage) -> str: pil_image = page.render().to_pil() - buffered = BytesIO() - pil_image.save(buffered, format="JPEG") - img_str = base64.b64encode(buffered.getvalue()) + with BytesIO() as buffered: + pil_image.save(buffered, format="JPEG") + img_str = base64.b64encode(buffered.getvalue()) return f"data:image/png;base64,{img_str.decode('utf-8')}"