Text to Bitmap

Hello,

I got this piece of code from wiki at wxpython. It is used to convert text to bitmap. The problem is i do not know how to run it or use it.
I request any one who can help.

====================== CODE =========================

from wxPython.wx import *
def _setupContext( memory, font=None, color=None ):
        if font:
                memory.SetFont( font )
        else:
                memory.SetFont( wxNullFont )
        if color:

                memory.SetTextForeground( color )
def write( text, bitmap, pos=(0,0), font=None, color=None):
        """Simple write into a bitmap doesn't do any checking."""
        memory = wx.MemoryDC( )

        _setupContext( memory, font, color )
        memory.SelectObject( bitmap )
        try:
                memory.DrawText( text, pos[0],pos[1],)
        finally:
                memory.SelectObject( wxNullBitmap)

        return bitmap

====================== CODE =========================

Thanks

···


Regards,
Tanay Shah

As far as I understand, you've got a function to write some text into a
bitmap. You're supposed to use it like this:
* creating a bitmap (a file of the type Bitmap; for example,
  EmptyBitmap(500, 200))
* passing this bitmap and some text you want to write into it to the
  function as parameters

The function will return a Bitmap with the text written into it. You can
do with it whatever you want. You can show it on screen using
wx.PaintDC's DrawBitmap() method, or you can convert it to an image, for
example.

···

On Fri, Apr 17, 2009 at 01:38:21PM +0530, Tanay Shah wrote:

Hello,

I got this piece of code from wiki at wxpython. It is used to convert
text to bitmap. The problem is i do not know how to run it or use it.
I request any one who can help.

--
Pablo Antonio (AKA crazy2k)
http://www.pablo-a.com.ar/

Thanks for the reply.

Here’s what i’m doing now:

================= CODE ==================

#Boa:Frame:Frame1

import wx
import wx, numpy

def create(parent):
return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL1, wxID_FRAME1STATICBITMAP1,
wxID_FRAME1TEXTCTRL1,
] = [wx.NewId() for _init_ctrls in range(5)]

class Frame1(wx.Frame):
def _init_ctrls(self, prnt):
# generated method, don’t edit
wx.Frame.init(self, id=wxID_FRAME1, name=‘’, parent=prnt,
pos=wx.Point(296, 164), size=wx.Size(683, 445),
style=wx.DEFAULT_FRAME_STYLE, title=‘Frame1’)
self.SetClientSize(wx.Size(683, 445))

    self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
          pos=wx.Point(0, 0), size=wx.Size(683, 445),
          style=wx.TAB_TRAVERSAL)

    self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
          parent=self.panel1, pos=wx.Point(32, 88), size=wx.Size(232, 88),
          style=0, value='textCtrl1')

    self.staticBitmap1 = wx.StaticBitmap(bitmap=wx.NullBitmap,
          id=wxID_FRAME1STATICBITMAP1, name='staticBitmap1',
          parent=self.panel1, pos=wx.Point(448, 96), size=wx.Size(176, 96),
          style=0)

    self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1',
          name='button1', parent=self.panel1, pos=wx.Point(112, 232),
          size=wx.Size(85, 34), style=0)
    self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
          id=wxID_FRAME1BUTTON1)

def __init__(self, parent):
    self._init_ctrls(parent)
   
    
def setupContext( memory, font=None, color=None ):
    if font:
            memory.SetFont( font )
    else:
            memory.SetFont( wxNullFont )
    if color:
            memory.SetTextForeground( color )
           
def write(  text, bitmap, pos=(0,0), font=None, color=None):
    """Simple write into a bitmap doesn't do any checking."""
    memory = wx.MemoryDC( )
    #self.setupContext( memory, font, color )
    memory.SelectObject( bitmap )
    try:
            memory.DrawText( text, pos[0],pos[1],)
    finally:
            memory.SelectObject( wxNullBitmap)
    return bitmap

def OnButton1Button(self, event):
    t = self.textCtrl1.GetValue()
    p = wx.EmptyBitmap(50,50)
    self.write(t,p)    
    self.staticBitmap1.SetBitmap(p)
   
def GetBitmap( self, width=32, height=32, colour = (0,0,0) ):
    array = numpy.zeros( (height, width, 3),'uint8')
    array[:,:,] = colour
    image = wx.EmptyImage(width,height)
    image.SetData( array.tostring())
    return image.ConvertToBitmap() # wx.BitmapFromImage(image)

if name == ‘main’:
app = wx.PySimpleApp()
frame = create(None)
frame.Show()

app.MainLoop()
···

=============================================

When I run it, it gives an error saying - SelectObject requires argument of type wxBitmap.
I have already passed an empty bitmap.

Can you help with where I’m going wrong ?

Also, I’ve commented out the setupContext() line as it gave me another error. I dont think it is required as of now.

On Fri, Apr 17, 2009 at 1:51 PM, Pablo Antonio pabloa@gmail.com wrote:

On Fri, Apr 17, 2009 at 01:38:21PM +0530, Tanay Shah wrote:

Hello,

I got this piece of code from wiki at wxpython. It is used to convert

text to bitmap. The problem is i do not know how to run it or use it.

I request any one who can help.

As far as I understand, you’ve got a function to write some text into a

bitmap. You’re supposed to use it like this:

  • creating a bitmap (a file of the type Bitmap; for example,

    EmptyBitmap(500, 200))

  • passing this bitmap and some text you want to write into it to the

    function as parameters

The function will return a Bitmap with the text written into it. You can

do with it whatever you want. You can show it on screen using

wx.PaintDC’s DrawBitmap() method, or you can convert it to an image, for

example.

Pablo Antonio (AKA crazy2k)

http://www.pablo-a.com.ar/


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


Tanay Shah

Okay. solved it.

Just had to add one argument in Write( self, text, bitmap,…)

···

On Fri, Apr 17, 2009 at 1:51 PM, Pablo Antonio pabloa@gmail.com wrote:

On Fri, Apr 17, 2009 at 01:38:21PM +0530, Tanay Shah wrote:

Hello,

I got this piece of code from wiki at wxpython. It is used to convert

text to bitmap. The problem is i do not know how to run it or use it.

I request any one who can help.

As far as I understand, you’ve got a function to write some text into a

bitmap. You’re supposed to use it like this:

  • creating a bitmap (a file of the type Bitmap; for example,

    EmptyBitmap(500, 200))

  • passing this bitmap and some text you want to write into it to the

    function as parameters

The function will return a Bitmap with the text written into it. You can

do with it whatever you want. You can show it on screen using

wx.PaintDC’s DrawBitmap() method, or you can convert it to an image, for

example.

Pablo Antonio (AKA crazy2k)

http://www.pablo-a.com.ar/


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


Tanay Shah