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

Grayscale PNGs render as a black box #349

Open
bdoms opened this issue May 17, 2017 · 3 comments
Open

Grayscale PNGs render as a black box #349

bdoms opened this issue May 17, 2017 · 3 comments
Labels
bug Something isn't working

Comments

@bdoms
Copy link

bdoms commented May 17, 2017

RGB and Indexed PNGs work fine, but when I switch a working image to be Grayscale (in GIMP it's Image -> Mode -> Grayscale) the image shows up with the correct dimensions, but as a single black box every time.

There's no explicit error or anything like that. In fact, it would probably be better if there were an error saying there's no support because it would save people like me time from hunting down the problem.

Here's a data URL of the python logo that behaves the way I'm describing:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAQAAABpN6lAAAAGo0lEQVR42u2deWwVRRzHP+W0AoogFEMpBQSFRFupomkLBCiKMWlAoLVgDDGGo4klGCUKghACbQIioMYaaEmMAkYD8Ug1lVZEgUSMaMPRcFU5yxXkkMu26x/d13b37Wvf2515nel7v/mvb/a38/10dnaO38xC+CyGOJIYTw65zGcpBaxmDWtYTQFLeIPZZJNBEnHE0GasC2nk8gGlHOU2RpDpNkfZTiHzyKC7nsJjyeR9KqgNWnSgVMdvLCJRJ/Gj2cS/noVbUy3beEwH8aPYI1h607pQxH0qi+/Eh9RJk1+fTpGq7lNfJlm8r4HMUhPAlrDINzCoYbJ68rPCJt/A4BbD1ZIfQ6VDMS9STiknpSA4zF1qtf32AlaTQ3vz13EclIDgHZUAFNgKd4IEy+9d2S0cwBWV+og7bIUb7ZcjXnjXyCBXHQDVloLtcsxTKBxAuSryO9i6P8scc02V0CPopAaA+20FywswQhDfEA5TA0C8rVjLHXNlSwAwTg0A/W3F+tUxV5EEAJlqAEj0K9gzfnkGcVMCgImqAqhmiCVHD/ZJ6Q8qC8DgMrl0Md8RU/lL0phAYQD1Q5Z97OWqxEGR4gDkpyi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Yl6KhB3QBsCPbM6n783OYA1LEilCO7Y5gRxN5BfQBUWEf/wVl7nuYjDjQTi60+gBoOs4HxDSsXrqw9fXiQhx1SBwEARjt6FpGG0Dc8Jw56AxCv/xEIUQBRAK3VxEXPEYoCiAKIAogCiAJo0wAuUEoJVZEJ4AxZDd8oSHc8mLdNA6iir+UusZRHFgD/z+n05h81ASRIkO980Ml7tlyKnC3eUwKApY53yrTlGqMGgBiuh2nxIs0xPkEBEx8jVuB4H+vXjG40vCNa3RYLB/CH432KnUKd1XgPiD9wcZLfXQZzy5IjR6VJkU+EA7hEkm3RrsLy+3FVPrFSb325LBzBdeab3xfszHS/NctJqk2MTZbyrcEaKqnght/fi1ScGwzfSTRlalX/RpsbltNnSwJHeLS+TeCc5PX9Feq8/Z2tN5ukyd/fcniDGpbGT8LFn2BmyweiqmSpbBa2f3AnOXqJ91kPZvGDq6POfEENe1kgbR962KwbmaxlXwhHtdRRyXqmeT8aXS3rykhepZByjtl69QYGt6hiB+uZx1hdP7gemt1Df4aRTBJDSQjuyGsZ9j+hydryNgGwygAAAABJRU5ErkJggg==

This is important because Grayscale images are usually smaller than their RGB equivalent, so they're useful for saving space and memory, especially when working with a large number of images.

@timobrembeck timobrembeck added the bug Something isn't working label Feb 2, 2023
@stefan6419846
Copy link
Contributor

stefan6419846 commented Oct 3, 2023

This seems to be a reportlab issue/limitation, as they apparently do not like alpha channels for images at all (https://hg.reportlab.com/hg-public/reportlab/file/tip/src/reportlab/pdfbase/pdfdoc.py#l2050):

_mode2CS={'RGB':'DeviceRGB', 'L':'DeviceGray', 'CMYK':'DeviceCMYK'}

Thus, xhtml2pdf always creates RGB images for not support modes:

if mode == 'RGBA':
im.load()
self._dataA = PmlImageReader(im.split()[3])
im = im.convert('RGB')
self.mode = 'RGB'
elif mode not in ('L', 'RGB', 'CMYK'):
im = im.convert('RGB')
self.mode = 'RGB'

The only way I see for this is to convert the image to RGB yourself (replacing the alpha channel with a color of your choice) and thus completely avoid the default alpha channel handling of xhtml2pdf. I am not aware of any reliable way to automatically replace the background of an image as you have to be aware of the content for this to choose the correct color.

@timobrembeck
Copy link
Collaborator

@stefan6419846 Thanks a lot for taking a deeper look here! Do you think we need a special section in the docs for this problem?

@stefan6419846
Copy link
Contributor

It might be worth documenting, although I am not sure where this would suit the best. Maybe by adding a dedicated FAQ page which can cover such aspects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants