diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a57c63a4..8f487be3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ in order to get warned about deprecated features used in your code. This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`. ## [2.5.8] - not released yet +### Added +- support for monochromatic images (PIL image.mode == '1') ## [2.5.7] - 2022-09-08 ### Changed diff --git a/fpdf/image_parsing.py b/fpdf/image_parsing.py index b0fe7d76c..2d750bcc4 100644 --- a/fpdf/image_parsing.py +++ b/fpdf/image_parsing.py @@ -2,6 +2,7 @@ from io import BytesIO import base64 from urllib.request import urlopen +from math import ceil try: from PIL import Image @@ -70,12 +71,15 @@ def get_img_info(img, image_filter="AUTO", dims=None): if img.mode in ("P", "PA") and image_filter != "FlateDecode": img = img.convert("RGBA") - if img.mode not in ("L", "LA", "RGB", "RGBA", "P", "PA"): + if img.mode not in ("1", "L", "LA", "RGB", "RGBA", "P", "PA"): img = img.convert("RGBA") w, h = img.size info = {} - if img.mode == "L": + if img.mode == "1": + dpn, bpc, colspace = 1, 1, "DeviceGray" + info["data"] = _to_data(img, image_filter) + elif img.mode == "L": dpn, bpc, colspace = 1, 8, "DeviceGray" info["data"] = _to_data(img, image_filter) elif img.mode == "LA": @@ -171,8 +175,11 @@ def _to_zdata(img, remove_slice=None, select_slice=None): if select_slice: data = data[select_slice] # Left-padding every row with a single zero: - channels_count = len(data) // (img.size[0] * img.size[1]) - loop_incr = img.size[0] * channels_count + 1 + if img.mode == "1": + loop_incr = ceil(img.size[0] / 8) + 1 + 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" diff --git a/test/image/image_info.json b/test/image/image_info.json index f9fe4fdce..76d4957ca 100644 --- a/test/image/image_info.json +++ b/test/image/image_info.json @@ -2,10 +2,10 @@ "basi0g01.png": { "w": 32, "h": 32, - "cs": "DeviceRGB", - "bpc": 8, + "cs": "DeviceGray", + "bpc": 1, "f": "FlateDecode", - "dp": "/Predictor 15 /Colors 3 /BitsPerComponent 8 /Columns 32", + "dp": "/Predictor 15 /Colors 1 /BitsPerComponent 1 /Columns 32", "trns": "" }, "basi0g02.png": { @@ -145,10 +145,10 @@ "basn0g01.png": { "w": 32, "h": 32, - "cs": "DeviceRGB", - "bpc": 8, + "cs": "DeviceGray", + "bpc": 1, "f": "FlateDecode", - "dp": "/Predictor 15 /Colors 3 /BitsPerComponent 8 /Columns 32", + "dp": "/Predictor 15 /Colors 1 /BitsPerComponent 1 /Columns 32", "trns": "" }, "basn0g02.png": { @@ -1535,4 +1535,4 @@ "dp": "/Predictor 15 /Colors 3 /BitsPerComponent 8 /Columns 32", "trns": "" } -} \ No newline at end of file +} diff --git a/test/image/image_types/image_types_insert_png_monochromatic.pdf b/test/image/image_types/image_types_insert_png_monochromatic.pdf new file mode 100644 index 000000000..448a08f9a Binary files /dev/null and b/test/image/image_types/image_types_insert_png_monochromatic.pdf differ diff --git a/test/image/image_types/test_insert_images.py b/test/image/image_types/test_insert_images.py index b0afaeec1..47b5f9980 100644 --- a/test/image/image_types/test_insert_images.py +++ b/test/image/image_types/test_insert_images.py @@ -60,6 +60,13 @@ def test_insert_png(tmp_path): assert_pdf_equal(pdf, HERE / "image_types_insert_png.pdf", tmp_path) +def test_insert_png_monochromatic(tmp_path): + pdf = fpdf.FPDF() + pdf.add_page() + pdf.image(HERE / "../png_test_suite/basi0g01.png", x=15, y=15, h=140) + assert_pdf_equal(pdf, HERE / "image_types_insert_png_monochromatic.pdf", tmp_path) + + def test_insert_png_alpha(tmp_path): pdf = fpdf.FPDF() pdf.compress = False diff --git a/test/template/flextemplate_elements.pdf b/test/template/flextemplate_elements.pdf index a1121287c..60edf72c2 100644 Binary files a/test/template/flextemplate_elements.pdf and b/test/template/flextemplate_elements.pdf differ diff --git a/test/template/flextemplate_rotation.pdf b/test/template/flextemplate_rotation.pdf index b006f12b9..9dd56624b 100644 Binary files a/test/template/flextemplate_rotation.pdf and b/test/template/flextemplate_rotation.pdf differ diff --git a/test/template/template_qrcode.pdf b/test/template/template_qrcode.pdf index f7d951a0e..2aab5e366 100644 Binary files a/test/template/template_qrcode.pdf and b/test/template/template_qrcode.pdf differ