Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FPDF.image(), when provided a BytesIO instance, does not close it any more - fix #881 #887

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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