From 3d5c16e9e6d351e9a6e2ed8f3ca87b78cf739119 Mon Sep 17 00:00:00 2001 From: Karl Bedrich Date: Mon, 9 Apr 2018 16:38:30 +0800 Subject: [PATCH] temporary FIX: drawToPMCanvas -> dpi ignored Hi there, when creating a raster image from an SVG with dpi values different then 72, the image range is scaled, but not the image itself. Version used: 0.8.1 Minimum working example: ` import numpy as np from lxml import etree from svglib.svglib import SvgRenderer from reportlab.graphics import renderPM import pylab as plt s = ''' aaaaa bbb '''.encode('UTF-8') parser = etree.XMLParser(remove_comments=True, recover=True) xml = etree.fromstring(s, parser=parser) for dpi in (72, 150, 300): drw = SvgRenderer('_tempfile.svg').render(xml) pil = renderPM.drawToPIL(drw, dpi=dpi) plt.figure(dpi) plt.imshow(np.array(pil)) plt.show() ` This generated the following output: ![image](https://user-images.githubusercontent.com/350050/38487612-650b040c-3c13-11e8-9e2a-ad8f3b0cad4b.png) Output after fix: ![image](https://user-images.githubusercontent.com/350050/38487858-3208db50-3c14-11e8-8b48-0e323648925d.png) I am sure that there is a better way ... such as incorporating d.renderScale (in renderScaledDrawing) right --- src/reportlab/graphics/renderPM.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/reportlab/graphics/renderPM.py b/src/reportlab/graphics/renderPM.py index 9ecafe2f..e394569f 100644 --- a/src/reportlab/graphics/renderPM.py +++ b/src/reportlab/graphics/renderPM.py @@ -638,6 +638,8 @@ def setLineWidth(self,width): def drawToPMCanvas(d, dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_): d = renderScaledDrawing(d) + scale = dpi/72.0 + d.scale(scale,scale) c = PMCanvas(d.width, d.height, dpi=dpi, bg=bg, configPIL=configPIL) draw(d, c, 0, 0, showBoundary=showBoundary) return c