I tried to convert a 32-bit Bitmap to 32-bit PNG using PIL.
from PIL import Image
im = Image.open('example.bmp')
print im.mode
# it prints 'RGB', but expected was 'RGBA'
im.save('output.png', format='PNG')
The expected image mode is ‘RGBA’, but actually i get ‘RGB’.
I also tried the following code, but it does not work.
from PIL import Image
im = Image.open('example.bmp')
im = im.convert('RGBA')
im.save('output.png', format='PNG')
Ok, here is something to get started. Since I don’t know specifically which format is your BMP file, I only handled a specific case of BMP with full alpha channel that I happen to have. The kind of BMPs I’m handling here can be obtained by converting, for example, PNG with alpha to BMP using ImageMagick. This will create what is called “BITMAPV5”. Given your description, you don’t have a BitmapV5 (because PIL would fail to even open it), so we will need an iteration with discussions to solve your specific case.
So, you either need a new file decoder or a patched
BmpImagePlugin.py. How to do the former is described in PIL’s manual. For the later you will obviously need to send a patch and hope to get it into the next PIL version. My focus is on creating a new decoder:To properly use this, the canonical way is supposedly to perform:
The problem is that there is already a plugin for handling “.bmp”, and I didn’t bother to find out how I could prepend this new extension so it is used before BmpImagePlugin is used (I also don’t know if it is possible to do such thing in PIL). Said that, I actually used the code directly, as in:
Where gearscolor.bmp is a sample bitmap with full alpha channel as described earlier. The resulting png is saved with alpha data. If you check
BmpImagePlugin.py‘s code, you will notice I reused most of its code.