Skip to content

Commit

Permalink
Fix performance issue with adding large images with FlateDecode ima…
Browse files Browse the repository at this point in the history
…ge filter (#644)

Co-authored-by: Vladimir Markov <[email protected]>
Fixes #643
  • Loading branch information
Markovvn1 authored Dec 25, 2022
1 parent 22f5e1f commit a961d74
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* `write_html()` now generates warnings for unclosed HTML tags, unless `warn_on_tags_not_matching=False` is set
### Fixed
* a `ValueError: Incoherent hierarchy` could be raised when using `write_html()` with some headings hierarchy
* performance issue with adding large images with `FlateDecode` image filter

## [2.6.0] - 2022-11-20
### Added
Expand Down
16 changes: 9 additions & 7 deletions fpdf/image_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ def _to_zdata(img, remove_slice=None, select_slice=None):
data = data[select_slice]
# Left-padding every row with a single zero:
if img.mode == "1":
loop_incr = ceil(img.size[0] / 8) + 1
row_size = ceil(img.size[0] / 8)
else:
channels_count = len(data) // (img.size[0] * img.size[1])
loop_incr = img.size[0] * channels_count + 1
i = 0
while i < len(data):
data[i:i] = b"\0"
i += loop_incr
return zlib.compress(data)
row_size = img.size[0] * channels_count

data_with_padding = bytearray()
for i in range(0, len(data), row_size):
data_with_padding.extend(b"\0")
data_with_padding.extend(data[i : i + row_size])

return zlib.compress(data_with_padding)


def _has_alpha(img, alpha_channel):
Expand Down

0 comments on commit a961d74

Please sign in to comment.