Table of Contents:

Converting Between PIL image, wx.Bitmap and wx.Image

   1 # Tested with wxPython 2.3.4.2 and PIL 1.1.3.
   2 import wx
   3 import Image             # PIL module. Only if you use the PIL library.
   4 
   5 def WxBitmapToPilImage( myBitmap ) :
   6     return WxImageToPilImage( WxBitmapToWxImage( myBitmap ) )
   7 
   8 def WxBitmapToWxImage( myBitmap ) :
   9     return wx.ImageFromBitmap( myBitmap )
  10 
  11 #-----
  12 
  13 def PilImageToWxBitmap( myPilImage ) :
  14     return WxImageToWxBitmap( PilImageToWxImage( myPilImage ) )
  15 
  16 def PilImageToWxImage( myPilImage ):
  17     myWxImage = wx.EmptyImage( myPilImage.size[0], myPilImage.size[1] )
  18     myWxImage.SetData( myPilImage.convert( 'RGB' ).tostring() )
  19     return myWxImage
  20 
  21 # Or, if you want to copy any alpha channel, too (available since wxPython 2.5)
  22 # The source PIL image doesn't need to have alpha to use this routine.
  23 # But, a PIL image with alpha is necessary to get a wx.Image with alpha.
  24 
  25 def PilImageToWxImage( myPilImage, copyAlpha=True ) :
  26 
  27     hasAlpha = myPilImage.mode[ -1 ] == 'A'
  28     if copyAlpha and hasAlpha :  # Make sure there is an alpha layer copy.
  29 
  30         myWxImage = wx.EmptyImage( *myPilImage.size )
  31         myPilImageCopyRGBA = myPilImage.copy()
  32         myPilImageCopyRGB = myPilImageCopyRGBA.convert( 'RGB' )    # RGBA --> RGB
  33         myPilImageRgbData =myPilImageCopyRGB.tostring()
  34         myWxImage.SetData( myPilImageRgbData )
  35         myWxImage.SetAlphaData( myPilImageCopyRGBA.tostring()[3::4] )  # Create layer and insert alpha values.
  36 
  37     else :    # The resulting image will not have alpha.
  38 
  39         myWxImage = wx.EmptyImage( *myPilImage.size )
  40         myPilImageCopy = myPilImage.copy()
  41         myPilImageCopyRGB = myPilImageCopy.convert( 'RGB' )    # Discard any alpha from the PIL image.
  42         myPilImageRgbData =myPilImageCopyRGB.tostring()
  43         myWxImage.SetData( myPilImageRgbData )
  44 
  45     return myWxImage
  46 
  47 #-----
  48 
  49 def imageToPil( myWxImage ):
  50     myPilImage = Image.new( 'RGB', (myWxImage.GetWidth(), myWxImage.GetHeight()) )
  51     myPilImage.fromstring( myWxImage.GetData() )
  52     return myPilImage
  53 
  54 def WxImageToWxBitmap( myWxImage ) :
  55     return myWxImage.ConvertToBitmap()

But, wait ... There's more ! There are two kinds of transparency that a wx.Image or wx.Bitmap can have: 1) Every pixel is either completely transparent or completely opaque. This binary transparency is a transparency mask. 2) Each pixel can have a variable amount of transparency that is from completely transparent (alpha value=0) to completely opaque (value=255). The amount of transparency each pixel has is defined by a value from 0 to 255. This is alpha transparency. For example, a GIF file may have a transparency mask, or no transparency at all. A PNG file, however, may have a transparency mask, alpha transparency or neither. In comparison, JPG files cannot have any kind of transparency.

Reading Image Files

Creating a wx.Bitmap from an image file :

    wxBmap = wx.EmptyBitmap( 1, 1 )     # Create a bitmap container object. The size values are dummies.
    wxBmap.LoadFile( filename, wx.BITMAP_TYPE_ANY )   # Load it with a file image.

Determine whether a bitmap has a mask or alpha transparency :

    bmapHasMask  = wxBmap.GetMask()    # "GetMask()", NOT "HasMask()" !
    bmapHasAlpha = wxBmap.HasAlpha()

Creating a wx.Image from an image file is just like that for a wx.Bitmap :

    wxImg = wx.EmptyBitmap( 1, 1 )     # Create a bitmap container
    wxImg.LoadFile( filename, wx.BITMAP_TYPE_ANY )

To determine whether the bitmap has a mask or alpha transparency :

    imgHasMask  = wxImg.HasMask()    # "HasMask()", NOT "GetMask()" !  Egads ...
    imgHasAlpha = wxImg.HasAlpha()

Reading an image file into a PIL Image :

    import Image
    pilImage = Image.open( filename )

The PIL open() function automatically determines the file's image type. wxPython will not allow you to drop the "wx.BITMAP_TYPE_ANY" parameter even though it's not needed. Remember that PIL image transparency can only be the alpha kind. To check if a PIL image has transparency :

    pilImageHasAlpha = pilImg.mode[ -1 ] == 'A'

}When PIL reads in a PNG file with a mask, it automatically converts the mask into binary-valued alpha transparency whose values are either 0 for completely transparent or 255 completely opaque. This is usually not what is wanted.

Converting Binary-Valued Alpha Transparency into True (256 values) Alpha Transparency

PIL image transparency can only be the true, 256-valued alpha kind, but a wx.Bitmap and a wx.Image can have either kind. To convert a wx.Image transparency mask in to true alpha transparency :

    imgHasMask  = wxImg.HasMask()
    if wxImg.HasMask() :
        wxImg.InitAlpha()

To handle all the various image type conversions while also properly copying any transparency the following ImgConv.py module can be used. Make the calls with any flag parameters at their default values, that is, don't even include them in the call parameter list. If you start with a wx.Bitmap or a wx.Image that has either a transparency mask or alpha transparency, these routines will carry over that transparency to a PIL image with alpha. A transparency mask will always be converted into alpha transparency. PIL images can have only have alpha for transparency, never masks. So, when this package converts a PIL image with transparency, the resulting wx.Image or wx.Bitmap will have alpha transparency, not a mask.

You can use the flag parameters addAlphaLayer and delAlphaLayer to optionally : 1) Create an image with a alpha transparency when a source image had none (addAlphaLayer=True), or conversely, 2) Prevent alpha from automatically being created in the new image when the source image does has alpha (delAlphaLayer=True).

ImgConv.py Win32IconImagePlugin.py

This module contains six conversion functions. By default it automatically preserves any transparency that is present in the input image. This is usually what is wanted when displaying them in an app.

WxBitmapFromPilImage()
WxImageFromPilImage()

WxBitmapFromWxImage()
PilImageFromWxImage()

WxImageFromWxBitmap()
PilImageFromWxBitmap()

The BitmapManip.py module has utility functions for extracting and combining masks useful for advanced image manipulation. Wx.Frames can be created that are free-form non-rectangular with semi-transparent background images overlaid with completely opaque and dynamically changing text.

BitmapManip.py

Conversions Between wx.Image, wx.Bitmap, wx.Cursor, wx.Icon and String Data

wx.Image to wx.Bitmap :

    myWxBmap = myWxImg.ConvertToBitmap()

or :

    myWxBmap = wxBitmapFromImage( myWxImg )

wxImage to String Data :

    myStrData = myWxImg.GetData()

This returns a binary data Python string of of length (width * height * 3).

Convert String Data to a wx.Image :

wxImg = wx.Image()
wxImg.SetData( myStrData )

Python string of binary data to a wx.Bitmap : Go through wx.Image to get a wx.Bitmap.

DATA to a wx.Icon : Should be possible, but I don't see an overloaded-constructor name for it.

wx.Icon to wxBitmap :

myWxBitmap = wx.EmptyBitmap( myIcon.GetWidth(), myIcon.GetHeight() )
myWxBitmap.CopyFromIcon( myIcon )

wxBitmap to string data :

Python string of binary data or wx.Image to wxCursor :

wx.Icon to Python string of binary data or wx.Image :

wxCursor to Python string of binary data or wxImage :

Again, to allow for editing cursors. Would also need methods to get the current hot spot. wxImage to wxIcon -- See DATA to wxIcon above, either one would be fine, but would be nice to have the complete set.

PIL (Python Imaging Library)

   1 import PIL.Image    # The fundamental PIL library module.
   2 import Win32IconImagePlugin     # Necessary for PIL to properly read any  transparency in .ICO files.
   3 
   4 import wx
   5 
   6 #------------------------------------------------------------------------------
   7 
   8 def BitmapFromFile( imgFilename ) :
   9     """ The following PIL image conversion must first go to a wx.Image.
  10     The wx.Image is always finally converted to a wx.Bitmap regardless of whether or not
  11     there is any image transparency information to be handled.
  12     This is because only a wxBitmap can be directly displayed - a wxImage can't !
  13 
  14     The module Win32IconImagePlugin.py must be imported to get PIL to properly read
  15     paletted images with a mask (binary valued transparency). All .ICO and some .PNG files
  16     may have paletted image data with mask transparency. See:
  17 
  18     Win32IconImagePlugin - Alternate PIL plugin for dealing with Microsoft .ico files.
  19     http://code.google.com/p/casadebender/wiki/Win32IconImagePlugin
  20     """
  21     pilImg = PIL.Image.open( imgFilename )
  22 
  23     # The following is equivalent to "wxImg = wx.EmptyImage( pilImg.size[0], pilImg.size[1] )".
  24     wxImg = wx.EmptyImage( *pilImg.size )   # Always created with no transparency plane.
  25 
  26     # Determine if the image file has any inherent transparency.
  27     pilMode = pilImg.mode     # Will usually be either "RGB" or "RGBA", but may be others.
  28     pilHasAlpha = pilImg.mode[-1] == 'A'
  29     if pilHasAlpha :
  30 
  31         # First extract just the RGB data from the data string and insert it into wx.Image .
  32         pilRgbStr = pilImg.convert( 'RGB').tostring()
  33         wxImg.SetData( pilRgbStr )
  34 
  35         # To convert to a wx.Image with alpha the pilImg mode needs to be "RGBA".
  36         # So, add an alpha layer even if the original file image doesn't have any transparency info.
  37         # If the file image doesn't have any transparency, the resulting wx.Image (and, finally, the wx.Bitmap)
  38         # will be 100% opaque just like the file image.
  39         pilImgStr = pilImg.convert( 'RGBA' ).tostring()    # Harmless if original image mode is already "RGBA".
  40 
  41         # Now, extract just the alpha data and insert it.
  42         pilAlphaStr = pilImgStr[3::4]    # start at byte index 3 with a stride (byte skip) of 4.
  43         wxImg.SetAlphaData( pilAlphaStr )
  44 
  45     #end if
  46 
  47     wxBmap = wxImg.ConvertToBitmap()     # Equivalent result:   wxBmap = wx.BitmapFromImage( wxImg )
  48     return wxBmap
  49 
  50 #end def

Notes: If a GIF file with a mask is read, PIL will automatically convert the mask to binary-valued alpha transparency. This is usually suitable for display purposes. Neither wx nor PIL can properly write a .ICO file with mask transparency. This is probably fine since it's better to write all non-photographic image files in .PNG format.

NumPy

   1 import wx, numpy
   2 
   3 def GetBitmap( self, width=32, height=32, colour = (0,0,0) ):
   4         array = numpy.zeros( (height, width, 3),'uint8')
   5         array[:,:,] = colour
   6         image = wx.EmptyImage(width,height)
   7         image.SetData( array.tostring())
   8         wxBitmap = image.ConvertToBitmap()       # OR:  wx.BitmapFromImage(image)
   9         return wxBitmap

Slicing

NumPy's slicing notation, for our purposes, works 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 ) 

Examples

This example shows use of NumPy's (extended) slicing notation to alter an in-memory image. Note that the slice notation allows for standard negative-value index semantics. Also note the accommodating nature of the assignment operation. Assigning a three-tuple to a row sets every value in the row, while assigning a list of three-tuples will set each value individually (and raise an error if the number of items is incorrect).

   1 import wx, numpy
   2 
   3 def GetBitmap( self, width=32, height=32, colour=(128, 128, 128), border=5, borderColour=(255, 255, 255) ):
   4     """
   5     Creates a bitmap with a border.
   6     """
   7     array = numpy.zeros( (height, width, 3), 'uint8' )
   8     array[ border:-border, border:-border, : ] = colour
   9     array[ :border, :, : ] = borderColour
  10     array[ -border:, :, : ] = borderColour
  11     array[ :, :border, :] = borderColour
  12     array[ :, -border:, : ] = borderColour
  13     image = wx.EmptyImage( width, height )
  14     image.SetData( array.tostring() )
  15     return image.ConvertToBitmap()    # OR:  wx.BitmapFromImage( image )

This example creates a horizontal gradient between two colours. Note that NumPy is automatically converting from float to uint8 data type in the assignment to array, because of the slice indexing. Without the indexing, array would just get overwritten to point to a different numpy array instead of assigning data within the existing one.

   1 import numpy as np
   2 
   3 def GetBitmap( self, width=640, height=480, leftColour=(255,128,0), rightColour=(64,0,255) ):
   4         """
   5         Create a horizontal gradient
   6         """
   7         array = np.zeros( (height, width, 3), 'uint8' )
   8 
   9         # alpha is a one dimensional array with a linear gradient from 0.0 to 1.0
  10         alpha = np.linspace( 0., 1., width )
  11 
  12         # This uses alpha to linearly interpolate between leftColour and rightColour
  13         colourGradient = np.outer(alpha, leftColour) + np.outer( (1.-alpha), rightColour )
  14 
  15         # NumPy's broadcasting rules will assign colourGradient to every row of the destination array
  16         array[ :, :, : ] = colourGradient
  17         image = wx.EmptyImage( width, height )
  18         image.SetData( array.tostring() )
  19         wxBitmap = wx.BitmapFromImage( image )    # OR:  image.ConvertToBitmap()
  20         return wxBitmap

Screen Capture

This example captures the client area of a frame to a wxBitmap object (and from there, a file). Note that there is no error checking done here. Should likely use Ok() or something to check each context. I'm not sure what would happen if this were called before the first painting of the screen, likely the context would have the desktop or something in it. Note: code below only captures the client area of the window. Tagore notes that you can get the whole window with a wx.WindowDC and arbitrary parts of the screen with a wx.ScreenDC.

   1 def OnSaveToFile( self, event ):
   2         context = wx.ClientDC( self )
   3         memory = wx.MemoryDC( )
   4         x, y = self.ClientSize
   5         bitmap = wx.EmptyBitmap( x, y, -1 )
   6         memory.SelectObject( bitmap )
   7         memory.Blit( 0, 0, x, y, context, 0, 0)
   8         memory.SelectObject( wx.NullBitmap)
   9         bitmap.SaveFile( 'test.bmp', wx.BITMAP_TYPE_BMP )

A Flexible Screen Capture App

Here's a general screen capture module and a demo app that first captures the whole primary screen, then captures 4 smaller portions of it.

ScreenShotWX.py

ScreenShotWX_Demo.py

   1 def ScreenCapture( captureStartPos, captureBmapSize, debug=False ):
   2     """
   3     General Desktop screen portion capture - partial or entire Desktop.
   4 
   5     My particular screen hardware configuration:
   6         wx.Display( 0 ) refers to the primary  Desktop display monitor screen.
   7         wx.Display( 1 ) refers to the extended Desktop display monitor screen (above the primary screen).
   8 
   9     Any particular Desktop screen size is :
  10         screenRect = wx.Display( n ).GetGeometry()
  11 
  12     Different wx.Display's in a single system may have different dimensions.
  13     """
  14 
  15     # A wx.ScreenDC provides access to the entire Desktop.
  16     # This includes any extended Desktop monitor screens that are enabled in the OS.
  17     scrDC = wx.ScreenDC()         # MSW HAS BUG:  DOES NOT INCLUDE ANY EXTENDED SCREENS.
  18     scrDcSize = scrDC.Size
  19     scrDcSizeX, scrDcSizeY = scrDcSize
  20 
  21     # Cross-platform adaptations :
  22     scrDcBmap     = scrDC.GetAsBitmap()
  23     scrDcBmapSize = scrDcBmap.GetSize()
  24     if debug :
  25         print 'DEBUG:  Size of scrDC.GetAsBitmap() ', scrDcBmapSize
  26 
  27     # Check if scrDC.GetAsBitmap() method has been implemented on this platform.
  28     if   not scrDcBmap.IsOk() :      # Not implemented :  Get the screen bitmap the long way.
  29 
  30         if debug :
  31             print 'DEBUG:  Using memDC.Blit() since scrDC.GetAsBitmap() is nonfunctional.'
  32 
  33         # Create a new empty (black) destination bitmap the size of the scrDC.
  34         scrDcBmap = wx.EmptyBitmap( *scrDcSize )    # Overwrire the invalid original assignment.
  35         scrDcBmapSizeX, scrDcBmapSizeY = scrDcSize
  36 
  37         # Create a DC tool that is associated with scrDcBmap.
  38         memDC = wx.MemoryDC( scrDcBmap )
  39 
  40         # Copy (blit, "Block Level Transfer") a portion of the screen bitmap
  41         #   into the returned capture bitmap.
  42         # The bitmap associated with memDC (scrDcBmap) is the blit destination.
  43 
  44         memDC.Blit( 0, 0,                           # Copy to this start coordinate.
  45                     scrDcBmapSizeX, scrDcBmapSizeY, # Copy an area this size.
  46                     scrDC,                          # Copy from this DC's bitmap.
  47                     0, 0,                    )      # Copy from this start coordinate.
  48 
  49         memDC.SelectObject( wx.NullBitmap )     # Finish using this wx.MemoryDC.
  50                                                 # Release scrDcBmap for other uses.
  51     else :
  52 
  53         if debug :
  54             print 'DEBUG:  Using scrDC.GetAsBitmap()'
  55 
  56         # This platform has scrDC.GetAsBitmap() implemented.
  57         scrDcBmap = scrDC.GetAsBitmap()     # So easy !  Copy the entire Desktop bitmap.
  58 
  59         if debug :
  60             print 'DEBUG:  scrDcBmap.GetSize() ', scrDcBmap.GetSize()
  61 
  62     #end if
  63 
  64     return scrDcBmap.GetSubBitmap( wx.RectPS( captureStartPos, captureBmapSize ) )
  65 
  66 #end ScreenCapture def

In the application all five captured wxBitmap objects are saved to PNG files.

The wx.ScreenDC treats the entire Desktop screen as a single seamless conglomerate of all exiting wx.Display() areas. Negative coordinate values relative to the main Desktop screen are allowed. Negative ordinate values must be used to capture any portion of an extension monitor screen to the left of or above the primary monitor screen (the one with the Taskbar). For example, I have two monitors where the extension monitor is configured to be directly above the main monitor. I have set both monitors to 1280x800 resolution. To capture the just the whole extended screen :

   1     screenWid = wx.SystemSettings.GetMetric( wx.SYS_SCREEN_X )
   2     screenHgt = wx.SystemSettings.GetMetric( wx.SYS_SCREEN_Y )
   3     captureSize = (screenWid, screenHgt*2)  # Both screens are set to be the same size.
   4     wxBitmap = ScreenCapture( startPos, captureSize )

Other wxPython add-on packages can take arbitrarily sized screen shots, too, such as ImageMagick and PIL. Python Imaging Library:

DesktopScreenShotPIL.py -- Ray Pasco ( AKA WinCrazy )

Write Text to a Bitmap

Writing text into a bitmap is done by creating a context then using the context's DrawText method to do the actual drawing. A simple example:

   1 import wx
   2 
   3 def SetDcContext( memDC, font=None, color=None ):
   4     if font:
   5         memDC.SetFont( font )
   6     else:
   7         memDC.SetFont( wx.NullFont )
   8 
   9     if color:
  10         memDC.SetTextForeground( color )
  11 
  12 #end def
  13 
  14 def WriteTextOnBitmap( text, bitmap, pos=(0, 0), font=None, color=None) :
  15     """
  16     Simple write into a bitmap doesn't do any checking.
  17     """
  18     memDC = wx.MemoryDC()
  19     SetDcContext( memDC, font, color )
  20     memDC.SelectObject( bitmap )
  21     try:
  22         memDC.DrawText( text, pos[0], pos[1])
  23     except :
  24         pass
  25 
  26     memDC.SelectObject( wx.NullBitmap )
  27 
  28     return bitmap
  29 
  30 #end def

   1 import wx
   2 
   3 MINIMUMFONTSIZE = 4
   4 
   5 def WriteCaptionOnBitmap( text, bitmap, font=None, margins = (2,2), color=None ):
   6     """
   7     Write the given caption (text) into the bitmap
   8     using the font (or default if not given) with the
   9     margins given.  Will try to make sure the text fits
  10     into the bitmap.
  11     """
  12     memory = wx.MemoryDC( )
  13     font = font or memory.GetFont()
  14     textLines = text.split( '\n' )
  15     fit = False
  16     while not fit:
  17         totalWidth=0
  18         totalHeight = 0
  19         setLines = []
  20         for line in textLines:
  21             if line and line[-1] == '\r':
  22                 line = line[:-1]
  23 
  24             width, height = extents = memory.GetTextExtent( line )
  25             totalWidth = max( totalWidth,  width )
  26             totalHeight += height
  27             setLines.append( (line, extents))
  28         #end for
  29 
  30         if (totalWidth > (bitmap.GetWidth()- 2*margins[0])  or  \
  31            (totalHeight > (bitmap.GetHeight()- 2*margins[0]) ) :
  32 
  33             size = font.GetPointSize() - 1
  34             if size < MINIMUMFONTSIZE:
  35                 fit = True # will overdraw!!!
  36             else:
  37                 font.SetPointSize( size )
  38                 memory.SetFont( font )
  39         else:
  40             fit = True
  41         #end if
  42 
  43     #end while
  44 
  45     if not setLines:
  46         return bitmap
  47 
  48     centreX, centreY = (bitmap.GetWidth()/2), (bitmap.GetHeight()/2)
  49     x, y = centreX-(totalWidth/2), centreY-(totalHeight/2)
  50     memory.SelectObject( bitmap )
  51     SetMemDcContext( memory, font, color)
  52     for line, (deltaX, deltaY) in setLines:
  53             x = centreX - (deltaX/2)
  54             memory.DrawText( line, x, y,)
  55             y += deltaY
  56     memory.SelectObject( wxNullBitmap)
  57     return bitmap
  58 
  59 
  60 def SetMemDcContext( memory, font=None, color=None ) :
  61 
  62     if font:
  63         memory.SetFont( font )
  64     else:
  65         memory.SetFont( wx.NullFont )
  66 
  67     if color:
  68         memory.SetTextForeground( color )
  69 
  70 #end def

Since transparency is a bit tricky, here is another example. (Which unlike the above, works for me).

In this example two bitmaps are created. One is completely white (oldPix) the other one completely black (newPix). Then a red box is drawn on the black bitmap. Now the black color in newPix is masked and then newPix is copied over oldPix (the white one). The result is, that only the red box is copied.

   1 import wx
   2 
   3 class Test(wx.App):
   4     def OnInit(self):
   5         oldPix  = wx.EmptyBitmap(300, 300)
   6         newPix  = wx.EmptyBitmap(300, 300)
   7         mem = wx.MemoryDC()
   8         mem.SelectObject(oldPix)
   9         mem.Clear()                       # The images have to be cleared
  10         mem.SelectObject(newPix)          # because wxEmptyBitmap only
  11         mem.SetBackground(wx.BLACK_BRUSH)  # allocates the space
  12         mem.Clear()
  13 
  14         # We now have a black and a white image
  15         # Next we plot a white box in the middle of the black image
  16 
  17         mem.SetPen(wx.RED_PEN)
  18         mem.DrawLines(((100, 200), (100, 100), (200, 100), (200,200), (100,200)))
  19 
  20         mem.SelectObject(oldPix)
  21         newPix.SetMask(wxMaskColour(newPix, wx.BLACK))
  22         mem.DrawBitmap(newPix, 0, 0, 1)
  23 
  24         oldPix.SaveFile("oldPix.bmp", wx.BITMAP_TYPE_BMP)
  25         newPix.SaveFile("newPix.bmp", wx.BITMAP_TYPE_BMP)
  26         return True
  27 
  28 app = Test(redirect=False)
  29 app.MainLoop()

This is a complete wxPython app, so just run it and have a look at the created Bitmaps oldPix.bmp and newPix.bmp for the results. -- Nikolai Hlubek

PythonMagick (a 16-bit Imaging Library)

Here is a short but complete PythonMagick/wxPython program. The program uses wxPython GUI, loads an image, displays the image, and does a simple image processing operation (threshold). Various conversions between PythonMagick, PIL, and wxPython images are available on the PythonMagick web site.

See the PythonMagick page for a greater description of PythonMagick.

Note that PythonMagick presently only has a Windows installer.

   1 from wxPython import wx
   2 import PythonMagick
   3 
   4 ID_FILE_OPEN = wx.wxNewId()
   5 ID_FILE_EXIT  = wx.wxNewId()
   6 ID_THRESHOLD = wx.wxNewId()
   7 
   8 class ImagePanel(wx.wxPanel):
   9     def __init__(self, parent, id):
  10         wx.wxPanel.__init__(self, parent, id)
  11         self.image = None  # wxPython image
  12         wx.EVT_PAINT(self, self.OnPaint)
  13 
  14     def display(self, magickimage):
  15         self.image = self.convertMGtoWX(magickimage)
  16         self.Refresh(True)
  17 
  18     def OnPaint(self, evt):
  19         dc = wx.wxPaintDC(self)
  20         if self.image:
  21             dc.DrawBitmap(self.image.ConvertToBitmap(), 0,0)
  22 
  23     def convertMGtoWX(self, magickimage):
  24         img = PythonMagick.Image(magickimage)  # make copy
  25         img.depth = 8        #  change depth only for display
  26         img.magick = "RGB"
  27         data = img.data
  28         wximg = wx.wxEmptyImage(img.columns(), img.rows())
  29         wximg.SetData(data)
  30         return wximg
  31 
  32 
  33 class mtFrame(wx.wxFrame):
  34     def __init__(self, parent, ID, title):
  35         wx.wxFrame.__init__(self, parent, ID, title, wx.wxDefaultPosition, wx.wxSize(500, 400))
  36 
  37         self.iPanel = ImagePanel(self, -1)
  38         self.im = None  # Magick image
  39 
  40         ## Construct "File" menu
  41         self.menuBar = wx.wxMenuBar()
  42         self.menuFile = wx.wxMenu()
  43         self.menuFile.Append(ID_FILE_OPEN, "&Open image","")
  44         wx.EVT_MENU(self, ID_FILE_OPEN, self.OnOpen)
  45         self.menuFile.AppendSeparator()
  46         self.menuFile.Append(ID_FILE_EXIT, "E&xit", "")
  47         wx.EVT_MENU(self, ID_FILE_EXIT,  self.OnExit)
  48         self.menuBar.Append(self.menuFile, "&File");
  49 
  50         ## Construct "Process" menu
  51         self.menuProcess = wx.wxMenu()
  52         self.menuProcess.Append(ID_THRESHOLD, "Threshold", "")
  53         wx.EVT_MENU(self, ID_THRESHOLD,  self.OnThreshold)
  54 
  55         self.menuBar.Append(self.menuProcess, "&Process")
  56         self.SetMenuBar(self.menuBar)
  57 
  58     def OnOpen(self, event):
  59         fd = wx.wxFileDialog(self, "Open Image", "", "", "*.*", wx.wxOPEN)
  60 
  61         if fd.ShowModal() == wx.wxID_OK:
  62             self.loadImage(fd.GetPath())
  63         fd.Destroy()
  64 
  65     def loadImage(self, path):
  66         try:
  67             self.im = PythonMagick.Image(path)
  68             self.iPanel.display(self.im)
  69         except IOError:
  70             print "can't open the file"
  71 
  72     ##-------------- Process ------------------------
  73 
  74     def OnThreshold(self, event):
  75         self.im = self.Threshold(self.im, 0.5)
  76         self.iPanel.display(self.im)
  77         #self.im.write('d:/threshold.tif')
  78 
  79     def Threshold(self, image, threshold):
  80         """
  81         Threshold image. Input threshold is normalized (0-1.0)
  82         """
  83         img = PythonMagick.Image(image) # copy
  84         img.threshold(threshold *65535.0)
  85         return img
  86 
  87     ##-----------------------------------------------
  88 
  89     def OnCloseWindow(self, event):
  90         self.Destroy()
  91 
  92     def OnExit(self, event):
  93         self.Close(True)
  94 
  95 #---------------------------------------------------------------------------
  96 
  97 class mtApp(wx.wxApp):
  98     def OnInit(self):
  99         frame = mtFrame(wx.NULL, -1, "MagickSimple1")
 100         frame.Show(True)
 101         self.SetTopWindow(frame)
 102         return True
 103 
 104 app = mtApp(0)
 105 app.MainLoop()

-Bob Klimek 9-23-03

Thanks

Another word of thanks. I used the pil --> image with alpha tricks learned from this page. \d

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