473,386 Members | 1,654 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,386 software developers and data experts.

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..flipped 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 BITMAPFILEHEADER ###
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 BITMAPINFOHEADER ###
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 7026
he*********@gmail.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..flipped 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*********@gmail.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..flipped 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 BITMAPINFOHEADER 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 BITMAPFILEHEADER.

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

# Do the BITMAPINFOHEADER.

bmih = struct.pack('LLLHHLLLLLL',
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(fout)
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
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...
8
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...
2
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...
5
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...
2
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...
1
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...
3
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...
2
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...
5
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.