Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:25:29+00:00 2026-06-15T15:25:29+00:00

I tried to convert a 32-bit Bitmap to 32-bit PNG using PIL. from PIL

  • 0

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')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:25:31+00:00Added an answer on June 15, 2026 at 3:25 pm

    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:

    from PIL import ImageFile, BmpImagePlugin
    
    _i16, _i32 = BmpImagePlugin.i16, BmpImagePlugin.i32
    
    class BmpAlphaImageFile(ImageFile.ImageFile):
        format = "BMP+Alpha"
        format_description = "BMP with full alpha channel"
    
        def _open(self):
            s = self.fp.read(14)
            if s[:2] != 'BM':
                raise SyntaxError("Not a BMP file")
            offset = _i32(s[10:])
    
            self._read_bitmap(offset)
    
        def _read_bitmap(self, offset):
    
            s = self.fp.read(4)
            s += ImageFile._safe_read(self.fp, _i32(s) - 4)
    
            if len(s) not in (40, 108, 124):
                # Only accept BMP v3, v4, and v5.
                raise IOError("Unsupported BMP header type (%d)" % len(s))
    
            bpp = _i16(s[14:])
            if bpp != 32:
                # Only accept BMP with alpha.
                raise IOError("Unsupported BMP pixel depth (%d)" % bpp)
    
            compression = _i32(s[16:])
            if compression == 3:
                # BI_BITFIELDS compression
                mask = (_i32(self.fp.read(4)), _i32(self.fp.read(4)),
                        _i32(self.fp.read(4)), _i32(self.fp.read(4)))
                # XXX Handle mask.
            elif compression != 0:
                # Only accept uncompressed BMP.
                raise IOError("Unsupported BMP compression (%d)" % compression)
    
            self.mode, rawmode = 'RGBA', 'BGRA'
    
            self.size = (_i32(s[4:]), _i32(s[8:]))
            direction = -1
            if s[11] == '\xff':
                # upside-down storage
                self.size = self.size[0], 2**32 - self.size[1]
                direction = 0
    
            self.info["compression"] = compression
    
            # data descriptor
            self.tile = [("raw", (0, 0) + self.size, offset,
                (rawmode, 0, direction))]
    

    To properly use this, the canonical way is supposedly to perform:

    from PIL import Image
    Image.register_open(BmpAlphaImageFile.format, BmpAlphaImageFile)
    # XXX register_save
    
    Image.register_extension(BmpAlphaImageFile.format, ".bmp")
    

    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:

    from BmpAlphaImagePlugin import BmpAlphaImageFile
    
    x = BmpAlphaImageFile('gearscolor.bmp')
    print x.mode
    x.save('abc1.png')
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried to convert bitrate mode from VBR to CBR with FFMPEG library,but bitrate
I want to convert the input 24 bit PNG image to 8 bit, I
I have tried to convert an SVN project to Mercurial using svnsync and then:
When I tried to convert something like 0.1 (from user in textbox), My value
I tried many things but I always get cannot convert string to membershipuser from
I'd like to, in code and on demand, convert a 32-bit RGBA Image object
Is there an easy way to take a TrueColor Image (24 Bit, Bitmap in
Exactly like the title. I take a single-band image of mode 1 and open
I am trying to convert a given Mat representing an RGB image with 8-bit
Summary Using Windows GDI to convert 24-bit color to indexed color, it seems GDI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.