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')}"