简体   繁体   中英

Conditional file loading and labeling python

I'm trying to make a function that loads certain images from a directory. The condition is that if the image name contains a certain number, then it should be loaded as an object (with a name and number).

Here's what I've done so far:

get_image_data([71,72,82,105...])
directory = os.listdir("/Users/Me/Desktop/path2images/")


#Sample of files
0070_pressure_pred.png 
0070_pressure_target.png 
0070_velX_pred.png 
0070_velX_target.png 
0070_velY_pred.png 
0070_velY_target.png 
0071_pressure_pred.png 
0071_pressure_target.png 
0071_velX_pred.png 
0071_velX_target.png 
0071_velY_pred.png 
0071_velY_target.png 
0072_pressure_pred.png 
0072_pressure_target.png 
0072_velX_pred.png 

Function:

def get_pressure_prediction(file_numbers):
#takes  a list of image number and returns them 
for files in directory:
    for x in file_numbers:
        if str(x) in files[:4]:
            print("File '{}' matched".format(files))  #Code functions fine until here
            if str(pressure_pred) in files:
                "pp{}".format(x) = mpimg.imread(path + files) #attempting to load image and label it with pp{image number}
            elif str(pressure_target) in files:
                "pt{}".format(x) = mpimg.imread(path + files)
            elif str(velX_pred) in files:
                "vxp{}".format(x) = mpimg.imread(path + files)
            elif str(velX_target) in files:
                "vxt{}".format(x) = mpimg.imread(path + files)
            elif str(velY_pred) in files:
                "vyp{}".format(x) = mpimg.imread(path + files)
            elif str(velY_target) in files:
                "vyt{}".format(x) = mpimg.imread(path + files)   
            break

I get this error message:

    "vyt{}".format(x) = mpimg.imread(path + files)
^
SyntaxError: can't assign to function call

You can't conditionally label a variable.

"pp{}".format(x) is a string and variable names cant be strings. For example "foo" = 0 will give you a syntax error.

Here is another example of something that won't work:

for i in range(5):
    "variable{i}" = i

I recommend creating a class to store the image's name and path and then append the images to a list, then you can do whatever you want with the images from the list. Example:

class Image:
    def __init__(self, label, filePath):
        self.label = label
        self.image = mpimg.imread(filePath)

images = []  

if str(x) in files[:4]:
    print("File '{}' matched".format(files))  
    if str(pressure_pred) in files:
        images.append(Image("pp{}".format(x), path + files)) 
    elif str(pressure_target) in files:
        images.append(Image("pt{}".format(x), path + files))
    elif str(velX_pred) in files:
        images.append(Image("vxp{}".format(x), path + files))
    elif str(velX_target) in files:
        images.append(Image("vxt{}".format(x), path + files))
    elif str(velY_pred) in files:
        images.append(Image("vyp{}".format(x), path + files))
    elif str(velY_target) in files:
        images.append(Image("vyt{}".format(x), path + files))
    break

You can't assign into a string ( "vyt{}".format(x) is string) another value. I would suggest you to assign the function output to a designated key in a dictionary:

mapping = dict()
if str(pressure_pred) in files:
                mapping["pp{}".format(x)] = mpimg.imread(path + files) 
elif ...
...

# You will have to change it in all your conditions
# and then you will be able to access it as follows:
res = mapping["vytX"] # where vytX is the desired variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM