Error creating function in Python 3.7.0

Asked

Viewed 234 times

0

I cannot execute this code, it should receive an image in format .BMP and return to the list of pixels, do not know what can 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
  • What did you call the function? What is the value of pach?

  • the function should have the same file name (img2.bmp), but def ( ) does not accept the point, I tried to put Pach with a common name but it did not work

1 answer

0

When defining a function, we place in parentheses the name of variables that will receive the parameters passed for its execution. For example, a function that takes 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 filename, for example:

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

Or, another example, ask the user to enter the parameter, and store it in a variable, to then pass 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 are trying to pass pach... This is the name you gave for the variable that will receive the parameter within the function, however, it does not exist outside the 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 code of the function... If the function receives a parameter, it should be used; otherwise, the function always executes in the same way independent of the parameter, so it makes no sense to exist.

  • Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.