﻿from TSMaster import *
from io import BytesIO
from PIL import Image as PILImage

def ProcessImageFromBytes(ABytes):
  stream = BytesIO(ABytes)
  im = PILImage.open(stream)
  new_im = im.rotate(-90, expand=True)
  new_im.format = im.format
  return new_im

def ProcessImageFromFile(AFile):
  im = PILImage.open(AFile)
  new_im = im.rotate(90, expand=True)
  new_im.format = im.format
  return new_im
  
def ImageToBytes(image):
  stream = BytesIO()
  image.save(stream, image.format)
  return stream.getvalue()

# Auto Generated Python Code, do not modify START [UI] --------------
class Library_3rd_Examples(frmTSForm):
    def __init__(self):
        # set type name internally
        self.ExternalTypeName = type(self).__name__
        try:
            # Form properties
            if not self.IsConfigurationLoaded:
                self.Left = 42
                self.Top = 286
                self.Caption = 'Library_3rd_Examples'
                self.ClientHeight = 502
                self.ClientWidth = 786
            self.Color = clBtnFace
            self.DoubleBuffered = True
            self.Font.Charset = DEFAULT_CHARSET
            self.Font.Color = clWindowText
            self.Font.Height = -12
            self.Font.Name = 'Segoe UI'
            self.Font.Style = []
            self.KeyPreview = True
            self.Position = "poDesigned"
            self.Visible = True
            self.TextHeight = 15
            # Create control: pg = PageControl(self)
            self.pg = PageControl(self)
            self.pg.Name = "pg"
            self.pg.Parent = self
            self.pg.Left = 0
            self.pg.Top = 0
            self.pg.Width = 786
            self.pg.Height = 502
            self.pg.Cursor = crArrow
            self.pg.Align = "alClient"
            self.pg.Images = app.get_system_imagelist_16px()
            self.pg.TabOrder = 0
            # Create control: shtPIL = TabSheet(self)
            self.shtPIL = TabSheet(self)
            self.shtPIL.Name = "shtPIL"
            self.shtPIL.PageControl = self.pg
            self.shtPIL.Cursor = crArrow
            self.shtPIL.Caption = 'Pillow'
            # Create control: splitImage = Splitter(self)
            self.splitImage = Splitter(self)
            self.splitImage.Name = "splitImage"
            self.splitImage.Parent = self.shtPIL
            self.splitImage.Left = 597
            self.splitImage.Top = 0
            self.splitImage.Cursor = crArrow
            self.splitImage.Align = "alRight"
            self.splitImage.Height = 472
            # Create control: pnlImage = Panel(self)
            self.pnlImage = Panel(self)
            self.pnlImage.Name = "pnlImage"
            self.pnlImage.Parent = self.shtPIL
            self.pnlImage.Left = 0
            self.pnlImage.Top = 0
            self.pnlImage.Width = 597
            self.pnlImage.Height = 472
            self.pnlImage.Cursor = crArrow
            self.pnlImage.Align = "alClient"
            self.pnlImage.ShowCaption = "False"
            self.pnlImage.TabOrder = 0
            # Create control: lblImage = Label(self)
            self.lblImage = Label(self)
            self.lblImage.Name = "lblImage"
            self.lblImage.Parent = self.pnlImage
            self.lblImage.Left = 1
            self.lblImage.Top = 1
            self.lblImage.Width = 595
            self.lblImage.Height = 15
            self.lblImage.Cursor = crArrow
            self.lblImage.Align = "alTop"
            self.lblImage.Alignment = "taCenter"
            self.lblImage.Caption = 'Image'
            # Create control: img = Image(self)
            self.img = Image(self)
            self.img.Name = "img"
            self.img.Parent = self.pnlImage
            self.img.Left = 1
            self.img.Top = 16
            self.img.Width = 595
            self.img.Height = 455
            self.img.Cursor = crArrow
            self.img.Align = "alClient"
            # Create control: pnlControl = Panel(self)
            self.pnlControl = Panel(self)
            self.pnlControl.Name = "pnlControl"
            self.pnlControl.Parent = self.shtPIL
            self.pnlControl.Left = 600
            self.pnlControl.Top = 0
            self.pnlControl.Width = 178
            self.pnlControl.Height = 472
            self.pnlControl.Cursor = crArrow
            self.pnlControl.Align = "alRight"
            self.pnlControl.ShowCaption = "False"
            self.pnlControl.TabOrder = 1
            # Create control: btnLoadFromPython = Button(self)
            self.btnLoadFromPython = Button(self)
            self.btnLoadFromPython.Name = "btnLoadFromPython"
            self.btnLoadFromPython.Parent = self.pnlControl
            self.btnLoadFromPython.AlignWithMargins = True
            self.btnLoadFromPython.Left = 4
            self.btnLoadFromPython.Top = 4
            self.btnLoadFromPython.Width = 170
            self.btnLoadFromPython.Height = 25
            self.btnLoadFromPython.Cursor = crArrow
            self.btnLoadFromPython.Align = "alTop"
            self.btnLoadFromPython.Caption = 'Load From Python'
            self.btnLoadFromPython.Images = app.get_system_imagelist_16px()
            self.btnLoadFromPython.TabOrder = 0
            # Create control: btnLoadFromImage = Button(self)
            self.btnLoadFromImage = Button(self)
            self.btnLoadFromImage.Name = "btnLoadFromImage"
            self.btnLoadFromImage.Parent = self.pnlControl
            self.btnLoadFromImage.AlignWithMargins = True
            self.btnLoadFromImage.Left = 4
            self.btnLoadFromImage.Top = 35
            self.btnLoadFromImage.Width = 170
            self.btnLoadFromImage.Height = 25
            self.btnLoadFromImage.Cursor = crArrow
            self.btnLoadFromImage.Align = "alTop"
            self.btnLoadFromImage.Caption = 'Load From Image'
            self.btnLoadFromImage.Images = app.get_system_imagelist_16px()
            self.btnLoadFromImage.TabOrder = 1
        finally:
            # End UI auto creation
            self.EndUIAutoCreation()
# Auto Generated Python Code, do not modify END [UI] ----------------
        # your init code starts here...
        mm = MemoryStream()
        def on_btn1_click(sender):
            img = ProcessImageFromFile(app.get_host_app_path() + r'Data\Resources\TOSUN.png')
            bs = ImageToBytes(img)
            mm.Clear()
            mm.WriteFromBytes(bs)
            mm.Position = 0
            self.img.Picture.LoadFromStream(mm)
        def on_btn2_click(sender):            
            self.img.Picture.LoadFromFile(app.get_host_app_path() + r'Data\Resources\TOSUN.png')
            mm.Clear()
            self.img.Picture.SaveToStream(mm)
            bs = ImageToBytes(ProcessImageFromBytes(mm.ReadToBytes()))
            mm.Clear()
            mm.WriteFromBytes(bs)
            mm.Position = 0
            self.img.Picture.LoadFromStream(mm)
        self.btnLoadFromPython.OnClick = on_btn1_click
        self.btnLoadFromImage.OnClick = on_btn2_click
        
# Auto Generated Python Code, do not modify START [MAIN] ------------
if __name__ == "__main__":
    try:
        Library_3rd_Examples().Show()
        Application.Run()
    except SystemExit:
        pass
# Auto Generated Python Code, do not modify END [MAIN] --------------
