Skip to content

Commit

Permalink
Merge pull request #6925 from radarhere/pdf
Browse files Browse the repository at this point in the history
Added saving RGBA images as PDFs
  • Loading branch information
mergify[bot] committed Mar 12, 2023
2 parents 5b3d39c + da2083f commit d0f3167
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from PIL import Image, PdfParser, features

from .helper import hopper, mark_if_feature_version
from .helper import hopper, mark_if_feature_version, skip_unless_feature


def helper_save_as_pdf(tmp_path, mode, **kwargs):
Expand Down Expand Up @@ -42,6 +42,11 @@ def test_save(tmp_path, mode):
helper_save_as_pdf(tmp_path, mode)


@skip_unless_feature("jpg_2000")
def test_save_rgba(tmp_path):
helper_save_as_pdf(tmp_path, "RGBA")


def test_monochrome(tmp_path):
# Arrange
mode = "1"
Expand Down
9 changes: 7 additions & 2 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1457,8 +1457,13 @@ PDF
^^^

Pillow can write PDF (Acrobat) images. Such images are written as binary PDF 1.4
files, using either JPEG or HEX encoding depending on the image mode (and
whether JPEG support is available or not).
files. Different encoding methods are used, depending on the image mode.

* 1 mode images are saved using TIFF encoding, or JPEG encoding if libtiff support is
unavailable
* L, RGB and CMYK mode images use JPEG encoding
* P mode images use HEX encoding
* RGBA mode images use JPEG2000 encoding

.. _pdf-saving:

Expand Down
6 changes: 3 additions & 3 deletions docs/releasenotes/9.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TODO
Other Changes
=============

TODO
^^^^
Added support for saving PDFs in RGBA mode
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

TODO
Using the JPXDecode filter, PDFs can now be saved in RGBA mode.
6 changes: 6 additions & 0 deletions src/PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def _save(im, fp, filename, save_all=False):
filter = "DCTDecode"
colorspace = PdfParser.PdfName("DeviceRGB")
procset = "ImageC" # color images
elif im.mode == "RGBA":
filter = "JPXDecode"
colorspace = PdfParser.PdfName("DeviceRGB")
procset = "ImageC" # color images
elif im.mode == "CMYK":
filter = "DCTDecode"
colorspace = PdfParser.PdfName("DeviceCMYK")
Expand All @@ -199,6 +203,8 @@ def _save(im, fp, filename, save_all=False):
)
elif filter == "DCTDecode":
Image.SAVE["JPEG"](im, op, filename)
elif filter == "JPXDecode":
Image.SAVE["JPEG2000"](im, op, filename)
elif filter == "FlateDecode":
ImageFile._save(im, op, [("zip", (0, 0) + im.size, 0, im.mode)])
elif filter == "RunLengthDecode":
Expand Down
4 changes: 4 additions & 0 deletions src/libImaging/Jpeg2KEncode.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,10 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
goto quick_exit;
}

if (strcmp(im->mode, "RGBA") == 0) {
image->comps[3].alpha = 1;
}

opj_set_error_handler(codec, j2k_error, context);
opj_set_info_handler(codec, j2k_warn, context);
opj_set_warning_handler(codec, j2k_warn, context);
Expand Down

0 comments on commit d0f3167

Please sign in to comment.