Wrong Transparency in ImageSurface.get_data
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- computer-graphics
Research direction
Start by running the supplied CairoSVG, cairocffi, NumPy, and OpenCV reproduction, then compare the ImageSurface.get_data and write_to_png entry points on the PNGSurface cairo object. Check the Cairo image-buffer convention and determine whether the difference is expected; done means the behavior is corrected or clearly documented, with coverage for the reproduced transparency case.
Written by the indexing model from the issue text.
Description
Hello,
It seems that ImageSurface.get_data gives different transparency values than ImageSurface.write_to_png. It seems as if the one from get_data is incorrect.
How to reproduce:
import cairosvg.parser
import numpy as np
import cv2
svg_text = """<?xml version="1.0" encoding="utf-8" ?>
<svg width="20" height="20" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<rect fill="white" x="10" y="0" width="10" height="20"/>
<circle fill="cyan" cx="10" cy="10" r="5" />
</svg>
"""
tree = cairosvg.parser.Tree(bytestring=svg_text.encode('utf-8'))
surf = cairosvg.surface.PNGSurface(tree, None, dpi=96).cairo
# write to PNG
surf.write_to_png('1_write_to_png.png')
# write to RGBA buffer
data = np.frombuffer(surf.get_data(), np.uint8).reshape(surf.get_height(), surf.get_width(), 4)
cv2.imwrite("2_get_data.png", data)
This generates the two PNG's here:

The problem is visible when they are viewed on a white background. Zooming in reveals:

The left one is write_to_png. The right get_data. The black outline on the second image is not correct.
Here is the alpha channel. It looks identical:

And the color channels (without alpha):

It looks like the alpha value is pre-applied to the RGB channel.
Is this just a different convention, as we are using cairo internals and the output needs to be processed before using it as a typical RGBA channel?
I found the snipped with get_data in some issues here, maybe it was incomplete?
System information:
Ubuntu 21.10
Python 3.8.12
CairoSVG==2.5.2
cairocffi==1.3.0
libcairo2 1.16.0
I tested other png writers like the one in matlab and PIL and get the same output.
- Dominant language
- Python
- Stars
- 959
- Forks
- 172
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Kozea/CairoSVG
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
并发下的渲染表现Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Exception "The SVG size is undefined" raised using svg2pngPossibly taken @DSeaStar claimed this 111 days ago. Openbug
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
feature
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Similar issues
-
accepted
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
future-agi/future-agi#5908 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
eclipse-score/coverage_tool#101 ·
Maintainers usually reply within 2 days
-
Placeholder export disables HTML image input for picture descriptionPossibly taken @cloudiful claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
docling-project/docling-serve#817 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Techiebutler/freeframe#687 ·
Maintainers usually reply within 1 day
-
Parse truncated verbose GC logs up to the first XML errorPossibly taken A pull request linked to this issue is open or already merged. Openbug verbose GC
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
IBM/javacore-analyser#677 ·
Maintainers usually reply within 1 day