Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed transparency when saving P mode images to PDF #7323

Merged
merged 4 commits into from
Aug 8, 2023

Conversation

radarhere
Copy link
Member

Resolves #7312

The (last unresolved) image from the issue is

which saved as a PDF with main becomes
Screenshot 2023-08-06 at 10 41 42 pm

print(img.mode)
print(img.info)

gives

P
{'transparency': b'\x00PJ\xdb/@k\x0b\x1d\x18\xac~\x9a\x98\xd9m\xe2s\xde\xbc\xc2\x90\xb0\xde\xef\xe8\xd1\xed\xcf\xb5\xbd\xde\xf2\xd9\xf1\xeb\xf2\xeb\xee\xf8'}

This is not a single transparency index, but different levels of opacity for different palette entries.

To attach this to a P mode PDF image, this PR writes a secondary image, the image converted to just an alpha channel, as an "SMask".

https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf describes "SMask" under 8.9.5 'Image Dictionaries' as

A subsidiary image XObject defining a soft-mask image (see 11.6.5.3, "Soft-Mask Images") that shall be used as a source of mask shape or mask opacity values in the transparent imaging model.

With that, the PDF becomes
Screenshot 2023-08-06 at 10 42 14 pm
which can be converted back to a transparent PNG using ImageMagick's convert.

Tests/test_file_pdf.py Outdated Show resolved Hide resolved
@radarhere radarhere merged commit 649ad8a into python-pillow:main Aug 8, 2023
50 checks passed
@radarhere radarhere deleted the pdf_p_transparency branch August 8, 2023 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RGBA image losing its transparency when saved as pdf
2 participants