insert_bitmap and GIF/JPEG

1,359 views
Skip to first unread message

super...@gmail.com

unread,
Mar 12, 2009, 5:50:23 PM3/12/09
to python-excel
Hello guys.
I need to insert GIF/JPEG images to Excel worksheet. As XLWT can only
handle bitmaps (BMP) i convert the image to BMP with PIL:

img = Image.open('.seller')
img.convert('RGB')
img.save('.seller', 'BMP')

But unfortuantly that doesnt work:
Exception("bitmap isn't a 24bit true color bitmap.")

Can anyone help me out?

PS: According to PIL documentation (http://www.pythonware.com/library/
pil/handbook/concepts.htm) RGB = (3x8-bit pixels, true colour)

John Machin

unread,
Mar 12, 2009, 8:36:42 PM3/12/09
to python...@googlegroups.com
On 13/03/2009 8:50 AM, super...@gmail.com wrote:
> Hello guys.
> I need to insert GIF/JPEG images to Excel worksheet. As XLWT can only
> handle bitmaps (BMP) i convert the image to BMP with PIL:
>
> img = Image.open('.seller')

Is '.seller' a GIF or a JPEG?
Why the somewhat unusual file-naming convention?

> img.convert('RGB')
> img.save('.seller', 'BMP')

Have you tried not saving it with the same name?

> But unfortuantly that doesnt work:
> Exception("bitmap isn't a 24bit true color bitmap.")
>
> Can anyone help me out?
>
> PS: According to PIL documentation (http://www.pythonware.com/library/
> pil/handbook/concepts.htm) RGB = (3x8-bit pixels, true colour)

It works for me (in my environment, on 1 sample JPEG file); details:

foo.jpg: copy of Water Lilies.jpg found in .../My Pictures/Samples/...
or some such folder

PIL: freshly installed from
http://effbot.org/downloads/PIL-1.1.6.win32-py2.6.exe

xlwt: 0.7.1

C:\xlwt\images>\python26\python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> img = Image.open('foo.jpg')
>>> img.convert('RGB')
<PIL.Image.Image instance at 0x00F6A058>
>>> img.save('bar', 'BMP')
>>> ^Z

'bar' is reported by various image programs as being a 24 bits-per-pixel
BMP file.

C:\xlwt\images>\python26\python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlwt
>>> wb = xlwt.Workbook()
>>> ws = wb.add_sheet("image")
>>> ws.insert_bitmap('bar', 2, 2)
>>> wb.save('bmp_from_jpg.xls')
>>> ^Z

Excel 2003 opens the file OK and the picture looks fine.

C:\xlwt\images>type dump_bmp_header.py
# dump BMP header
import struct, sys
fname = sys.argv[1]
data = open(fname, 'rb').read()
guff = struct.unpack("<2s6L2H", data[:30])
fmt = ("sig=%r, bitmap_size=%r, reserved1=%r, offset=%r,\n"
"header_len=%r, width=%r, height=%r, planes=%r, bit_count=%r")
print fmt % guff

C:\xlwt\images>dump_bmp_header.py python.bmp
sig='BM', bitmap_size=37446, reserved1=0, offset=54,
header_len=40, width=76, height=164, planes=1, bit_count=24

C:\xlwt\images>dump_bmp_header.py bar
sig='BM', bitmap_size=1440054, reserved1=0, offset=54,
header_len=40, width=800, height=600, planes=1, bit_count=24

Note that the code in Bitmap.py raises the exception that you saw if
bit_count != 24. It has already checked that sig == "BM". The next thing
that it will check is that planes == 1. Evidently planes are called
"frames" by some software. There's a possibility that your file may have
3 x 8 instead of 1 x 24.

Please tell us what your environment is.
Please run the dump_bmp_header.py on your file.
Can you send a copy of your original GIF/JPEG file?

Cheers,
John

super...@gmail.com

unread,
Mar 12, 2009, 8:51:32 PM3/12/09
to python-excel
Thank you, for the response, John (:

Image i try to insert: http://i42.tinypic.com/oifzfp.jpg

My environment:
Debian Lenny
python 2.5.4
PIL 1.1.6
xlwt 0.7.1

Where can i find dump_bmp_header.py?

Sergei

super...@gmail.com

unread,
Mar 12, 2009, 8:54:55 PM3/12/09
to python-excel
aww...sorry

here's the header:
sig='BM', bitmap_size=5878, reserved1=0, offset=1078,
header_len=40, width=160, height=30, planes=1, bit_count=8

Looks like it IS 8 bit

super...@gmail.com

unread,
Mar 12, 2009, 9:03:44 PM3/12/09
to python-excel
Something happened with my prev. answers.

Image: http://i42.tinypic.com/oifzfp.jpg
Environment:
Debian Lenny
python 2.5.4
xlwt 0.7.1
PIL 1.1.6

John Machin

unread,
Mar 12, 2009, 9:22:52 PM3/12/09
to python...@googlegroups.com

I don't know how you got 8 in there ....

C:\xlwt\images>\python26\python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image

>>> img = Image.open('oifzfp.jpg')
>>> img.convert('RGB')
<PIL.Image.Image instance at 0x00F8F2D8>
>>> img.save('oifzfp_conv.bmp', 'BMP')
>>> ^Z

C:\xlwt\images>dump_bmp_header.py oifzfp_conv.bmp
sig='BM', bitmap_size=14454, reserved1=0, offset=54,
header_len=40, width=160, height=30, planes=1, bit_count=24

*AND* I got the same effect when I left out the img.convert('RGB') step
... the JPEG file is already RGB.

So: I suggest that you either
(a) find another method of converting files to BMP format
or
(b) stick with PIL, gird up your loins, and do battle with the effbot :-)

Cheers,
John

John Machin

unread,
Mar 12, 2009, 9:24:34 PM3/12/09
to python...@googlegroups.com


Already got that info (spread over two messages).

super...@gmail.com

unread,
Mar 13, 2009, 7:43:06 AM3/13/09
to python-excel
Omg, i just realised that img.convert() doesnt change the object, u
need to use img = img.convert() instead.
Thank u John, everythin works perfect now.

Sergei

John Machin

unread,
Mar 13, 2009, 10:00:53 AM3/13/09
to python...@googlegroups.com

Very strange. As I reported last time, omitting the convert step made no
difference.

C:\xlwt\images>\python26\python
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image

>>> img = Image.open('oifzfp.jpg')
>>> img.bits
8
>>> img.format
'JPEG'
>>> img.info
{'jfif_version': (1, 1), 'jfif': 257, 'jfif_unit': 2, 'jfif_density':
(0, 0)}
>>> img.mode
'RGB'
>>> img.save('.oifzfp_noconv_noext', 'BMP')
>>> ^Z


C:\xlwt\images>dump_bmp_header.py .oifzfp_noconv_noext


sig='BM', bitmap_size=14454, reserved1=0, offset=54,
header_len=40, width=160, height=30, planes=1, bit_count=24

==== see? no conversion step at all, got bit_count == 24

I can't get it to do bit_count = 8 at all. Did you post the wrong file
(oifzfp.jpg)?

In any case, any vague fears that xlwt was part of the problem seem to
have been well and truly dispelled.

Cheers,
John

Reply all
Reply to author
Forward
0 new messages