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

RGBA image losing its transparency when saved as pdf #7312

Closed
pubpub-zz opened this issue Aug 1, 2023 · 20 comments · Fixed by #7323
Closed

RGBA image losing its transparency when saved as pdf #7312

pubpub-zz opened this issue Aug 1, 2023 · 20 comments · Fixed by #7323

Comments

@pubpub-zz
Copy link

pubpub-zz commented Aug 1, 2023

What did you do?

convert an png to image to pdf or JPEG2000
edit:
after checking with various image viewer, I've observed that the issue could be linked with my original viewer(nomacs)
In order to not bother any I will left on side JPEG2000 and carry on my investigations

What did you expect to happen?

have the good alpha channel

What actually happened?

alpha channel not correctly displayed in pdf is not the good one

What are your OS, Python and Pillow versions?

  • OS: windows 11
  • Python: 3.10
  • Pillow: 10.0
    with this png
    tt
from PIL import Image
img = Image.open("tt.png")
print("mode : ",img.mode)
img.save("tt.pdf")
img.save("tt.jp2")
img = Image.open("tt.jp2")
print("mode jp2 : ",img2.mode)
img.save("tt.jp2")

output
mode : RGBA
mode jp2 : RGBA

but the image in the PDF has lost its transparency
same for the jpeg2000 image, although still reported as RGBA

@homm
Copy link
Member

homm commented Aug 1, 2023

I can't confirm JPEG2000. Your image is transparent after conversion.

@pubpub-zz
Copy link
Author

pubpub-zz commented Aug 1, 2023

@homm
this is the pdf I'm getting:
tt.pdf
the background is black whereas the page is normally white
can you confirm you have the same result ?

@homm
Copy link
Member

homm commented Aug 1, 2023

This is how your pdf is rendered to me. It's unclear to me if PDF really can be transparent.

Screenshot 2023-08-02 at 01 55 57

@radarhere radarhere changed the title RGBA image loosing its transparency when saved as pdf or jpeg2000 RGBA image losing its transparency when saved as pdf or jpeg2000 Aug 1, 2023
@radarhere
Copy link
Member

I'm also unable to replicate the problem with either format. Are you sure that you are using Pillow 10.0.0, and not a version before 9.5.0?

@pubpub-zz
Copy link
Author

I reinstalled it before to confirm the bug was on the latest version.
Actually it looks like the problem is also linked with the viewer:
mozilla/pdf.js#5649 (comment)

Personally I checked all under windows with acrobat reader (black) sumatra (transparent) pdf.js( black and damaged) chrome (black)

@mnmtz
Copy link

mnmtz commented Aug 2, 2023

I am using Pillow 10.0.0 and getting the same incorrect results using the code above:

Transparent_Image
Transparent_Image.pdf

Transparent_Image2
Transparent_Image2.pdf

@radarhere
Copy link
Member

How did you install Pillow?

What version of OpenJPEG are you using?

>>> from PIL import features
>>> features.version_codec("jpg_2000")
'2.5.0'

@mnmtz
Copy link

mnmtz commented Aug 2, 2023

I have installed it using pip on Windows 11. The OpenJPEG version is 2.5.0.

@homm
Copy link
Member

homm commented Aug 2, 2023

Sorry, people, could you be a bit more verbose here? You are claiming that the initial problem is related to both PDF and JP2K. So could we isolate this? For example, if you are sure that the problem with PDF happens because of embedding bad JP2K, let's focus on the JP2K problem here. Otherwise, if there are separate problems, let's consider them separate.

I've tried to convert the example image to JP2K above and experienced no problems, it has transparency. If some of you have different results, could you please at least mention this and provide your result with broken transparency and expose why you think it s broken (software you are using to viewing, for example).

@mnmtz
Copy link

mnmtz commented Aug 2, 2023

The problem is not (only) related to JP2K, because in my example you find 2 transparent png images, both are not correctly converted to PDF.

The PDF files have been opened with:

  • Acrobat Reader 2023.003.20244
  • Google Chrome 115.0.5790.110
  • qpdf 11.5.0 (extracting the image)
  • macOS 13.5 Preview App

@homm
Copy link
Member

homm commented Aug 2, 2023

"is not related to JP2K" and "is not only related to JP2K" are two completely different statements.

@pubpub-zz could you provide any example or proof that you are experiencing problems with JP2K?

@homm
Copy link
Member

homm commented Aug 2, 2023

I am using Pillow 10.0.0 and getting the same incorrect results using the code above:

Why do you consider these results incorrect? Which result would be correct?

I've tried to convert the second image to PDF using macOS Preview App (with "Alpha" checkbox applied), the results are identical:

Screenshot 2023-08-02 at 16 20 54

@mnmtz
Copy link

mnmtz commented Aug 2, 2023

Your results are correct, but the PDFs generated using Pillow 10.0.0 look like this (on all PDF viewers mentioned above):

Screenshot 2023-08-02 152450

@homm
Copy link
Member

homm commented Aug 2, 2023

but the PDFs generated using Pillow 10.0.0

The file opened in the right window on my screenshot is your PDF from the comment which claimed as generated using Pillow 10.0.

@pubpub-zz pubpub-zz changed the title RGBA image losing its transparency when saved as pdf or jpeg2000 RGBA image losing its transparency when saved as pdf Aug 2, 2023
@pubpub-zz
Copy link
Author

pubpub-zz commented Aug 2, 2023

My test showed that the viewer allows to show or not the transparency. I have the same feeling about jpeg 2000 viewer (the image was not good with nomacs but displayed correctly in gimp and imagemagick)

digging further in the pdf specification I found this:
image

I've removed ColorSpace to take into account
I've set the SMaskIn to 1 and the transparency "appeared" 😁 in Acrobat Reader.
I've checked in pdf.js and there the image is damaged:
image
removing the "BitsPerComponent fixed this:
image

There is still a color inversion but I think this is a bug in pdf.js(mozilla/pdf.js#16782)

to sum-up, can you :

  • add SMaskIn=1 (to enable alpha decoding)
  • remove ColorSpace and BitsPerComponents (to allow the extraction of data from the image)

with the fixed PDF:
tt1.pdf

@radarhere
Copy link
Member

I've created PR #7316 to set SMaskInData to 1.

I've removed ColorSpace to take into account

Could you explain this some more?

remove ColorSpace and BitsPerComponents (to allow the extraction of data from the image)

What makes you think that the color inversion is a bug in pdf.js, but not the BitsPerComponent problem? Does "allow the extraction of data from the image" mean something special, or is that just another way of saying "appear correctly in a viewer"?

@pubpub-zz
Copy link
Author

I've created PR #7316 to set SMaskInData to 1.

👍

I've removed ColorSpace to take into account
Could you explain this some more?

This is an extract from Pdf 1.7 reference:
image
(...)
image

For the same reason we should ignore BitsPerComponents
image

remove ColorSpace and BitsPerComponents (to allow the extraction of data from the image)

What makes you think that the color inversion is a bug in pdf.js, but not the BitsPerComponent problem? Does "allow the extraction of data from the image" mean something special, or is that just another way of saying "appear correctly in a viewer"?

I've tested a few set of PdfViewer( Acrobat / Sumatra / pdf.js / chrome) and the issue was observable in PDF.js. previously this time of error was linked with Decode entry but setting the Decode entry was not improving that.

@radarhere
Copy link
Member

Thanks. I've updated the PR to remove those properties.

I presume that is sufficient to fix the original problem here. 'Transparent_Image2' is also an RGBA image, so I think it should fix that as well.

@radarhere
Copy link
Member

'Transparent_Image' is different, because it is not RGBA, but P.

I've created PR #7323. With it, running

from PIL import Image
img = Image.open("transparent_image.png")
img.save("transparent_image.pdf")

gives
transparent_image.pdf

This should clearly be an improvement, because the background is no longer green, but @mnmtz, let us know if you think that is fixed.

If that is all good, then everything in this issue should be resolved.

@mnmtz
Copy link

mnmtz commented Aug 7, 2023

As for me, it looks like that the PR is doing the trick.

Thank you!

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 a pull request may close this issue.

4 participants