473,770 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting to ARGB and writing to a bitmap

Hi,
I don't know what type of files they are, but the script allows me to
save to a bitmap just fine. But I do know they need to be in RGBA 4444
format, so I've followed what most of the tutorials for RGBA
conversions said to do...shifting the bytes.

The major problem is that everything is in green, it's shifted up by x
pixels, and it's..um..flipp ed from left to right. I've been working on
this for a few weeks now and any insight to the problem is greatly
appreciated.

http://www.geocities.com/fisherdude_99/TestImg.zip
import struct
import sys
import Image
if len(sys.argv) < 3:
print "usage: tobmp.py <file> <width>"
sys.exit(1)

width = int(sys.argv[2])

f1 = open(sys.argv[1], "rb")
f1.seek(0, 2)
size = f1.tell()
f1.seek(0)

### write BITMAPFILEHEADE R ###
f2 = open(sys.argv[1] + ".bmp", "wb")
f2.write("BM")

bfSize = size+54
f2.write(struct .pack("L", bfSize))

# bfReserved1 and 2
f2.write("\x00\ x00\x00\x00")

bfOffBits = 54
f2.write(struct .pack("L", bfOffBits))

### write BITMAPINFOHEADE R ###
biSize = 40
f2.write(struct .pack("L", biSize))

biWidth = width
f2.write(struct .pack("L", biWidth))

biHeight = (size/ 2/ width)
f2.write(struct .pack("L", biHeight))

biPlanes = 1
f2.write(struct .pack("H", biPlanes))

biBitCount = 32
f2.write(struct .pack("H", biBitCount))

# biCompression
f2.write("\x00\ x00\x00\x00")

# biSizeImage
f2.write("\x00\ x00\x00\x00")

# biXPelsPerMeter
f2.write("\x00\ x00\x00\x00")

# biYPelsPerMeter
f2.write("\x00\ x00\x00\x00")

# biClrUsed
f2.write("\x00\ x00\x00\x00")

# biClrImportant
f2.write("\x00\ x00\x00\x00")

### write the bitmap data ###
print f2.tell() ##must be 54

# since bitmaps are upside down, gotta flip it over
data = f2.read()

i = size-21
while i >= 0:
nm = 0x100

f1.seek(i)
temp1 = struct.unpack(' B', f1.read(1))[0]

peek = temp1

a = nm*(peek >> 24) & 0xff
r = nm*(peek >> 16) & 0xff
g = nm*(peek >> 8 ) & 0xff
b = nm*(peek & 0xff)

sh = (a<<24)|(r<<16) |(g<<8)|b

f2.write(struct .pack('H', sh))
i -= 1

f1.close()
f2.close()
Usage: bmpcon.py testimg 100

Thanks a lot

Mar 17 '06 #1
2 7108
he*********@gma il.com wrote:
Hi,
I don't know what type of files they are, but the script allows me to
save to a bitmap just fine. But I do know they need to be in RGBA 4444
format, so I've followed what most of the tutorials for RGBA
conversions said to do...shifting the bytes.

The major problem is that everything is in green, it's shifted up by x
pixels, and it's..um..flipp ed from left to right. I've been working on
this for a few weeks now and any insight to the problem is greatly
appreciated.

http://www.geocities.com/fisherdude_99/TestImg.zip


This doesn't really answer your question, but have you looked into PIL yet?

http://www.pythonware.com/products/pil/index.htm
(except, hmmm.... it looks like the web site is down right now. Check back
later?)

It's got a bunch of different formats supported natively and makes image
processing work a lot easier in some respects.

--
Steve Juranich
Tucson, AZ
USA

Mar 17 '06 #2
he*********@gma il.com wrote:

I don't know what type of files they are, but the script allows me to
save to a bitmap just fine. But I do know they need to be in RGBA 4444
format, so I've followed what most of the tutorials for RGBA
conversions said to do...shifting the bytes.
It wasn't until I looked at your sample image that I realized you meant the
INCOMING image is ARGB 4444, and you're trying to convert it to ARGB 8888.
The major problem is that everything is in green, it's shifted up by x
pixels, and it's..um..flipp ed from left to right. I've been working on
this for a few weeks now and any insight to the problem is greatly
appreciated.
There are a couple of issues here. Depth 32 is not one of the standard
formats for DIBs. 24 is (B G R B G R), but not 32. To create 32-bit DIBs,
you are supposed to set biCompression to BI_BITFIELDS, and then supply
bitmasks in the three dwords that follow the BITMAPINFOHEADE R telling
exactly where the R, G, and B values can be found. However, NT happens to
handle the format as you have it, so we'll leave it alone.

DIBs have no concept of an alpha channel. What are you going to use to
read this?
# since bitmaps are upside down, gotta flip it over
data = f2.read()

i = size-21
while i >= 0:
nm = 0x100

f1.seek(i)
temp1 = struct.unpack(' B', f1.read(1))[0]
This reads one 8 bit value, and then tries to pull 32 bits of information
from it. You need to read a 16-bit value and crack it 4 bits at a time,
not 8 bits.
peek = temp1

a = nm*(peek >> 24) & 0xff
r = nm*(peek >> 16) & 0xff
g = nm*(peek >> 8 ) & 0xff
b = nm*(peek & 0xff)
What is the "multiply by 0x100" supposed to be doing? As near as I can
tell, since "*" binds more tightly than "&", this will result in a, r, and
always being 0.
sh = (a<<24)|(r<<16) |(g<<8)|b

f2.write(struct .pack('H', sh))


"sh" is a 32-bit value. You almost certainly want 'L' here, not 'H'.

Here is one that works, although it creates the DIB upside down, as you
note. I'll leave that as an exercise for the reader.

import os
import struct
import array

IN = 'testimg'
OUT = 'xxx.bmp'

fin = file(IN,'rb')

width = 100
insize = os.stat(IN)[6]
height = insize / width / 2

fout = file(OUT,'wb')

# Do the BITMAPFILEHEADE R.

fout.write('BM' )
bmfh = struct.pack( 'LLL', insize*2 + 14 + 40, 0, 14+40 )
fout.write(bmfh )

# Do the BITMAPINFOHEADE R.

bmih = struct.pack('LL LHHLLLLLL',
40,
width,
height,
1,
32,
0, 0, 0, 0, 0, 0 )
fout.write(bmih )

# Expand the pixels, scan by scan.

for i in range(height):
words = array.array('H' )
words.fromfile( fin, width )
rgba = array.array('B' )
for word in words:
a = ((word >> 12) & 0xf) * 0x11
r = ((word >> 8) & 0xf) * 0x11
g = ((word >> 4) & 0xf) * 0x11
b = ((word >> 0) & 0xf) * 0x11
rgba.extend( [b,g,r,a] )
rgba.tofile(fou t)
print '*',
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4861
by: Steve Amey | last post by:
Hi all I have an ARGB value for a Colour (Eg. -65536. The value was retrieved by using the Color.ToArgb method), is there any way that I can create a System.Drawing.Image or a System.Drawing.Bitmap from that value? Regards, Steve
8
4568
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
2
9897
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular bitmap file (filename.bmp) I am experiencing some difficulties with writing a bitmap to a http response stream. I load the bitmap from a file. and i use the following snippet of code to write to HTTP response stream
5
29185
by: Samuel L Matzen | last post by:
Hi NG, I have an Argb color stored in a database as a 32 bit integer. If this color is a KnownColor, is there any way I can get the known color name so I can display the known color name on the form and in my reports? The System.DrawingColor.ToKnowColor method will not do this for me. Thanks in advance for any help.
2
6197
by: Map Reader | last post by:
Greetings, I am converting an old VB6 application to use .NET. One of the old controls loads icons from the disk and displays them. However, the transparent color turns to blue somewhere in the process. I narrowed it down to the conversion to IPictureDisp by first loading and saving the icon as a bitmap with no problems; and then repeating the process but adding the step of converting to an IPictureDisp as well. The first operation...
1
6145
by: Martin Widmer | last post by:
Hi Folks In my object I am trying to implement a function in order to render the picture to XML (to generate RDL for SQL Server). That Function causes the error mentioned in the subject at the marked line. I read various articles about it but none of them seems to apply as I am not writing to a file or to a response in ASP, instead it's a pure VB class that is used from a winforms applciation. I am using VS 2005. Private Function...
3
6110
by: Sharon | last post by:
I have a buffer of byte that contains a raw data of a 1 byte-per-pixel image data. I need to convert this buffer to a Bitmap of Format32bppArgb and to a Bitmap of Format24bppRgb. Can anybody tell how to do it? ------ Thanks
2
10353
by: Laurent Navarro | last post by:
Hello, I am using a library which returns a byte containing RAW data, ie all pixels' color values coded in a byte array without header. I would like to save those data into a JPEG file so I tried to use the MetaFile class. byte data; (...) // Creating the RAW image through the DLL call. MemoryStream memoryStream = new MemoryStream(data);
5
9423
by: almurph | last post by:
RE: Tryign to convert Graphics object to a bitmap Hi, Hope you can help me with this. I have to open a file and add some text to it and then display it. So I create an Image object then import it into a Graphics object and add the text. I then try converting it to a bitmap image using the following line:
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.