'bitstream' is an array where each element contains 8 bits, i.e. etc. etc.

I want to print out this array to a file so it'll be recognised and can be opened as a jpeg.

I have tried the following:

output = open('outputfile.jpg', 'wb')
for i in range(len(bitstream)):
    output.write(bitstream[i])

and this...

bitstreamtoprint = string.join(bitstream, "")

output = open('outputfile.jpg', 'wb')
for i in range(len(bitstreamtoprint)):
      output.write(bitstreamtoprint[i])

however neither of these work, they create the file but it won't open as a jpeg, no matter what application I try to open them in. I'm using Windows Vista, and have also tried opening the files in Ubuntu Linux and it does not work on that either - I get an error saying the file is corrupted or that it is not a jpeg.

Any help?

A jpeg file needs a header with all sorts of infomation in it. Also the image array itself is processed with a compression algorithm to make it as small as possible.

You should be better off to go with a bitmap file. They are somewhat simpler. See:
http://en.wikipedia.org/wiki/BMP_file_format

Ok... but how exactly would I write that data to a file as a jpeg, or bitmap file? This is what is confusing me...

If you use a library like PIL or pygame, you can pass that data to the library and let it handle to compression and saving for you.

Just a thought.

I played around with PIL a while back and pretty much came to the conclusion that it needs a list of (r, g, b) tuples to work. You could potentially translate a color value to an (r, g, b) tuple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.