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:
|
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):
|
||||||
|
|
|
@ -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
|
||||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
|
pix = self.svg.get_pixbuf_sub(id__)
|
||||||
int(width_), int(height_))
|
if False:
|
||||||
context = cairo.Context(surface)
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
|
||||||
self.svg.render_cairo_sub(context, id__)
|
int(width_), int(height_))
|
||||||
tk_image = ImageTk.PhotoImage('RGBA')
|
context = cairo.Context(surface)
|
||||||
image = Image.frombuffer('RGBA', (width_, height_),
|
# context.scale(width_, height_)
|
||||||
bytes(surface.get_data()),
|
assert self.svg.render_cairo_sub(context, id__)
|
||||||
'raw', 'BGRA', 0, 1)
|
buf = bytes(surface.get_data())
|
||||||
return image.crop((0, 0, width, height))
|
image = Image.frombuffer('RGBA', (width_, height_), buf,
|
||||||
tk_image = ImageTk.PhotoImage(image.crop((0, 0, width, height)))
|
'raw', 'BGRA', 0, 1)
|
||||||
return tk_image
|
image = self.pixbuf2image(pix)
|
||||||
|
return image.resize((width, height))
|
||||||
|
|
Loading…
Add table
Reference in a new issue