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

temporary FIX: drawToPMCanvas -> dpi ignored #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Apr 9, 2018

  1. 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 = ''' <svg 
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:cc="http://creativecommons.org/ns#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    version="1.1"
    
    width="100mm"
    height="100mm" >
    
    <image 
        x="0mm" y="27mm" 
        width="45mm" height="45mm" 
        xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAtAC0DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG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ux5H4YnvoLyafSOSHYtz781tH4xanYn7LJcgMnBBaqnhbQvEmiy3ITTZNsjNk+xrN1DwXp11ePPcyqrscsCKvJsLxXVyOl/ZVKk53lzQnLljFXduX16nyVTF4zKaacKlSm3vo0n+B6h8aIDolrHLZT7uhIRs965G1v5fG9qugzW5t8cecy7evvWpqV3capIkd9IZBuAw1avxM02y0T4dR6jpcAhmKtl1616/H2TZlkmVKvXr89N35opWbXa/Q+5wtejxHmmMx2Hj7OlTgnKD1uuyfQZ4P0Kw8I3KFr+ORk7mQGu0m8QWGqyq7PGfY182WHifXZbgGTUHP411eh65qjSoDeN1rn4Rz3B8UUKdOnScPZrkV3fQ4sBxxRyanLD0KNqcpXav1PUPG/jq08M2rRWmkpLvTGUhzjNeK6xrtxe6lLdCJk3tnbtIxXtXhGxtda02VtThEpERwW+leY+I9Os49ZnRIQAH4ArpxPDmYvNatHD4jkUbdO5fHuMxuPyzC46rJezm3yxsk42316n/9k="/>
    <text 
        x="22.5mm" y="50mm" 
        font-family="sans-serif" 
        font-size="4.666666666666667mm" fill="black">aaaaa</text>
    <text 
        x="77.5mm" y="50mm" 
        font-family="sans-serif" 
        font-size="4.666666666666667mm" fill="black">bbb</text>
    </svg>'''.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
    radjkarl authored Apr 9, 2018
    Configuration menu
    Copy the full SHA
    3d5c16e View commit details
    Browse the repository at this point in the history