From: Kevin A. <al...@se...> - 2001-09-15 22:18:51
|
I added a drawPIL method to the BitmapCanvas widget so Roman or anyone else can try some sample tests using images created or manipulated by PIL. The changes are in cvs and will be part of the next release, which I hope to get out by Tuesday, if not earlier. When your sample starts up you can check the variable PythonCardPrototype.widget.PIL_FOUND to make sure PIL exists on the local machine, or you can use a variation of the test I currently use to set the variable in widget.py: try: import Image PIL_FOUND = 1 except ImportError: PIL_FOUND = 0 I copied the trash.gif image from the proof directory into the doodle sample directory and then typed the following lines in the shell to test out the method. >>> import Image >>> source = Image.open('trash.gif') >>> comp.bufOff.drawPIL(source, 10, 10) Transparency appears to be lost in the conversion from PIL to wxImage to wxBitmap, so perhaps Robin or some other wxPython expert can shed some light on how to preserve the transparency color if available. On the other hand, I wrote this method pretty quickly, so my code might just be buggy. ka --- def drawPIL(self, image, x, y, transparency=1): if PIL_FOUND: if not self._drawingInProgress: self._bufImage.BeginDrawing() self._drawingInProgress = 1 if (image.mode != 'RGB'): image = image.convert('RGB') imageData = image.tostring('raw', 'RGB') imageWx = wxEmptyImage(image.size[0], image.size[1]) imageWx.SetData(imageData) bmp = wxBitmapFromImage(imageWx) #imageWx = apply(wxEmptyImage, image.size) #imageWx.SetData(image.convert("RGB").tostring() ) #bmp = imageWx.ConvertToBitmap() self._bufImage.DrawBitmap(bmp, x, y, transparency) if self.autoRefresh: self._bufImage.EndDrawing() self._drawingInProgress = 0 dcDelegate = wxClientDC(self._delegate) dcDelegate.Blit(x, y, bmp.GetWidth(), bmp.GetHeight(), self._bufImage, x, y) |