471,349 Members | 1,173 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

how to convert a bitmap file to an array ?

hello

I can find all kind of procedures to convert an array to a bitmap
(wxPython, PIL),
but I can't find the reverse,
either
- convert a bitmap to an array
or
- read a bitmap file to an array

thanks,
Stef Mientki
Jun 5 '07 #1
5 8210
stef schrieb:
hello

I can find all kind of procedures to convert an array to a bitmap
(wxPython, PIL),
but I can't find the reverse,
either
- convert a bitmap to an array
or
- read a bitmap file to an array

thanks,
Stef Mientki
Take a look at the "struct" module.
There you fill find pack/unpack, which can do what you need.
Jun 5 '07 #2
stef wrote:
hello

I can find all kind of procedures to convert an array to a bitmap
(wxPython, PIL),
but I can't find the reverse,
either
- convert a bitmap to an array
or
- read a bitmap file to an array
Not true for PIL, Image.getdata and Image.putdata are your friends.

Diez
Jun 5 '07 #3
Stefan Sonnenberg-Carstens wrote:
stef schrieb:
>hello

I can find all kind of procedures to convert an array to a bitmap
(wxPython, PIL),
but I can't find the reverse,
either
- convert a bitmap to an array
or
- read a bitmap file to an array

thanks,
Stef Mientki
Take a look at the "struct" module.
There you fill find pack/unpack, which can do what you need.
thanks Stefan,

but using struct, I need to know how a bitmap file is build,
I don't know that (and I rather don't want to know that ;-)

Any better solutions (I'm a spoiled Delphi user ;-)

cheers,
Stef Mientki
Jun 5 '07 #4
Diez B. Roggisch wrote:
stef wrote:

>hello

I can find all kind of procedures to convert an array to a bitmap
(wxPython, PIL),
but I can't find the reverse,
either
- convert a bitmap to an array
or
- read a bitmap file to an array

Not true for PIL, Image.getdata and Image.putdata are your friends.

thanks Diez,
I really overlooked that.

cheers,
Stef Mientki
Jun 5 '07 #5
stef wrote:
Stefan Sonnenberg-Carstens wrote:
>stef schrieb:
>>hello

I can find all kind of procedures to convert an array to a bitmap
(wxPython, PIL),
but I can't find the reverse,
either
- convert a bitmap to an array
or
- read a bitmap file to an array

thanks,
Stef Mientki
Take a look at the "struct" module.
There you fill find pack/unpack, which can do what you need.
thanks Stefan,

but using struct, I need to know how a bitmap file is build,
I don't know that (and I rather don't want to know that ;-)

Any better solutions (I'm a spoiled Delphi user ;-)
I can offer the code below, but I can't guarantee you won't need to play
with it. You can almost certainly ignire the fonty bits!

def _setupContext(memory, font=None, color=None):
if font:
memory.SetFont(font)
else:
memory.SetFont(wx.NullFont)
if color:
memory.SetTextForeground(wx.Colour(*color))

def write( text, bitmap, pos=(0,0), font=None, color=None):
"""Simple write into a bitmap doesn't do any checking."""
memory = wx.MemoryDC()
_setupContext(memory, font, color)
memory.SelectObject(bitmap)
width, height = memory.GetTextExtent(text)
try:
memory.DrawText(text, pos[0],pos[1],)
finally:
memory.SelectObject(wx.NullBitmap)
return width

def bitmapToPil(bitmap):
return imageToPil(bitmapToImage(bitmap))

def bitmapToImage(bitmap):
return wx.ImageFromBitmap(bitmap)
def pilToBitmap(pil):
return imageToBitmap(pilToImage(pil))

def pilToImage(pil):
image = wx.EmptyImage(pil.size[0], pil.size[1])
image.SetData(pil.convert('RGB').tostring())
return image

def imageToPil(image):
pil = Image.new('RGB', (image.GetWidth(), image.GetHeight()))
pil.fromstring(image.GetData())
return pil

def imageToBitmap(image):
return image.ConvertToBitmap()
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Jun 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nicolas Guilhot | last post: by
2 posts views Thread by Clas Hortien | last post: by
7 posts views Thread by Scott Schluer | last post: by
3 posts views Thread by ShihChengYu | last post: by
1 post views Thread by =?Utf-8?B?UmljYXJkbyBGdXJ0YWRv?= | last post: by
2 posts views Thread by Bill Fuller | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.