1,105,333 Community Members

Converting an image file (png) to a bitmap file

Member Avatar
rasizzle
Light Poster
32 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here's my dilemma:

I am creating a histogram through matplotlib and saving it in the same folder I'm running. There are a few image files that I can save it as, none of them being bitmap.

So I'm trying to convert the image to bitmap. I've looked at WxImage and Python Imaging Library (PIL) and I have no clue what I'm doing wrong.

My goal is to take that newly saved image file (.png) and to convert it to a bitmap (.bmp) file within the same folder.

I've looked at a number of examples but I either don't know how to apply them or they don't work.

I just want to know a simple example that works. Python Image Library has a tobitmap() function but it doesn't work. WxImage has a ConvertToBitmap() function but I don't understand it.

Any help?

Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
2
 

It's as simple as this:

# convert a .png image file to a .bmp image file using PIL

from PIL import Image

file_in = "test1.png"
img = Image.open(file_in)

file_out = "test1.bmp"
img.save(file_out)
Member Avatar
rasizzle
Light Poster
32 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks, Ene Uran. This is the closest I've come but there's still an error.

So the program manages to create a "test1.bmp" image but it says "No Preview Available" when I double click on it.

I also get this error message.

Traceback (most recent call last):
  File "C:\Python26\January\wxpython.py", line 9, in <module>
    img.save(file_out)
  File "C:\Python26\lib\site-packages\PIL\Image.py", line 1405, in save
    save_handler(self, fp, filename)
  File "C:\Python26\lib\site-packages\PIL\BmpImagePlugin.py", line 197, in _save
    raise IOError("cannot write mode %s as BMP" % im.mode)
IOError: cannot write mode RGBA as BMP

Any idea what I'm doing wrong?

Member Avatar
rasizzle
Light Poster
32 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I figured out what I'm doing wrong and this is actually similar to an error I had before. PIL will not let me convert to bitmap unless the image is black and white first. I'm going to try to convert it to black and white by associating the image with "1".

I did something like that earlier and failed. Attempt #2.

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
2
 

PIL does not support alpha in BMP files. You need to strip it yourself. Something like (warning: untested):

img = Image.open("file.png")
r, g, b, a = img.split()
img = Image.merge("RGB", (r, g, b))
img.save("file.bmp")
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

Strange, my test1.png image file is full color and I have no problems.

Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

Thanks nezachem!
Looks like my .png image file only has RGB and not RGBA. You could use this modified code:

# convert a .png image file to a .bmp image file using PIL

from PIL import Image

file_in = "test.png"
img = Image.open(file_in)

file_out = "test2.bmp"
print len(img.split())  # test
if len(img.split()) == 4:
    # prevent IOError: cannot write mode RGBA as BMP
    r, g, b, a = img.split()
    img = Image.merge("RGB", (r, g, b))
    img.save(file_out)
else:
    img.save(file_out)
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

Thanks nezachem!
Looks like my .png image file only has RGB and not RGBA. You could use this modified code:

# convert a .png image file to a .bmp image file using PIL

from PIL import Image

file_in = "test.png"
img = Image.open(file_in)

file_out = "test2.bmp"
print len(img.split())  # test
if len(img.split()) == 4:
    # prevent IOError: cannot write mode RGBA as BMP
    r, g, b, a = img.split()
    img = Image.merge("RGB", (r, g, b))
    img.save(file_out)
else:
    img.save(file_out)

Not sure what is going on, but I get these double posts with DaniWeb.

Member Avatar
rasizzle
Light Poster
32 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes!! It works! Thanks guys. I've been banging my head over this for the past couple of days and found little luck.

I really appreciate it.

Question Answered as of 4 Years Ago by Ene Uran and nezachem
Member Avatar
LaurieW
Newbie Poster
3 posts since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Thanks for this, it works great. I needed it to convert a png to bmp for inclusion in an Excel spreadsheet.

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Notice that independently from the pythonic solution above, one can use imagemagick on the command line, it's a specialized tool to convert between image formats. Here is a working command once imagemagick is installed

convert temp.png temp.bmp

This command has many options to manipulate size, color, transparency, etc.

Member Avatar
LaurieW
Newbie Poster
3 posts since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That sounds good but I needed to do everything in Python. Unless this can be run from Python or imported as a module within it?

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

That sounds good but I needed to do everything in Python. Unless this can be run from Python or imported as a module within it?

Actually, it can be used from python. The first way is to use the subprocess module to call imagemagick externally like in subprocess.call("convert temp.png temp.bmp", shell=True), but this is cheating because any program can be called from python this way. Another way is to use one of the imagemagick's python APIs. However, I never tried this myself :)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article