Skip to content

Commit

Permalink
Fixed "fake duplicates" bug when a Pillow Image was passed to FPDF.im…
Browse files Browse the repository at this point in the history
…age - close #139
  • Loading branch information
Lucas-C committed Apr 30, 2021
1 parent ab3eb3d commit ec279cd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/),
and [PEP 440](https://www.python.org/dev/peps/pep-0440/).

## [2.3.4] - 2021-04-30
### Fixed
- a "fake duplicates" bug when a `Pillow.Image.Image` was passed to `FPDF.image`

## [2.3.3] - 2021-04-21
### Added
- new features: **document outline & table of contents**! Check out the new dedicated [documentation page](https://pyfpdf.github.io/fpdf2/DocumentOutlineAndTableOfContents.html) for more information
Expand Down
6 changes: 5 additions & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from pathlib import Path
from typing import Callable, NamedTuple, Optional, Union

from PIL import Image

from .errors import FPDFException, FPDFPageFormatException
from .fonts import fpdf_charwidths
from .image_parsing import get_img_info, load_resource
Expand All @@ -59,7 +61,7 @@
HERE = Path(__file__).resolve().parent

# Global variables
FPDF_VERSION = "2.3.3"
FPDF_VERSION = "2.3.4"
FPDF_FONT_DIR = HERE / "font"
SYSTEM_TTFONTS = None

Expand Down Expand Up @@ -1720,6 +1722,8 @@ def image(
)
if isinstance(name, str):
img = None
elif isinstance(name, Image.Image):
name, img = hashlib.md5(name.tobytes()).hexdigest(), name
elif isinstance(name, io.BytesIO):
name, img = hashlib.md5(name.getvalue()).hexdigest(), name
else:
Expand Down
Binary file not shown.
14 changes: 13 additions & 1 deletion test/image/image_types/test_insert_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from pathlib import Path

from PIL import Image
from PIL import Image, ImageDraw, ImageFont

import fpdf
from test.conftest import assert_pdf_equal
Expand Down Expand Up @@ -60,6 +60,18 @@ def test_insert_pillow(tmp_path):
assert_pdf_equal(pdf, HERE / "image_types_insert_png.pdf", tmp_path)


def test_insert_pillow_issue_139(tmp_path):
pdf = fpdf.FPDF()
pdf.add_page()
font = ImageFont.truetype("arial.ttf", 40)
for y in range(5):
for x in range(4):
img = Image.new(mode="RGB", size=(100, 100), color=(60, 255, 10))
ImageDraw.Draw(img).text((20, 20), f"{y}{x}", fill="black", font=font)
pdf.image(img, x=x * 50 + 5, y=y * 50 + 5, w=45)
assert_pdf_equal(pdf, HERE / "insert_pillow_issue_139.pdf", tmp_path)


def test_insert_bytesio(tmp_path):
pdf = fpdf.FPDF()
pdf.add_page()
Expand Down

0 comments on commit ec279cd

Please sign in to comment.