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?

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)
commented: Great help. +1
commented: very helpful +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?

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.

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")

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

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)

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.

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.

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

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.

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?

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.