7 Replies - 2373 Views - Last Post: 07 December 2010 - 05:49 AM Rate Topic: -----

#1 bocochoco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-December 09

reading file data

Posted 17 February 2010 - 12:01 PM

So I'm starting to figure out python more than I had previously (not at all). I'm not sure how to approach this particular problem though. I'm trying to read pixel data from a bitmap file. In C, I created a struct with unsigned shorts for the red, green, and blue channels. Then I made an array of them and read the file into that.

struct pixel
{
   unsigned char b;
   unsigned char g;
   unsigned char r;
};
int length = width * height;
struct pixel data[length];



I don't know how to create that in Python. I've made a class for it, but I don't know how to create the array like that..
class pixel:
    def __init__(self, r, g, B)/>:
        self.r = r
        self.g = g
        self.b = b



Is there a simple way to make an array of the class?

Many apologies for posting C code in the Python board.

Is This A Good Question/Topic? 0
  • +

Replies To: reading file data

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: reading file data

Posted 17 February 2010 - 12:18 PM

Probably want a list: Python Data Structures
Was This Post Helpful? 0
  • +
  • -

#3 bocochoco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-December 09

Re: reading file data

Posted 17 February 2010 - 02:25 PM

View PostJackOfAllTrades, on 17 February 2010 - 11:18 AM, said:

Probably want a list: Python Data Structures


That would work. Is there a way to loop through file.read(3)? I can read through line by line but is there a way to read a number of bytes in a loop
Was This Post Helpful? 0
  • +
  • -

#4 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: reading file data

Posted 17 February 2010 - 06:12 PM

while True:
    bytes = file.read(3)
    if bytes == '':
        # EOF has been reached
        break
    # Do stuff with bytes here
# Continue code after EOF has been reached here


Was This Post Helpful? 0
  • +
  • -

#5 bocochoco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-December 09

Re: reading file data

Posted 18 February 2010 - 02:42 PM

View Postprogramble, on 17 February 2010 - 05:12 PM, said:

while True:
    bytes = file.read(3)
    if bytes == '':
        # EOF has been reached
        break
    # Do stuff with bytes here
# Continue code after EOF has been reached here



That worked great. Thank you.
However, being the noob that I am, I'm stuck again.. Everything was working fine until I realized that bitmaps are written from the bottom row up. My code worked great for bitmaps with a height of 1, but it gets confused if there are more. I can't quite figure out how to compensate for this. Unfortunately this means that all my bitmaps are read in reverse. Since I need to have each pixel independent, I can't simply read it in and .reverse() it or the whole thing will get flipped around. I could do it in C, but I still don't get a lot of how python works...My code to read the bitmap data is below (the header is read above it but is unnecessary). Any idea how I could make this read from the bottom up so it's ordered correctly? It's the last part that I'm stuck on with this.

    # Read bitmap into program memory
    row = 0
    while row < head.height:
        col = 0
        while col < head.width:
            pixels.append(pixel(*struct.unpack('BBB', file.read(3))))
            col += 1
        # Compensate for padding
        lo = (col * head.width) % 4
        if lo != 0:
            file.read(lo)
        row += 1


Was This Post Helpful? 0
  • +
  • -

#6 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: reading file data

Posted 18 February 2010 - 06:46 PM

You could make your pixels list a list of lists (2d array), and then you could reverse just the rows. Here is an example to get you in the right direction.
pixels = []
for row in rows:
    thisrow = []
    for col in columns:
        thisrow.append(col)
    pixels.append(thisrow)
pixels.reverse()
# Later on:
pixels[0] # Get first row (a list of values)
pixels[0][0] # Get first value of first row
# etc.


Was This Post Helpful? 0
  • +
  • -

#7 bocochoco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-December 09

Re: reading file data

Posted 18 February 2010 - 08:26 PM

View Postprogramble, on 18 February 2010 - 05:46 PM, said:

You could make your pixels list a list of lists (2d array), and then you could reverse just the rows. Here is an example to get you in the right direction.

That worked out well. Sorta... It works for some test inputs, but not all. Whether or not it works depends on the width of the image.

    pixeldata = []
    row = 0
    while row < head.height:
        col = 0
        rowdata = []
        while col < head.width:
            rowdata.append(pixel(*struct.unpack('BBB', file.read(3))))
            col += 1
        pixeldata.append(rowdata)
        # Compensate for padding
        # lo = (col * head.width) % 4
        lo = (3 * head.width) % 4
        if lo != 0:
            file.read(lo)
        row += 1
    pixeldata.reverse
    
    currow = head.height - 1
    while currow >= 0:
        curcol = 0
        while curcol <= (head.width - 1):
            pixels.append(pixeldata[currow][curcol])
            curcol += 1
        currow -= 1

    for apixel in pixels:
        print apixel.r, apixel.g, apixel.b


For an 11x3 bitmap with the top row pure red, the middle row pure green, and the bottom row pure blue the output is:
0 255 0
0 0 255
0 0 255
0 255 0
0 0 255
0 0 255
0 0 255
0 0 255
0 0 255



I think it has something to do with how I'm compensating for the padding but I can't put my finger on it.

edit: I figured it out. Shouldn't have multiplied head.width.

This post has been edited by bocochoco: 19 February 2010 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 soldner  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 07-December 09

Re: reading file data

Posted 07 December 2010 - 05:49 AM

Couldn't he do a zip on the 2 lists?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1