Skip to content

Commit

Permalink
FPDF.image(), when provided a BytesIO instance, does not close it any…
Browse files Browse the repository at this point in the history
…more - fix #881
  • Loading branch information
Lucas-C committed Aug 13, 2023
1 parent 978db12 commit b366d09
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
## [2.7.6] - Not released yet
### Added
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html) now supports heading colors defined as attributes (_e.g._ `<h2 color="#00ff00">...`)
### Fixed
* [`FPDF.image()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image), when provided a `BytesIO` instance, does not close it anymore - _cf._ issue [#881](https://github.com/py-pdf/fpdf2/issues/881)

## [2.7.5] - 2023-08-04
### Added
Expand Down
13 changes: 8 additions & 5 deletions fpdf/image_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

RESAMPLE = Resampling.LANCZOS
except ImportError: # For Pillow < 9.1.0
# pylint: disable=no-member
# pylint: disable=no-member, useless-suppression
RESAMPLE = Image.ANTIALIAS
except ImportError:
Image = None
Expand Down Expand Up @@ -116,16 +116,16 @@ def get_img_info(filename, img=None, image_filter="AUTO", dims=None):
raise EnvironmentError("Pillow not available - fpdf2 cannot insert images")

is_pil_img = True
keep_bytes_io_open = False
jpeg_inverted = False # flag to check whether a cmyk image is jpeg or not, if set to True the decode array is inverted in output.py
img_raw_data = None
if not img or isinstance(img, (Path, str)):
img_raw_data = load_image(filename)
img = Image.open(img_raw_data)
is_pil_img = False
elif not isinstance(img, Image.Image):
if isinstance(img, bytes):
img = BytesIO(img)
img_raw_data = img
keep_bytes_io_open = isinstance(img, BytesIO)
img_raw_data = BytesIO(img) if isinstance(img, bytes) else img
img = Image.open(img_raw_data)
is_pil_img = False

Expand Down Expand Up @@ -293,7 +293,10 @@ def get_img_info(filename, img=None, image_filter="AUTO", dims=None):
dp = f"/BlackIs1 true /Columns {w} /K -1 /Rows {h}"

if not is_pil_img:
img.close()
if keep_bytes_io_open:
img.fp = None # cf. issue #881
else:
img.close()

info.update(
{
Expand Down
1 change: 1 addition & 0 deletions test/image/image_types/test_insert_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def test_insert_bytesio(tmp_path):
img.save(img_bytes, "PNG")
pdf.image(img_bytes, x=15, y=15, h=140)
assert_pdf_equal(pdf, HERE / "image_types_insert_png.pdf", tmp_path)
assert not img_bytes.closed # cf. issue #881


def test_insert_bytes(tmp_path):
Expand Down

0 comments on commit b366d09

Please sign in to comment.