I can not execute this code, it should receive an image in .BMP
format and return the list of pixels, I do not know what could be wrong:
from struct import pack
from struct import unpack
def ReadBMP(pach):
image_file = open ('C:/img2.bmp', "rb" ,pach)
image_file.seek(18)
width = unpack('i', image_file.read(4))[0]
height = unpack('i', image_file.read(4))[0]
#print(width, height)
image_file.seek(0,0)
image_file.seek(54)
rows = []
row = []
pixel_index = 0
while True:
if pixel_index == width:
pixel_index = 0
rows.insert(0, row)
row = []
pixel_index += 1
r_string = image_file.read(1)
g_string = image_file.read(1)
b_string = image_file.read(1)
if len(r_string) == 0:
if len(rows) != height:
print("Warning!!! Read to the end of the file at the correct sub-pixel (red) but we've not read 1080 rows!")
break
if len(g_string) == 0:
print ("Warning!!! Got 0 length string for green. Breaking.")
break
if len(b_string) == 0:
print ("Warning!!! Got 0 length string for blue. Breaking.")
break
r = ord(r_string)
g = ord(g_string)
b = ord(b_string)
pixels = []
pixels.append(b)
pixels.append(g)
pixels.append(r)
row.append(pixels)
#row.append(g)
#row.append(r)
image_file.close()
return rows
This is the error message:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
ReadBMP(pach)
NameError: name 'pach' is not defined