python-iterating dictionary values if one key has one or more values

Viewed 41

I have a program to find the orientation of the image. To make it a straight image if it is in any other orientation, I rotate it using its pred_val (using Pytorch) so I create it as a dictionary where the pred_val as key and image name as a value. I partially achieve it, but as an exception came to know that if one or more images have the same pred_val only the last image name got assigned, and hence I conclude it like this:

for k, v in dictImgNamValues.items(): #k-pred,v-image
    if ',' in v:
        for v in v.split(','):
            imname = v[:-4]
            print(k,v)
            if os.path.isfile(image_folder+'/'+v):
                if k == '0':
                    print('a')
                    image = cv2.imread(rot_img+'/'+file1+'/'+v)
                    rotImg = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
                    cv2.imwrite(save_path+'/'+imname+'.png',rotImg)
                elif k == '1':
                    print('b')
                    image = cv2.imread(rot_img+'/'+file1+'/'+v)
                    rotImg = cv2.rotate(image, cv2.ROTATE_180)
                    cv2.imwrite(save_path+'/'+imname+'.png',rotImg)
                elif k =='2':
                    print('c')
                    image = cv2.imread(rot_img+'/'+file1+'/'+v)
                    rotImg = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
                    cv2.imwrite(save_path+'/'+imname+'.png',rotImg)
                elif k=='3':
                    print('d')
                    image = cv2.imread(rot_img+'/'+file1+'/'+v)
                    cv2.imwrite(save_path+'/'+imname+'.png',image)        
    imname = v[:-4]
    print(k,v)
    if os.path.isfile(image_folder+'/'+v):
        if k == '0':
            print('a')
            image = cv2.imread(rot_img+'/'+file1+'/'+v)
            rotImg = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
            cv2.imwrite(save_path+'/'+imname+'.png',rotImg)
        elif k == '1':
            print('b')
            image = cv2.imread(rot_img+'/'+file1+'/'+v)
            rotImg = cv2.rotate(image, cv2.ROTATE_180)
            cv2.imwrite(save_path+'/'+imname+'.png',rotImg)
        elif k =='2':
            print('c')
            image = cv2.imread(rot_img+'/'+file1+'/'+v)
            rotImg = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
            cv2.imwrite(save_path+'/'+imname+'.png',rotImg)
        elif k=='3':
            print('d')
            image = cv2.imread(rot_img+'/'+file1+'/'+v)
            cv2.imwrite(save_path+'/'+imname+'.png',image)

Looks so Elaborative method is there any other way for the dictionary to short it out?

1 Answers

As far as i understand, in your case dictionary - is just a storage? Then why don't you use list of tuples? I don't see you using any of dict specific functionality, so might just replace it with a better option.

Say:

dictImgNamValues = [(<pred_val1>, <nam1>), (<pred_val2>, <name2>), ...]

Then your code would be almost the same. I've refactored it to reduce code duplication. Don't know why you have part after imname = v[:4] written twice though. Ayway:

rotation = {'0': cv2.ROTATE_90_COUNTERCLOCKWISE,
            '1': cv2.ROTATE_180,
            '2': cv2.ROTATE_90_CLOCKWISE}

for pred_val, name in dictImgNamValues: #k-pred,v-image
    for name in name.split(','):
        imname = name[:-4]
        if os.path.isfile(image_folder +'/' + name):
            image = cv2.imread(rot_img +'/' + file1 +'/' + name)
            if pred_val in rotation:
                image = cv2.rotate(image, rotation[pred_val])
            cv2.imwrite(save_path + '/' + imname + '.png', image)
Related