473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 9040
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.