Fix problem with fetch urls from cairoSVG

remotes/origin/fix-throttling-bug
Jesús Espino 2017-07-20 13:41:11 +02:00
parent 9b80f5a1a3
commit d35135a3f5
1 changed files with 8 additions and 1 deletions

View File

@ -33,15 +33,22 @@ from io import BytesIO
# SVG thumbnail generator
try:
from cairosvg.surface import PNGSurface
from cairosvg.url import fetch
import magic
def url_fetcher(url, resource_type):
if url.startswith("data:"):
return fetch(url, resource_type)
return b""
def svg_image_factory(fp, filename):
mime_type = magic.from_buffer(fp.read(1024), mime=True)
if mime_type != "image/svg+xml":
raise TypeError
fp.seek(0)
png_data = PNGSurface.convert(fp.read())
png_data = PNGSurface.convert(fp.read(), url_fetcher=url_fetcher)
return PngImageFile(BytesIO(png_data))
Image.register_mime("SVG", "image/svg+xml")