Wrong Transparency in ImageSurface.get_data

Open
#357 3 comments 0 reactions 0 assignees View on GitHub

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

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:

1_write_to_png 2_get_data

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

image

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:

image

And the color channels (without alpha):

image

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Kozea/CairoSVG

All issues in Kozea/CairoSVG

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.