How to use functions that are in a different file? - Python

0

I have a problem using functions that are in a different file than the location where I am calling these functions. Basically my project is organized as follows:

Lista_I (Pacote Contendo os Arquivos)
    __init__.py
    funcoes_uteis.py
    PDI.py

In the file PDI.py I have the following code:

import numpy

def aplicar_mascara_media_aritmetica(imagem):

    filtro = numpy.zeros((3, 3))

    for i in range(1, len(imagem) - 1):
        for j in range(1, len(imagem[0]) - 1):
            filtro[0][0] = imagem[i - 1][j - 1]
            filtro[0][1] = imagem[i - 1][j]
            filtro[0][2] = imagem[i - 1][j + 1]
            filtro[1][0] = imagem[i][j - 1]
            filtro[1][1] = imagem[i][j]
            filtro[1][2] = imagem[i][j + 1]
            filtro[2][0] = imagem[i + 1][j - 1]
            filtro[2][1] = imagem[i + 1][j]
            filtro[2][2] = imagem[i + 1][j + 1]

            imagem[i][j] = calcular_media(filtro) #não funciona

    return imagem

And in the funcoes_uteis.py file I have the calcular_media(filtro) function being called in the PDI.py file. I already tried to import funcoes_uteis but it is not working. I do not know if this has to do with the version of python I'm using, but by the way, I'm using version 3.6 of python. Can anyone help me with this problem? Thank you in advance!

    
asked by anonymous 15.09.2017 / 22:26

1 answer

1

No PDI.py add:

import funcoes_uteis

And call:

imagem[i][j] = funcoes_uteis.calcular_media(filtro)

Or if you do not want to use as funcoes_uteis.calcular_media call:

from funcoes_uteis import calcular_media

And call:

imagem[i][j] = calcular_media(filtro)

I created two very simple test scripts

PDI.py

import funcoes_uteis

def aplicar_mascara_media_aritmetica():
    return funcoes_uteis.calcular_media(2)

print(aplicar_mascara_media_aritmetica())

funcoes_uteis.py

def calcular_media(imagem):
    return imagem

And then I executed it in cmd:

    
15.09.2017 / 22:29