Merge pull request #6925 from radarhere/pdf

Added saving RGBA images as PDFs
This commit is contained in:
mergify[bot] 2023-03-12 14:25:26 +00:00 committed by GitHub
commit d0f316718c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 6 deletions

View File

@ -8,7 +8,7 @@ import pytest
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):
@ -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"

View File

@ -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:

View File

@ -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.

View File

@ -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")
@ -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":

View File

@ -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);