Error creating function in Python 3.7.0

0

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
    
asked by anonymous 10.12.2018 / 00:14

1 answer

0

When defining a function, we put in the parentheses the name of variables that will receive the parameters passed to execute them. For example, a function that gets the name of a file and opens it would look like this:

def ler_arquivo(nome_arquivo):
    with open(nome_arquivo, 'rb') as arquivo:
         #... codigo aqui para trabalhar o arquivo ../

Only when calling the above function should you pass a file name, for example:

ler_arquivo(r'c:\img2.bmp')

Or, another example, ask the user to enter the parameter, and store it in a variable, then move on to the function:

texto_digitado = input('Digite um nome de arquivo'):
ler_arquivo(texto_digitado)

In your example, by error, you are trying to call the function, but it is trying to pass pach ... This is the name you gave to the variable that will receive the parameter inside the function, but it does not exist out of function.

Another problem I see with the code is that the declared parameter is not being used within the function; The file name is fixed in the function code ... If the function receives a parameter, it should be used; Otherwise, the function always executes the same way regardless of the parameter, so it does not make sense for it to exist.

    
10.12.2018 / 01:59