Changed Image Element to use SuperImage class

This commit is contained in:
MikeTheWatchGuy 2019-04-20 20:49:55 -04:00
parent fbf6bf9644
commit 6a8680db8b
1 changed files with 37 additions and 11 deletions

View File

@ -13,8 +13,7 @@ import logging
import traceback import traceback
import os import os
import base64 import base64
import mimetypes
from PySimpleGUI import Radio
try: try:
from io import StringIO from io import StringIO
@ -1631,7 +1630,7 @@ class Image(Element):
:param key: :param key:
:param tooltip: :param tooltip:
''' '''
self.Filename = '/'+filename if filename else None # note that Remi expects a / at the front of resource files self.Filename = filename if filename else None # note that Remi expects a / at the front of resource files
self.Data = data self.Data = data
self.tktext_label = None self.tktext_label = None
self.BackgroundColor = background_color self.BackgroundColor = background_color
@ -1662,6 +1661,30 @@ class Image(Element):
super().__del__() super().__del__()
class SuperImage(remi.gui.Image):
def __init__(self, file_path_name=None, **kwargs):
super(SuperImage, self).__init__("/res/logo.png", **kwargs)
self.imagedata = None
self.mimetype = None
self.encoding = None
self.load(file_path_name)
def load(self, file_path_name):
self.mimetype, self.encoding = mimetypes.guess_type(file_path_name)
with open(file_path_name, 'rb') as f:
self.imagedata = f.read()
self.refresh()
def refresh(self):
i = int(time.time() * 1e6)
self.attributes['src'] = "/%s/get_image_data?update_index=%d" % (id(self), i)
def get_image_data(self, update_index):
headers = {'Content-type': self.mimetype if self.mimetype else 'application/octet-stream'}
return [self.imagedata, headers]
# ---------------------------------------------------------------------- # # ---------------------------------------------------------------------- #
# Graph # # Graph #
# ---------------------------------------------------------------------- # # ---------------------------------------------------------------------- #
@ -1807,18 +1830,20 @@ class Graph(Element):
def DrawImage(self, image_source=None, location=(None, None), size=(100, 100), color='black', font=None, angle=0): def DrawImage(self, image_source=None, location=(None, None), size=(100, 100), color='black', font=None, angle=0):
if location == (None, None): if location == (None, None):
return return
if type(image_source) is bytes: # if type(image_source) is bytes:
image = base64_to_style_image(image_source) # image = base64_to_style_image(image_source)
else: # else:
image = "url('{}')".format('/'+image_source) # image = "url('{}')".format('/'+image_source)
converted_point = self._convert_xy_to_canvas_xy(location[0], location[1]) converted_point = self._convert_xy_to_canvas_xy(location[0], location[1])
if self.Widget is None: if self.Widget is None:
print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***')
print('Call Window.Finalize() prior to this operation') print('Call Window.Finalize() prior to this operation')
return None return None
rpoint = remi.gui.Svg(size[0], size[1]) image_widget = SuperImage(image_source)
rpoint.style['background-image'] = image image_widget.attributes['x'] = converted_point[0]
image_widget.attributes['y'] = converted_point[1]
# image_widget.style['background-repeat'] = 'no-repeat'
self.Widget.append([image_widget,])
def Erase(self): def Erase(self):
if self.Widget is None: if self.Widget is None:
@ -4525,7 +4550,8 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form):
# ------------------------- IMAGE element ------------------------- # # ------------------------- IMAGE element ------------------------- #
elif element_type == ELEM_TYPE_IMAGE: elif element_type == ELEM_TYPE_IMAGE:
element = element # type: Image element = element # type: Image
element.Widget = remi.gui.Image(element.Filename) # element.Widget = remi.gui.Image(element.Filename)
element.Widget = SuperImage(element.Filename)
do_font_and_color(element.Widget) do_font_and_color(element.Widget)
if element.EnableEvents: if element.EnableEvents:
element.Widget.onclick.connect(element.ChangedCallback) element.Widget.onclick.connect(element.ChangedCallback)