mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-05 00:02:29 -04:00
got tiny images to show.
This commit is contained in:
parent
28024ff6e6
commit
f62780c6fc
2 changed files with 35 additions and 14 deletions
|
@ -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):
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Reference in a new issue