Is there a way to automate similar (almost repetitive) code and make it like a function?

Viewed 59

Here is the code: it's may not make a lot of sense as such but my question isn't so much to do with syntax.

Essentially ... there are upto 7 images and it arranges in predefined layouts and other stuff ... how do i make this repetitive, non expandable code more streamlined?

im1 = None
im2 = None
im3 = None
im4 = None
im5 = None
im6 = None
im7 = None

if com == 2:
#    cv2.imshow("")
    layout = int(input("Enter layout number: "))
    im1 = im_1()
    im2 = im_2()
    while (im1 == im2):
        print("Invalid")
        im1 = im_1()
        im2 = im_2()
elif com == 3:
#    cv2.imshow("")
    layout = int(input("Enter layout number: "))
    im1 = im_1()
    im2 = im_2()
    im3 = im_3()
    while (im1 == im2) | (im1 == im3) | (im2 == im3):
        print("Invalid")
        im1 = im_1()
        im2 = im_2()
        im3 = im_3()
elif com == 4:
#    cv2.imshow("")
    layout = int(input("Enter layout number: "))
    im1 = im_1()
    im2 = im_2()
    im3 = im_3()
    im4 = im_4()
    while (im1 == im2) | (im1 == im3) | (im1 == im4) | (im2 == im3) | (im2 == im4) | (im3 == im4):
        print("Invalid")
        im1 = im_1()
        im2 = im_2()
        im3 = im_3()
        im4 = im_4()
elif com == 5:
#    cv2.imshow("Layouts: 5", l5)
    layout = int(input("Enter layout number: "))
    im1 = im_1()
    im2 = im_2()
    im3 = im_3()
    im4 = im_4()
    im5 = im_5()
    while (im1 == im2) | (im1 == im3) | (im1 == im4) | (im1 == im5) | (im2 == im3) | (im2 == im4) | (im2 == im5) | (im3 == im4) | (im3 == im5) | (im4 == im5):
        print("Invalid")
        im1 = im_1()
        im2 = im_2()
        im3 = im_3()
        im4 = im_4()
        im5 = im_5()
elif com == 6:
#    cv2.imshow("")
    layout = int(input("Enter layout number: "))
    im1 = im_1()
    im2 = im_2()
    im3 = im_3()
    im4 = im_4()
    im5 = im_5()
    im6 = im_6()
    while (im1 == im2) | (im1 == im3) | (im1 == im4) | (im1 == im5) | (im1 == im6) | (im2 == im3) | (im2 == im4) | (im2 == im5) | (im2 == im6) | (im3 == im4) | (im3 == im5) | (im3 == im6) | (im4 == im5) | (im4 == im6) | (im5 == im6):
        print("Invalid")
        im1 = im_1()
        im2 = im_2()
        im3 = im_3()
        im4 = im_4()
        im5 = im_5()
        im6 = im_6()
elif com == 7:
#    cv2.imshow("")
    layout = int(input("Enter layout number: "))
    im1 = im_1()
    im2 = im_2()
    im3 = im_3()
    im4 = im_4()
    im5 = im_5()
    im6 = im_6()
    im7 = im_7()
    while (im1 == im2) | (im1 == im3) | (im1 == im4) | (im1 == im5) | (im1 == im6) | (im1 == im7) | (im2 == im3) | (im2 == im4) | (im2 == im5) | (im2 == im6) | (im2 == im7) | (im3 == im4) | (im3 == im5) | (im3 == im6) | (im3 == im7) | (im4 == im5) | (im4 == im6) | (im4 == im7) | (im5 == im6) | (im5 == im7) | (im6 == im7):
        print("Invalid")
        im1 = im_1()
        im2 = im_2()
        im3 = im_3()
        im4 = im_4()
        im5 = im_5()
        im6 = im_6()
        im7 = im_7()

It's a computer vision code that compares images, but rn it only works up to 7 because i have to predefine the images before hand. com is actually user inputed and if it is < 7 ... memory is still used as 7 images are defined, and it doesn't allow com to be >7.

Also, if you see, it only works till 7 as the image comparison hasn't been hardcoded for >7. But the logic for com = n is very easy to hardcode but just laborious. Is there a way to make this code for n images/terms where it defines the exact number of variables and self right the comparison/layout section for com = n?

Similarly, can we extend that to this:

if i != 0:
                if i == 1:
                    anim1 = cv2.imread("anim_1.jpg")
                    border_b = cv2.copyMakeBorder(anim1, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim1 = border_w
                if i == 2:
                    anim2 = cv2.imread("anim_2.jpg")
                    border_b = cv2.copyMakeBorder(anim2, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim2 = border_w
                if i == 3:
                    anim3 = cv2.imread("anim_3.jpg")
                    border_b = cv2.copyMakeBorder(anim3, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim3 = border_w
                if i == 4:
                    anim4 = cv2.imread("anim_4.jpg")
                    border_b = cv2.copyMakeBorder(anim4, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim4 = border_w
                if i == 5:
                    anim5 = cv2.imread("anim_5.jpg")
                    border_b = cv2.copyMakeBorder(anim5, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim5 = border_w
                if i == 6:
                    anim6 = cv2.imread("anim_6.jpg")
                    border_b = cv2.copyMakeBorder(anim6, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim6 = border_w
                if i == 7:
                    anim7 = cv2.imread("anim_7.jpg")
                    border_b = cv2.copyMakeBorder(anim7, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
                    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
                    anim7 = border_w

and

if com == 2:
    if im1 == 1:
        im1 = anim1
        im2 = anim2
    elif im1 == 2:
        im1 = anim2
        im2 = anim1
elif com == 3:
    if (im1 == 1) | (im1 == 2) | (im1 == 3):
        if im1 == 1:
            im1 = anim1
        elif im1 == 2:
            im1 = anim2
        elif im1 == 3:
            im1 = anim3
    if (im2 == 1) | (im2 == 2) | (im2 == 3):
        if im2 == 1:
            im2 = anim1
        elif im2 == 2:
            im2 = anim2
        elif im2 == 3:
            im2 = anim3
    if (im3 == 1) | (im3 == 2) | (im3 == 3):
        if im3 == 1:
            im3 = anim1
        elif im3 == 2:
            im3 = anim2
        elif im3 == 3:
            im3 = anim3

//skipped till end of 7 because too repetitive.

elif com == 7:
    if (im1 == 1) | (im1 == 2) | (im1 == 3) | (im1 == 4) | (im1 == 5) | (im1 == 6) | (im1 == 7):
        if im1 == 1:
            im1 = anim1
        elif im1 == 2:
            im1 = anim2
        elif im1 == 3:
            im1 = anim3
        elif im1 == 4:
            im1 = anim4
        elif im1 == 5:
            im1 = anim5
        elif im1 == 6:
            im1 = anim6
        elif im1 == 7:
            im1 = anim7
    if (im2 == 1) | (im2 == 2) | (im2 == 3) | (im2 == 4) | (im2 == 5) | (im2 == 6) | (im2 == 7):
        if im2 == 1:
            im2 = anim1
        elif im2 == 2:
            im2 = anim2
        elif im2 == 3:
            im2 = anim3
        elif im2 == 4:
            im2 = anim4
        elif im2 == 5:
            im2 = anim5
        elif im2 == 6:
            im2 = anim6
        elif im2 == 7:
            im2 = anim7
    if (im3 == 1) | (im3 == 2) | (im3 == 3) | (im3 == 4) | (im3 == 5) | (im3 == 6) | (im3 == 7):
        if im3 == 1:
            im3 = anim1
        elif im3 == 2:
            im3 = anim2
        elif im3 == 3:
            im3 = anim3
        elif im3 == 4:
            im3 = anim4
        elif im3 == 5:
            im3 = anim5
        elif im3 == 6:
            im3 = anim6
        elif im3 == 7:
            im3 = anim7
    if (im4 == 1) | (im4 == 2) | (im4 == 3) | (im4 == 4) | (im4 == 5) | (im4 == 6) | (im4 == 7):
        if im4 == 1:
            im4 = anim1
        elif im4 == 2:
            im4 = anim2
        elif im4 == 3:
            im4 = anim3
        elif im4 == 4:
            im4 = anim4
        elif im4 == 5:
            im4 = anim5
        elif im4 == 6:
            im4 = anim6
        elif im4 == 7:
            im4 = anim7
    if (im5 == 1) | (im5 == 2) | (im5 == 3) | (im5 == 4) | (im5 == 5) | (im5 == 6) | (im5 == 7):
        if im5 == 1:
            im5 = anim1
        elif im5 == 2:
            im5 = anim2
        elif im5 == 3:
            im5 = anim3
        elif im5 == 4:
            im5 = anim4
        elif im5 == 5:
            im5 = anim5
        elif im5 == 6:
            im5 = anim6
        elif im5 == 7:
            im5 = anim7
    if (im6 == 1) | (im6 == 2) | (im6 == 3) | (im6 == 4) | (im6 == 5) | (im6 == 6) | (im6 == 7):
        if im6 == 1:
            im6 = anim1
        elif im6 == 2:
            im6 = anim2
        elif im6 == 3:
            im6 = anim3
        elif im6 == 4:
            im6 = anim4
        elif im6 == 5:
            im6 = anim5
        elif im6 == 6:
            im6 = anim6
        elif im6 == 7:
            im6 = anim7
    if (im7 == 1) | (im7 == 2) | (im7 == 3) | (im7 == 4) | (im7 == 5) | (im7 == 6) | (im7 == 7):
        if im7 == 1:
            im7 = anim1
        elif im7 == 2:
            im7 = anim2
        elif im7 == 3:
            im7 = anim3
        elif im7 == 4:
            im7 = anim4
        elif im7 == 5:
            im7 = anim5
        elif im7 == 6:
            im7 = anim6
        elif im7 == 7:
            im7 = anim7

tl;dr: how do i make this unnecessarily repetitive code more streamlined?

2 Answers

Code block 1:

See if you can only define things when you need them

Code block 2:

"anim_"+i+".jpg"

Code block 3:

if im1 == 1) | (im1 == 2) ... | (im1 == 7):

should just be

if im1 <= 7:

For the long elif chains, use a list

Anim = [anim0, anim1, anim2]
im1 = Anim[0]

I think you would save time learning more of the basics before creating large projects



# example code 

TmpLayout1 = [[1,0],[0,1]]
TmpLayout2 = [[0,1],[0,1]]
ImageLayouts = [TmpLayout1, TmpLayout2]

The first piece of code could use a function to get rid of the nasty boolean logic and arrays and loops to get rid of the hardcoded variables.

# Replaces im1 - im7
im_values = []
for i in range(7):
    im_values.append(None)

# Map functions to array
im_functions = []
im_functions.append(im_1)
im_functions.append(im_2)
im_functions.append(im_3)
im_functions.append(im_4)
im_functions.append(im_5)
im_functions.append(im_6)
im_functions.append(im_7)

# Checks the long if-tests in your code, a helper function
def any_equal(n):
    for i in range(n-1):
        for j in range(i+1, n):
            if (im_values[i] == im_values[j]):
                return True
    return False

# All hardcoded if tests are replaced with a for loop
#    cv2.imshow("")
layout = int(input("Enter layout number: "))
for i in range(com):
    im_values[i] = im_functions[i]()
    while(any_equal(com)):
        print("Invalid")
        for j in range(com):
            im_values[j] = im_functions[j]()

The second part of the code looks like it could be replaced with a simple function.

anims = []
for i in range(7):
    anims.append(None)

def load_anim(img):
    anim = cv2.imread(img)
    border_b = cv2.copyMakeBorder(anim, bt, bt, bt, bt, cv2.BORDER_CONSTANT, value = c_b)
    border_w = cv2.copyMakeBorder(border_b, wt, wt, wt, wt, cv2.BORDER_CONSTANT, value = c_w)
    return border_w

if i != 0:
    anims[i-1] = load_anim(f"anim_{i}.jpg")

The third part looks similar, just use arrays and loops and you should get it done. I leave that for you to solve yourself, it shouldn't be too hard hopefully :)

Related