shibukawa / imagesize_py

MIT License
222 stars 43 forks source link

cannot read a svg file #59

Closed 12rambau closed 3 months ago

12rambau commented 1 year ago

I try to get the size of a .svg file. This file is displayed just fine on my computer but raise an error in this lib. do you have any idea why ?

To reproduce:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg">
    <style> * { fill: black } </style>
    <polygon points="0,1 1,1 0.5,0" class="triangle" />
</svg>
import imagesize

w, h = imagesize.get("triangle.svg")
The full error traceback
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/.pyenv/versions/3.8.3/lib/python3.8/site-packages/imagesize.py:210, in get(filepath)
    209 data = data.decode('utf-8')
--> 210 width = re.search(r'[^-]width="(.*?)"', data).group(1)
    211 height = re.search(r'[^-]height="(.*?)"', data).group(1)

AttributeError: 'NoneType' object has no attribute 'group'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/Users/pierrickrambaud/Documents/travail/FAO/app_buffer/sphinx-favicon/toto.ipynb Cellule 3 in ()
      1 import imagesize
----> 3 w, h = imagesize.get("/Users/pierrickrambaud/Documents/travail/FAO/app_buffer/sphinx-favicon/tests/roots/test-static_files/gfx/nested/triangle.svg")

File ~/.pyenv/versions/3.8.3/lib/python3.8/site-packages/imagesize.py:213, in get(filepath)
    211     height = re.search(r'[^-]height="(.*?)"', data).group(1)
    212 except Exception:
--> 213     raise ValueError("Invalid SVG file")
    214 width = _convertToPx(width)
    215 height = _convertToPx(height)

ValueError: Invalid SVG file
tuncbkose commented 3 months ago

In case you are still curious, or anyone else wonders this, you can look at the source code: https://github.com/shibukawa/imagesize_py/blob/8d88ec6b646d6184b5633604551d6fc154783073/imagesize/imagesize.py#L204-L215 Since your svg doesn't have width or height, there are no regex matches.

12rambau commented 3 months ago

Thanks for your answer, I thought it was computed from the value but since the value is extracted from the xml it make complete sense that it doesn't work. Sorry for the noise.