1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-22 03:04:09 -04:00

got tiny images to show.

This commit is contained in:
Shlomi Fish 2019-04-28 03:04:39 +03:00
parent 869bcece37
commit 3ca42f2b26
2 changed files with 35 additions and 14 deletions

View file

@ -85,7 +85,7 @@ class Images:
if rec and 'suit' in rec: if rec and 'suit' in rec:
# img = PIL_Image(image=self.svg.render_fragment("6_heart")) # img = PIL_Image(image=self.svg.render_fragment("6_heart"))
img = self.svg.render_fragment( 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) img = PIL_Image(image=img)
else: else:
if not os.path.exists(f): if not os.path.exists(f):

View file

@ -7,9 +7,14 @@
import cairo 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: class SVGManager:
@ -18,19 +23,35 @@ class SVGManager:
self.filename = filename self.filename = filename
self.svg = Rsvg.Handle().new_from_file(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): def render_fragment(self, id_, width, height):
id__ = '#' + id_ id__ = '#' + id_
"""docstring for render_""" """docstring for render_"""
dims = self.svg.get_dimensions_sub(id__)[1] dims = self.svg.get_dimensions_sub(id__)[1]
width_, height_ = dims.width, dims.height width_, height_ = dims.width, dims.height
pix = self.svg.get_pixbuf_sub(id__)
if False:
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
int(width_), int(height_)) int(width_), int(height_))
context = cairo.Context(surface) context = cairo.Context(surface)
self.svg.render_cairo_sub(context, id__) # context.scale(width_, height_)
tk_image = ImageTk.PhotoImage('RGBA') assert self.svg.render_cairo_sub(context, id__)
image = Image.frombuffer('RGBA', (width_, height_), buf = bytes(surface.get_data())
bytes(surface.get_data()), image = Image.frombuffer('RGBA', (width_, height_), buf,
'raw', 'BGRA', 0, 1) 'raw', 'BGRA', 0, 1)
return image.crop((0, 0, width, height)) image = self.pixbuf2image(pix)
tk_image = ImageTk.PhotoImage(image.crop((0, 0, width, height))) return image.resize((width, height))
return tk_image