Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2001-04-05 07:44:48
Size: 615
Editor: anonymous
Comment: missing edit-log entry for this revision
Revision 11 as of 2001-06-08 07:35:13
Size: 3050
Editor: anonymous
Comment: missing edit-log entry for this revision
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Formats and Conversions == Formats and Conversions ==
Line 3: Line 3:
    Conversions among wxImage, wxBitmap, wxCursor, wxIcon     Conversions among wxImage, wxBitmap, wxCursor, wxIcon and DATA
Line 5: Line 5:
    Embedding images in code as strings, conversion to/from strings         wxImage to wxBitmap -- {{{ myWxImage.ConvertToBitmap() }}}
Line 7: Line 7:
    Using PIL with wxPython         wxImage to DATA -- {{{ myWxImage.GetData() }}} returning a string in width * height * 3 format
Line 9: Line 9:
    Animated Icons (are these possible in wxPython?)         DATA to wxImage -- {{{ image = wxImage(); image.SetData( data ) }}} where data is a Python string of length width * height * 3.
Line 11: Line 11:
Using dynamic bitmaps (wxBitmapFromData)         DATA to wxBitmap -- ???
Line 13: Line 13:
    Run-time generation of images         DATA to wxIcon -- Should be possible, but I don't see an overloaded-constructor name for it.

== Using dynamic bitmaps (wxBitmapFromData) ==
Line 17: Line 19:
Drawing Bitmaps to Screen/Printer Contexts === PIL (Python Imaging Library) ===

    PIL can be used with wxPython if image processing services are required beyond loading the formats built into wxPython. DATA formatted strings can be created from PIL Image objects using the .convert and .tostring methods (as show in the example below).

{{{
 def GetBitmap( self ):
  import Image
  source = Image.open( r"Z:\mcfport\portfoli\full\claire.jpg", 'r')
  image = apply( wxEmptyImage, source.size )
  image.SetData( source.convert( "RGB").tostring() )
  return image.ConvertToBitmap()
}}}

=== Numeric Python ===

    This example shows the creation of a bitmap using a Numeric Python array as the data source. Note that Numeric uses reversed column-row ordering compared to wxPython, so you'll need to make sure that you generate images using height, width, not width, height coordinates. Also note the user of the 'c' data type for image data in rgb format.

{{{
 def GetBitmap( self, width=32, height=32, colour = (0,0,0) ):
  array = Numeric.zeros( (height, width, 3),'c')
  array[:,:,] = colour
  image = wxEmptyImage(width,height)
  image.SetData( array.tostring())
  return image.ConvertToBitmap()
}}}

    This example shows use of Numeric's (extended) slicing notation to alter the in-memory image. This slicing notation, for our purposes, goes as follows:

    {{{ [ rowStart: rowEnd, columnStart: columnEnd, colourPlanes ] }}}

    To assign a single colour to the entire image: {{{ array[:,:] = (r,g,b) }}} . To assign a value to the entire red bit plane: {{{ array[:,:,0] = 255 }}} . To assign a colour to the first row: {{{ array[ 0 ] = (r,g,b ) }}} .

{{{
 def GetBitmap( self, width=32, height=32, colour = (128,128,128), border=5, borderColour=(255,255,255) ):
  array = Numeric.zeros( (height, width, 3),'c')
  array[border:-border,border:-border,:] = colour
  array[:border,:,:] = borderColour
  array[-border:,:,:] = borderColour
  array[:,:border,:] = borderColour
  array[:,-border:,:] = borderColour
  image = wxEmptyImage(width,height)
  image.SetData( array.tostring())
  return image.ConvertToBitmap()
}}}


== Drawing Bitmaps to Screen/Printer Contexts ==
Line 21: Line 69:
Masks and Transparency/Alpha == Masks and Transparency/Alpha ==

Formats and Conversions

  • Conversions among wxImage, wxBitmap, wxCursor, wxIcon and DATA
    • wxImage to wxBitmap --  myWxImage.ConvertToBitmap() 

      wxImage to DATA --  myWxImage.GetData()  returning a string in width * height * 3 format

      DATA to wxImage --  image = wxImage(); image.SetData( data )  where data is a Python string of length width * height * 3. DATA to wxBitmap -- ??? DATA to wxIcon -- Should be possible, but I don't see an overloaded-constructor name for it.

Using dynamic bitmaps (wxBitmapFromData)

  • Using device contexts to write into wxBitmaps

PIL (Python Imaging Library)

  • PIL can be used with wxPython if image processing services are required beyond loading the formats built into wxPython. DATA formatted strings can be created from PIL Image objects using the .convert and .tostring methods (as show in the example below).

        def GetBitmap( self ):
                import Image
                source = Image.open( r"Z:\mcfport\portfoli\full\claire.jpg", 'r')
                image = apply( wxEmptyImage, source.size )
                image.SetData( source.convert( "RGB").tostring() )
                return image.ConvertToBitmap()

Numeric Python

  • This example shows the creation of a bitmap using a Numeric Python array as the data source. Note that Numeric uses reversed column-row ordering compared to wxPython, so you'll need to make sure that you generate images using height, width, not width, height coordinates. Also note the user of the 'c' data type for image data in rgb format.

        def GetBitmap( self, width=32, height=32, colour = (0,0,0) ):
                array = Numeric.zeros( (height, width, 3),'c')
                array[:,:,] = colour
                image = wxEmptyImage(width,height)
                image.SetData( array.tostring())
                return image.ConvertToBitmap()
  • This example shows use of Numeric's (extended) slicing notation to alter the in-memory image. This slicing notation, for our purposes, goes as follows:

     [ rowStart: rowEnd, columnStart: columnEnd, colourPlanes ] 

    To assign a single colour to the entire image:  array[:,:] = (r,g,b)  . To assign a value to the entire red bit plane:  array[:,:,0] = 255  . To assign a colour to the first row:  array[ 0 ] = (r,g,b )  .

        def GetBitmap( self, width=32, height=32, colour = (128,128,128), border=5, borderColour=(255,255,255) ):
                array = Numeric.zeros( (height, width, 3),'c')
                array[border:-border,border:-border,:] = colour
                array[:border,:,:] = borderColour
                array[-border:,:,:] = borderColour
                array[:,:border,:] = borderColour
                array[:,-border:,:] = borderColour
                image = wxEmptyImage(width,height)
                image.SetData( array.tostring())
                return image.ConvertToBitmap()

Drawing Bitmaps to Screen/Printer Contexts

  • This is basically covered in the demo, but it's part of the topic.

Masks and Transparency/Alpha

  • Bitmap masks (from the demo) Alpha-blending (is this even possible?)

WorkingWithImages (last edited 2011-06-20 15:32:02 by pool-71-244-98-82)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.