Make the calls with any flag parameters at their default values, that is, don't even include them in the call parameter list. To handle all the various image type conversions while also properly copying any transparency the following ImgConv.py module can be used. To convert a wx.Image transparency mask in to true 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. This is usually not what is wanted.Ĭonverting Binary-Valued Alpha Transparency into True (256 values) Alpha Transparency }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. To check if a PIL image has transparency : Remember that PIL image transparency can only be the alpha kind. wxPython will not allow you to drop the "wx.BITMAP_TYPE_ANY" parameter even though it's not needed. The PIL open() function automatically determines the file's image type. ImgHasMask = wxImg.HasMask() # "HasMask()", NOT "GetMask()" ! Egads. To determine whether the bitmap has a mask or alpha transparency :
![pil image getdata pil image getdata](https://user-images.githubusercontent.com/6065806/147676169-28a6c1b7-77a2-46a8-8621-5a0357922888.png)
WxImg.LoadFile( filename, wx.BITMAP_TYPE_ANY ) WxImg = wx.EmptyBitmap( 1, 1 ) # Create a bitmap container WxBmap.LoadFile( filename, wx.BITMAP_TYPE_ANY ) # Load it with a file image.ĭetermine whether a bitmap has a mask or alpha transparency :īmapHasMask = wxBmap.GetMask() # "GetMask()", NOT "HasMask()" !Ĭreating a wx.Image from an image file is just like that for a wx.Bitmap : WxBmap = wx.EmptyBitmap( 1, 1 ) # Create a bitmap container object. In comparison, JPG files cannot have any kind of transparency.Ĭreating a wx.Bitmap from an image file : A PNG file, however, may have a transparency mask, alpha transparency or neither. For example, a GIF file may have a transparency mask, or no transparency at all. The amount of transparency each pixel has is defined by a value from 0 to 255. 2) Each pixel can have a variable amount of transparency that is from completely transparent (alpha value=0) to completely opaque (value=255). This binary transparency is a transparency mask. 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. GetData() ) 52 return myPilImage 53 54 def WxImageToWxBitmap( myWxImage ) : 55 return myWxImage.
SetData( myPilImageRgbData ) 44 45 return myWxImage 46 47 #- 48 49 def imageToPil( myWxImage ): 50 myPilImage = Image. 42 myPilImageRgbData = myPilImageCopyRGB. convert( ' RGB ' ) # Discard any alpha from the PIL image. copy() 41 myPilImageCopyRGB = myPilImageCopy. 36 37 else : # The resulting image will not have alpha. tostring() ) # Create layer and insert alpha values. SetData( myPilImageRgbData ) 35 myWxImage. convert( ' RGB ' ) # RGBA -> RGB 33 myPilImageRgbData = myPilImageCopyRGB. copy() 32 myPilImageCopyRGB = myPilImageCopyRGBA. size ) 31 myPilImageCopyRGBA = myPilImage.
![pil image getdata pil image getdata](https://images.wondershare.com/pdfelement/top-pdf-software/extract-images-from-pdf-python-01.jpg)
mode = ' A ' 28 if copyAlpha and hasAlpha : # Make sure there is an alpha layer copy. 24 25 def PilImageToWxImage( myPilImage, copyAlpha= True ) : 26 27 hasAlpha = myPilImage. 23 # But, a PIL image with alpha is necessary to get a wx.Image with alpha. 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. ImageFromBitmap( myBitmap ) 10 11 #- 12 13 def PilImageToWxBitmap( myPilImage ) : 14 return WxImageToWxBitmap( PilImageToWxImage( myPilImage ) ) 15 16 def PilImageToWxImage( myPilImage ): 17 myWxImage = wx. 4 5 def WxBitmapToPilImage( myBitmap ) : 6 return WxImageToPilImage( WxBitmapToWxImage( myBitmap ) ) 7 8 def WxBitmapToWxImage( myBitmap ) : 9 return wx. 1 # Tested with wxPython 2.3.4.2 and PIL 1.1.3.