Open jason40418 opened 4 years ago
For renderPM
, it looks like there is a bg
option, read https://github.com/deeplook/svglib/issues/171#issuecomment-468239750.
For renderPDF
, the _PDFRenderer.drawImage
method is using canvas.drawInlineImage
instead of canvas.drawImage
, and the former doesn't expose the mask
parameter. I don't see what svglib can do.
After trying I found that Plotly
layout have bgcolor
in scene
and it default is rgba(0, 0, 0, 0)
.
If I change it to rgba(255, 255, 255, 0)
it would not cause black background.
So, I assume that some method would cause lost alpha value and finally result it.
Still getting this issue...It only happens with time series plots on plotly in my case, or maybe it only happens on graphs with more than x data points. I want to think it is a plotly issue because svg2rlg works fine on histograms as well as bar graphs that are also generated from plotly....
For
renderPM
, it looks like there is abg
option
Sadly, renderPM.drawTo_anything_(svglib.svg2rlg(fn), bg=0x00000000)
doesn't work/allow leaving the Alpha channel low, so this workaround only works for those who need a solid, non-tranparent background.
(That said, should this issue #209 be closed as a duplicate of #171?)
I seem to get the same bug when converting into pdf. A black background appears. My svg is generated by mapnik and seems to have an embedded raster image.
I too have the same issue with Python 3.9 and following packages: plotly==5.10.0 svglib==1.4.1 kaleido==0.1.0.post1
The svg does not have transparent background but svglib makes it black Attached svg
For
renderPM
, it looks like there is abg
option, read #171 (comment). ForrenderPDF
, the_PDFRenderer.drawImage
method is usingcanvas.drawInlineImage
instead ofcanvas.drawImage
, and the former doesn't expose themask
parameter. I don't see what svglib can do.
I totally agree with @claudep. Had a same problem when generating with BaseDocTemplate
. I just needed to change the source code of reportlab, and it worked flawlessly. For anyone interested, it is located in /usr/lib/python3.11/site-packages/reportlab/graphics/renderPDF.py
:
def drawImage(self, image):
path = image.path
# currently not implemented in other renderers
if path and (hasattr(path,'mode') or os.path.exists(image.path)):
- self._canvas.drawInlineImage(
+ self._canvas.drawImage(
path,
image.x, image.y,
image.width, image.height,
+ mask='auto'
)
I use plotly to draw a mesh3D and use "to_image()" to transfer into svg byte file. If use plotly "write_image()" to generate svg and png could success to export what I needed. However, when I use "io.BytesIO()" to read and "svg2rlg()" would cause black background.
Code
Plotly Layout Setting
svg2rlg
Result
export png from plotly write_image()
export svg from plotly to_image()
After svg2rlg() to read and export by renderPM.drawToFile()
Environments