diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b4c641b5..cecf3f7f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/fpdf/fpdf.py b/fpdf/fpdf.py index 56b87b375..f0fdc6567 100644 --- a/fpdf/fpdf.py +++ b/fpdf/fpdf.py @@ -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 @@ -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 @@ -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: diff --git a/test/image/image_types/insert_pillow_issue_139.pdf b/test/image/image_types/insert_pillow_issue_139.pdf new file mode 100644 index 000000000..ba3df0cba Binary files /dev/null and b/test/image/image_types/insert_pillow_issue_139.pdf differ diff --git a/test/image/image_types/test_insert_images.py b/test/image/image_types/test_insert_images.py index b9a47e992..54872c26e 100644 --- a/test/image/image_types/test_insert_images.py +++ b/test/image/image_types/test_insert_images.py @@ -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 @@ -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()