Merge pull request #7316 from radarhere/pdf_alpha

This commit is contained in:
Hugo van Kemenade 2023-08-04 11:42:08 +03:00 committed by GitHub
commit 917769bd9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -125,7 +125,6 @@ def _save(im, fp, filename, save_all=False):
# (packbits) or LZWDecode (tiff/lzw compression). Note that # (packbits) or LZWDecode (tiff/lzw compression). Note that
# PDF 1.2 also supports Flatedecode (zip compression). # PDF 1.2 also supports Flatedecode (zip compression).
bits = 8
params = None params = None
decode = None decode = None
@ -134,10 +133,11 @@ def _save(im, fp, filename, save_all=False):
width, height = im.size width, height = im.size
dict_obj = {"BitsPerComponent": 8}
if im.mode == "1": if im.mode == "1":
if features.check("libtiff"): if features.check("libtiff"):
filter = "CCITTFaxDecode" filter = "CCITTFaxDecode"
bits = 1 dict_obj["BitsPerComponent"] = 1
params = PdfParser.PdfArray( params = PdfParser.PdfArray(
[ [
PdfParser.PdfDict( PdfParser.PdfDict(
@ -152,17 +152,17 @@ def _save(im, fp, filename, save_all=False):
) )
else: else:
filter = "DCTDecode" filter = "DCTDecode"
colorspace = PdfParser.PdfName("DeviceGray") dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceGray")
procset = "ImageB" # grayscale procset = "ImageB" # grayscale
elif im.mode == "L": elif im.mode == "L":
filter = "DCTDecode" filter = "DCTDecode"
# params = f"<< /Predictor 15 /Columns {width-2} >>" # params = f"<< /Predictor 15 /Columns {width-2} >>"
colorspace = PdfParser.PdfName("DeviceGray") dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceGray")
procset = "ImageB" # grayscale procset = "ImageB" # grayscale
elif im.mode == "P": elif im.mode == "P":
filter = "ASCIIHexDecode" filter = "ASCIIHexDecode"
palette = im.getpalette() palette = im.getpalette()
colorspace = [ dict_obj["ColorSpace"] = [
PdfParser.PdfName("Indexed"), PdfParser.PdfName("Indexed"),
PdfParser.PdfName("DeviceRGB"), PdfParser.PdfName("DeviceRGB"),
255, 255,
@ -171,15 +171,15 @@ def _save(im, fp, filename, save_all=False):
procset = "ImageI" # indexed color procset = "ImageI" # indexed color
elif im.mode == "RGB": elif im.mode == "RGB":
filter = "DCTDecode" filter = "DCTDecode"
colorspace = PdfParser.PdfName("DeviceRGB") dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceRGB")
procset = "ImageC" # color images procset = "ImageC" # color images
elif im.mode == "RGBA": elif im.mode == "RGBA":
filter = "JPXDecode" filter = "JPXDecode"
colorspace = PdfParser.PdfName("DeviceRGB")
procset = "ImageC" # color images procset = "ImageC" # color images
dict_obj["SMaskInData"] = 1
elif im.mode == "CMYK": elif im.mode == "CMYK":
filter = "DCTDecode" filter = "DCTDecode"
colorspace = PdfParser.PdfName("DeviceCMYK") dict_obj["ColorSpace"] = PdfParser.PdfName("DeviceCMYK")
procset = "ImageC" # color images procset = "ImageC" # color images
decode = [1, 0, 1, 0, 1, 0, 1, 0] decode = [1, 0, 1, 0, 1, 0, 1, 0]
else: else:
@ -204,6 +204,7 @@ def _save(im, fp, filename, save_all=False):
elif filter == "DCTDecode": elif filter == "DCTDecode":
Image.SAVE["JPEG"](im, op, filename) Image.SAVE["JPEG"](im, op, filename)
elif filter == "JPXDecode": elif filter == "JPXDecode":
del dict_obj["BitsPerComponent"]
Image.SAVE["JPEG2000"](im, op, filename) Image.SAVE["JPEG2000"](im, op, filename)
elif filter == "FlateDecode": elif filter == "FlateDecode":
ImageFile._save(im, op, [("zip", (0, 0) + im.size, 0, im.mode)]) ImageFile._save(im, op, [("zip", (0, 0) + im.size, 0, im.mode)])
@ -228,10 +229,9 @@ def _save(im, fp, filename, save_all=False):
Width=width, # * 72.0 / x_resolution, Width=width, # * 72.0 / x_resolution,
Height=height, # * 72.0 / y_resolution, Height=height, # * 72.0 / y_resolution,
Filter=filter, Filter=filter,
BitsPerComponent=bits,
Decode=decode, Decode=decode,
DecodeParms=params, DecodeParms=params,
ColorSpace=colorspace, **dict_obj,
) )
# #