From df921eae256a91118ed25d569e4d9ed07910ace9 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sat, 27 Apr 2019 21:34:16 +0300 Subject: [PATCH] add svg loading code from stackoverflow. Starting work on https://github.com/shlomif/PySolFC/issues/3 . --- pysollib/ui/tktile/svg.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 pysollib/ui/tktile/svg.py diff --git a/pysollib/ui/tktile/svg.py b/pysollib/ui/tktile/svg.py new file mode 100644 index 00000000..adc34a7e --- /dev/null +++ b/pysollib/ui/tktile/svg.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Distributed under terms of the CC-BY-SA licence, see: +# https://stackoverflow.com/questions/22583035 +# Thanks! + + +def svgPhotoImage(file_path_name): + import Image + import ImageTk + import rsvg + import cairo + "Returns a ImageTk.PhotoImage object represeting the svg file" + # Based on pygame.org/wiki/CairoPygame and http://bit.ly/1hnpYZY + svg = rsvg.Handle(file=file_path_name) + width, height = svg.get_dimension_data()[:2] + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height)) + context = cairo.Context(surface) + # context.set_antialias(cairo.ANTIALIAS_SUBPIXEL) + svg.render_cairo(context) + tk_image = ImageTk.PhotoImage('RGBA') + image = Image.frombuffer('RGBA', (width, height), surface.get_data(), + 'raw', 'BGRA', 0, 1) + tk_image.paste(image) + return tk_image