472,958 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 8937
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to...
2
by: Clas Hortien | last post by:
Hi, i have a array of uint and want to display this array as a System.Web.UI.WebControls.Image. //========================================= uint ar = new uint; string fName;
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
3
by: Dennis | last post by:
I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte array containing the resulting JPEG Image as follows: Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As...
3
by: ShihChengYu | last post by:
Dear all: How to convert color image to bi-level image? I have confronted one problem when I build my OCR project. I used an software API function to enhance my project, but the API only...
1
by: =?Utf-8?B?UmljYXJkbyBGdXJ0YWRv?= | last post by:
I'm using OpenGL in Visual Basic .net 2005 and i need to use the following function glBitmap(width as integer,height as integer, xorig as single, yorig as single, xmove as single, ymove as...
4
by: krishp | last post by:
Hi, I need to convert raw data recieved from a device through C#.Net socket to BMP format. For some reason the image conversion is not happening proparly and I can see only blur image . ...
2
by: Bill Fuller | last post by:
I have a Bitmap (bm) that I am able to save using, for example, bm.Save("file.jpg", System.Drawing.Imaging.ImageFormat.Jpeg). However, I would like to convert this to a byte array (byte). What is...
14
by: Joe | last post by:
this file is drawn in VB.NET and input output goes by XmlSerializer. Therefore, simple output file looks like: <?xml version="1.0" encoding="utf-8"?> <DrawablePicture...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.