Send terminal output to an embedded console

Hello,
I have written a small program to watermark pictures. When it watermarks then, it produces a terminal output which I would like to send it in a real-time way to an embedded console within the GUI. I have created the console using this code

hbox5 = wx.BoxSizer(wx.HORIZONTAL)
         text = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE|wx.TE_READONLY, size=(550, 200))
         self.text = text
         hbox5.Add(text, 0, wx.ALL , 5)

and the function to extract the terminal output and populate the embedded console is

     def textoutput(self, text):
         self.text.AppendText(text)

However, it is not a real time procedure, as it only send the terminal output to the console just when it has finished processing all the images. Is there anyway to send the terminal output to the console in a real-time way, line by line, as it shows in the terminal?
Cheers!

Dani

···

--
Daniel Valverde Saub�
c/Guillem Colteller 9 Baixos 3
17005 Girona
Spain
Cellular: +34651987662
e-mail: dani.valverde@gmail.com
http://www.acrocephalus.net
http://natupics.blogspot.com

Si no �s del tot necessari, no imprimeixis aquest missatge. Si ho fas utilitza paper 100% reciclat i blanquejat sense clor. D'aquesta manera ajudar�s a estalviar aigua, energia i recursos forestals. GR�CIES!

Do not print this message unless it is absolutely necessary. If you must print it, please use 100% recycled paper whitened without chlorine. By doing so, you will save water, energy and forest resources. THANK YOU!

Hello, I have written a small
program to watermark pictures. When

  > it watermarks then, it produces a terminal output which I

would

  > like to send it in a real-time way to an embedded console

within

  > the GUI. I have created the console using this code

  >

  > hbox5 = wx.BoxSizer(wx.HORIZONTAL) text =

wx.TextCtrl(panel, -1,

  > style=wx.TE_MULTILINE|wx.TE_READONLY, size=(550, 200))

self.text =

  > text hbox5.Add(text, 0, wx.ALL , 5)

  >

  > and the function to extract the terminal output and

populate the

  > embedded console is

  >

  > def textoutput(self, text): self.text.AppendText(text)

  >

  > However, it is not a real time procedure, as it only send

the

  > terminal output to the console just when it has finished

  > processing all the images. Is there anyway to send the

terminal

  > output to the console in a real-time way, line by line, as

it shows

  > in the terminal? Cheers!

  >

  > Dani

  >

There are 2 possible scenarios in your question:

1) The gui is also a part of the watermark program, or

2) The gui is executing the watermarking program as a separate

task.

In the first case you may need to do a refresh() in textoutput in

the

second you need to look at how you are passing the text back and

how

you are executing the watermark program.

Gadget/Steve
···

On 07/02/2011 11:07 AM, Dani Valverde wrote:

Hi Steve,

The GUI is executing the watermarking program as a separate task. As

I am pretty new to python, I paste the code in case you could help:

#!/usr/bin/python

# WipGUI.py

import wx, os, shutil, fnmatch, Image, ImageEnhance, sys, textwrap,

re, wx.lib.intctrl, time

ID_HELP = 1

ID_ABOUT = 2

ID_GO = 3

class Wip(wx.Frame):

    def __init__(self, parent, id, title):

        wx.Frame.__init__(self, parent, id, title, size=(750,500))

       

#Define menus

        menubar = wx.MenuBar()

    #Define file menu

        file = wx.Menu()

        quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')

        file.AppendItem(quit)

        self.Bind(wx.EVT_MENU, self.OnQuit, id=1)

    #Define help menu

        helpMenu = wx.Menu()

        helpMenu.Append(ID_HELP, '&Help')

        helpMenu.Append(ID_ABOUT, '&About')

        self.Bind(wx.EVT_MENU, self.OnAboutBox, id=ID_ABOUT)

       

#Append menus

        menubar.Append(file, '&File')

        menubar.Append(helpMenu, '&Help')

        self.SetMenuBar(menubar)

#Define main panel

        panel = wx.Panel(self, -1)

        vbox1 = wx.BoxSizer(wx.VERTICAL)

    #Define buttons

        #Define button to select the pictures folder

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)

        self.picsFolder = wx.Button(panel,-1,"Select your picture

directory",size=(210,30))

        self.DirPathTextBox=wx.TextCtrl(panel,-1,"",size=(400,-1))

        self.Bind(wx.EVT_BUTTON, self.SelDir, self.picsFolder)

        hbox1.Add(self.picsFolder, 0, wx.ALL , 5)

        hbox1.Add(self.DirPathTextBox, 0, wx.ALL , 5)

        #Define the button to select the watermark file

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)

        mask ='*.png'

        self.markFile = wx.Button(panel,-1,"Select your watermark

file",size=(210,30))

        self.Bind(wx.EVT_BUTTON, self.SelFile, self.markFile)

        self.FilePathTextBox=wx.TextCtrl(panel,-1,"",size=(400,-1))

        hbox2.Add(self.markFile, 0, wx.ALL , 5)

        hbox2.Add(self.FilePathTextBox, 0, wx.ALL , 5)

       

        #Define a text box to select desired size

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        self.pxText = wx.StaticText(panel, -1,'Desired size of the

longest picture side in pixels: ', pos=(300, 170), size=(321,30))

        self.px = wx.lib.intctrl.IntCtrl(panel, -1, value=1024,

pos=wx.Point(10, 90), size=(45, 30))

        hbox3.Add(self.pxText, 0, wx.ALL , 5)

        hbox3.Add(self.px, 0, wx.ALL , 0)

       

        #Define the button to start processing

        hbox4 = wx.BoxSizer(wx.HORIZONTAL)

        go = wx.Button(panel, -1, 'Go', size=(100, 30))

        hbox4.Add(go, 0, wx.ALL , 5)

        self.Bind(wx.EVT_BUTTON, self.WaterMark, go)

        #Console

        hbox5 = wx.BoxSizer(wx.HORIZONTAL)

        text = wx.TextCtrl(panel, -1,

style=wx.TE_MULTILINE|wx.TE_READONLY, size=(550, 200))

        self.text = text

        hbox5.Add(text, 0, wx.ALL , 5)

        #Merge sizers

        vbox1.Add(hbox1, 0, wx.ALIGN_LEFT | wx.ALL, 5)

        vbox1.Add(hbox2, 0, wx.ALIGN_LEFT | wx.ALL, 5)

        vbox1.Add(hbox3, 0, wx.ALIGN_LEFT | wx.ALL, 5)

        vbox1.Add(hbox4, 0, wx.ALIGN_RIGHT | wx.ALL, 5)

        vbox1.Add(hbox5, 0, wx.ALIGN_CENTER | wx.ALL, 5)

        panel.SetSizer(vbox1)

        self.Centre()

        self.Show(True)

    def OnQuit(self, event):

        self.Close()

    def OnAboutBox(self, event):

        info = wx.AboutDialogInfo()

        info.SetIcon(wx.Icon('icons/exit.png', wx.BITMAP_TYPE_PNG))

        info.SetName('Watermark Image Processing')

        info.SetVersion('1.0b')

        description = open('docs/info.txt').read()

        info.SetDescription(description)

        info.SetCopyright('(C) 2010 Acrocephalus Soft')

        info.SetWebSite('')
    license = open('docs/licence.txt').read()
    info.SetLicence(license)
    info.AddDeveloper('Daniel Valverde')
    info.AddDocWriter('Daniel Valverde')
    info.AddArtist('Daniel Valverde')
    info.AddTranslator('Daniel Valverde')
    wx.AboutBox(info)
def SelDir(self,event):         dialog = wx.DirDialog(None, "Select your picture

directory",style=wx.DD_DEFAULT_STYLE | wx.DD_CHANGE_DIR)
if dialog.ShowModal() == wx.ID_OK:
self.dirname=dialog.GetPath()
self.DirPathTextBox.write(self.dirname)
dialog.Destroy
def SelFile(self,event): markFile = wx.FileDialog(None, “Select your watermark
file”,style=wx.FD_OPEN, wildcard=“PNG files (.png)|.png”)
if markFile.ShowModal() == wx.ID_OK:
self.markFile=markFile.GetPath()
self.FilePathTextBox.write(self.markFile)
markFile.Destroy
def textoutput(self, text):
self.Refresh()
self.text.AppendText(text)
def WaterMark(self, event):
cwd = os.getcwd()
markFile=self.markFile
px = self.px.GetValue()
#Create a Backup directory
if not os.path.exists(‘Backup’):
os.mkdir(‘Backup’)
#Create a Resized directory
if not os.path.exists(‘Resized’):
os.mkdir(‘Resized’)
#Create a marked directory
if not os.path.exists(‘Watermarked’):
os.mkdir(‘Watermarked’)
#Copy images to backup folder
for file in fnmatch.filter(os.listdir(cwd),’.jpg’):
shutil.copy2(file, ‘Backup/%s’ % file)
self.textoutput(‘Copying ’ + file + ’ to Backup/\n’)
print ‘Copying ’ + file + ’ to Backup/\n’
#Image processing for file in fnmatch.filter(os.listdir(cwd),’
.jpg’):
self.textoutput(‘Resizing ’ + file + ‘\n’)
print ‘Resizing ’ + file + ‘\n’
# Open the image
img = Image.open(file)
#Get actual image size and convert it to float
width, height = img.size
width = float(width)
height = float(height)
#Resize landscape images
if width > height:
resizeFactor = width/int(px2)
img =
img.resize((int(width/resizeFactor),int(height/resizeFactor)))
width, height = img.size
img = img.resize((int(width/2),int(height/2)),
Image.ANTIALIAS)
#Resize portrait images
elif width<height:
resizeFactor = height/int(px
2)
img =
img.resize((int(width/resizeFactor),int(height/resizeFactor)))
width, height = img.size
img = img.resize((int(width/2),int(height/2)),
Image.ANTIALIAS)
#Resize square images
else:
img = img.resize((int(px2),int(px2)))
width, height = img.size
img = img.resize((int(px),int(px)), Image.ANTIALIAS)
#Save resized image
img.save(file)
#Copy pictures to a Resized directory
for filename in fnmatch.filter(os.listdir(cwd),’*.jpg’):
shutil.copy2(filename, ‘Resized/%s’ % filename)

···

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=enhttp://www.acrocephalus.net

http://www.sentex.net/


-- Daniel Valverde Saubí
c/Guillem Colteller 9 Baixos 3
17005 Girona
Spain
Cellular: +34651987662
e-mail: Si no és del tot necessari, no imprimeixis aquest missatge. Si ho fas utilitza paper 100% reciclat i blanquejat sense clor. D'aquesta manera ajudaràs a estalviar aigua, energia i recursos forestals. GRÀCIES!
Do not print this message unless it is absolutely necessary. If you must print it, please use 100% recycled paper whitened without chlorine. By doing so, you will save water, energy and forest resources. THANK YOU!

dani.valverde@gmail.comhttp://www.acrocephalus.nethttp://natupics.blogspot.com

Dani,

The problem is that you are getting stuck in one long procedure,

WaterMark, which is not giving your GUI a chance to update the
text, try restructuring so that you follow the pattern:

Make sure all your directories exist,

Get a list of files to watermark,

Inform the user you are starting on the first,

give the GUI some processing time,

process the file,

tell the user you have finished it

give the GUI some processing time,

repeat for all the files in the list.

inform the user that you have finished.

Consider using a progress dialogue for letting the user know what

is going on. I personally think you should save the files you are
working on to a resize directory first rather than last and make
less sue of shell commands.

Gadget/Steve
···

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en


-- Daniel Valverde Saubí
c/Guillem Colteller 9 Baixos 3
17005 Girona
Spain
Cellular: +34651987662
e-mail: Si no és del tot necessari, no imprimeixis aquest missatge. Si ho fas utilitza paper 100% reciclat i blanquejat sense clor. D'aquesta manera ajudaràs a estalviar aigua, energia i recursos forestals. GRÀCIES!
Do not print this message unless it is absolutely necessary. If you must print it, please use 100% recycled paper whitened without chlorine. By doing so, you will save water, energy and forest resources. THANK YOU!

dani.valverde@gmail.comhttp://www.acrocephalus.nethttp://natupics.blogspot.com
wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en

Hi!

Thank you so much Steve. I'll work on it.

Dani
···

wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en


-- Daniel Valverde Saubí
c/Guillem Colteller 9 Baixos 3
17005 Girona
Spain
Cellular: +34651987662
e-mail: Si no és del tot necessari, no imprimeixis aquest missatge. Si ho fas utilitza paper 100% reciclat i blanquejat sense clor. D'aquesta manera ajudaràs a estalviar aigua, energia i recursos forestals. GRÀCIES!
Do not print this message unless it is absolutely necessary. If you must print it, please use 100% recycled paper whitened without chlorine. By doing so, you will save water, energy and forest resources. THANK YOU!

dani.valverde@gmail.comhttp://www.acrocephalus.nethttp://natupics.blogspot.com
wxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=enwxPython-users+unsubscribe@googlegroups.com
http://groups.google.com/group/wxPython-users?hl=en


-- Daniel Valverde Saubí
c/Guillem Colteller 9 Baixos 3
17005 Girona
Spain
Cellular: +34651987662
e-mail: Si no és del tot necessari, no imprimeixis aquest missatge. Si ho fas utilitza paper 100% reciclat i blanquejat sense clor. D'aquesta manera ajudaràs a estalviar aigua, energia i recursos forestals. GRÀCIES!
Do not print this message unless it is absolutely necessary. If you must print it, please use 100% recycled paper whitened without chlorine. By doing so, you will save water, energy and forest resources. THANK YOU!

dani.valverde@gmail.comhttp://www.acrocephalus.nethttp://natupics.blogspot.com

Also take a look at LongRunningTasks - wxPyWiki

···

On 2/7/11 5:44 AM, Dani Valverde wrote:

On 02/07/2011 01:36 PM, Gadget/Steve wrote:

The problem is that you are getting stuck in one long procedure,
WaterMark, which is not giving your GUI a chance to update the text,
try restructuring so that you follow the pattern:
Make sure all your directories exist,
Get a list of files to watermark,
Inform the user you are starting on the first,
give the GUI some processing time,
process the file,
tell the user you have finished it
give the GUI some processing time,
repeat for all the files in the list.
inform the user that you have finished.

Consider using a progress dialogue for letting the user know what is
going on. I personally think you should save the files you are working
on to a resize directory first rather than last and make less sue of
shell commands.

Hi!
Thank you so much Steve. I'll work on it.

--
Robin Dunn
Software Craftsman