typemytype / drawbot

http://www.drawbot.com
Other
398 stars 61 forks source link

`imageSize()` with svg images #517

Closed roberto-arista closed 1 year ago

roberto-arista commented 1 year ago

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 commented 1 year ago

Does image() support SVG to begin with?

roberto-arista commented 1 year ago

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

justvanrossum commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.