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

imageSize() with svg images #517

Closed
roberto-arista opened this issue Mar 2, 2023 · 6 comments
Closed

imageSize() with svg images #517

roberto-arista opened this issue Mar 2, 2023 · 6 comments

Comments

@roberto-arista
Copy link
Contributor

I can't get the correct width and height from a svg file with imageSize(). Even though the svg has a viewBox attribute with positive width and height, imageSize() returns (0, 0)

@justvanrossum
Copy link
Collaborator

Does image() support SVG to begin with?

@roberto-arista
Copy link
Contributor Author

roberto-arista commented Mar 2, 2023

I was able to draw some svgs on a canvas, so I assumed drawbot supports them

@justvanrossum
Copy link
Collaborator

Ah, it doesn't work on 10.15 for example. It's mostly an OS support thing.

That said the imageSize() implementation goes through some hoops to support PDF, (animated) GIF and "others". Maybe it needs some SVG handling code.

@typemytype
Copy link
Owner

A lots goes wrong while drawing svg as images...

but we could get the size from the nsimage object, we just have to flag _hasPixels = True with the ext .svg here

image

@roberto-arista
Copy link
Contributor Author

my workaround for now is this:

import xml.etree.ElementTree as ET
tree = ET.parse(imagePath)
_, _, imageWidth, imageHeight = [float(i) for i in tree.getroot().attrib["viewBox"].split()]

@roberto-arista
Copy link
Contributor Author

I understand that svg support as input format might be complicated and unreliable. I ended up converting the svg files in pdf with cairosvg and it worked fine. I'll open a pull request to add to the docs which formats are supported by the image proporties functions and the ImageObject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants