From f62780c6fc7c815c1e1c4fa0ab34fee73c88b6f9 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sun, 28 Apr 2019 03:04:39 +0300 Subject: [PATCH] got tiny images to show. --- pysollib/images.py | 2 +- pysollib/ui/tktile/svg.py | 47 ++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/pysollib/images.py b/pysollib/images.py index b1585bbd..60354904 100644 --- a/pysollib/images.py +++ b/pysollib/images.py @@ -84,7 +84,7 @@ class Images: if rec and 'suit' in rec: # img = PIL_Image(image=self.svg.render_fragment("6_heart")) img = self.svg.render_fragment( - id_="6_heart", width=self.CARDW, height=self.CARDH) + id_="queen_heart", width=self.CARDW, height=self.CARDH) img = PIL_Image(image=img) else: if not os.path.exists(f): diff --git a/pysollib/ui/tktile/svg.py b/pysollib/ui/tktile/svg.py index 9d4f0297..50c4bed5 100644 --- a/pysollib/ui/tktile/svg.py +++ b/pysollib/ui/tktile/svg.py @@ -7,9 +7,14 @@ import cairo -from gi.repository import Rsvg +from gi import require_version +require_version('Rsvg', '2.0') +from gi.repository import Rsvg # noqa: E402 -from pysollib.mfxutil import Image, ImageTk +import pysnooper # noqa: E402 + + +from pysollib.mfxutil import Image # noqa: E402 class SVGManager: @@ -18,19 +23,35 @@ class SVGManager: self.filename = filename self.svg = Rsvg.Handle().new_from_file(filename) + # Taken from https://stackoverflow.com/questions/44471795 + # Under MIT License - thanks. + def pixbuf2image(self, pxb): + """ Convert GdkPixbuf.Pixbuf to PIL image """ + data = pxb.get_pixels() + w = pxb.get_width() + h = pxb.get_height() + stride = pxb.get_rowstride() + mode = "RGB" + if pxb.get_has_alpha(): + mode = "RGBA" + img = Image.frombytes(mode, (w, h), data, "raw", mode, stride) + return img + + @pysnooper.snoop() def render_fragment(self, id_, width, height): id__ = '#' + id_ """docstring for render_""" dims = self.svg.get_dimensions_sub(id__)[1] width_, height_ = dims.width, dims.height - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, - int(width_), int(height_)) - context = cairo.Context(surface) - self.svg.render_cairo_sub(context, id__) - tk_image = ImageTk.PhotoImage('RGBA') - image = Image.frombuffer('RGBA', (width_, height_), - bytes(surface.get_data()), - 'raw', 'BGRA', 0, 1) - return image.crop((0, 0, width, height)) - tk_image = ImageTk.PhotoImage(image.crop((0, 0, width, height))) - return tk_image + pix = self.svg.get_pixbuf_sub(id__) + if False: + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, + int(width_), int(height_)) + context = cairo.Context(surface) + # context.scale(width_, height_) + assert self.svg.render_cairo_sub(context, id__) + buf = bytes(surface.get_data()) + image = Image.frombuffer('RGBA', (width_, height_), buf, + 'raw', 'BGRA', 0, 1) + image = self.pixbuf2image(pix) + return image.resize((width, height))