1

I got a error saying:

AttributeError: 'tuple' object has no attribute 'append' means or fixed?

How do I correct it? move.py


    image = cv2.imread('C:/Users/user/Desktop/line.jpg')
    image_gray = cv2.imread('C:/Users/user/Desktop/line.jpg', cv2.IMREAD_GRAYSCALE)
    image = cv2.resize(image, (80, 80))
    image_gray = cv2.resize(image_gray, (80,80))
    blur = cv2.GaussianBlur(image_gray, ksize=(5,5), sigmaX=0)
    ret, thresh = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY)
    edged = cv2.Canny(blur, 10, 250)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
    closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)  #important
    
    # equation of a straight line
    x1, y1, x2, y2 = 81, 63, 1035, 679
    a = (y1 - y2) / (x1 - x2)
    b = y1 - (a * x1)
    X = sympy.symbols("X")
    
    func = a*X + b
    previous_count = 0
    count = 0

    # distance between a point and a straight line
    def dist(P, A, B):
        area = abs((A[0] - P[0]) * (B[1] - P[1]) - (A[1] - P[1]) * (B[0] - P[0]))
        AB = ((A[0] - B[0]) ** 2 + (A[1] - B[1]) ** 2) ** 0.5
        return (area / AB)

    for i in range(closed.shape[0]):
        for j in range(closed.shape[1]):
            if closed[i][j] == 255:          
                P = (0, 25)
                A0, A1, A2, A3, A4 = (81, 61), (81, 62), (81, 63), (81, 64), (81, 65)
                B0, B1, B2, B3, B4 = (1035, 677), (1035, 678), (1035, 679), (1035, 680), 
                                     (1035, 681)
                # Define (P, A, B) again
                P = P.append((closed[i], closed[j]))
                A = A0, A1, A2, A3, A4
                B = B0, B1, B2, B3, B4
                print('-------------------------- closed[i][j] print --------------------------')
                print(closed[i][j])
                print()
                print('---------------------------- i, j print -------------------------------' )
                print(i, j)
                print()
                print('----------------------- dist(P, A[i], B[i] print -----------------------')
                
                for i in range(0, len(A)):
                    print(dist(P, A[i], B[i]))

                if dist(P, A[i], B[i]) < 1:
                    count += 1
if count > previous_count:
    previous_count = count
    print("-----------------count print----------------------")
    print(count)
    print("-----------------previous_count print----------------------")
    print(previous_count)

    **error**
    File "move.py", line 71, in <module>
      P = P.append((closed[i], closed[j]))
AttributeError: 'tuple' object has no attribute 'append'

I know that tuples cannot be modified by adding, removing, etc. I made P =[], and I used append. And I changed P = [()] also.

P = []  # P = [()] also I did, but it doesn't work.
P = P. append((closed[i], closed[j]))

But it doesn't work. Like this.

  File "move.py", line 84, in <module>
    print(dist(P, A[i], B[i]))
  File "move.py", line 58, in dist
    area = abs((A[0] - P[0]) * (B[1] - P[1]) - (A[1] - P[1]) * (B[0] - P[0]))
TypeError: 'NoneType' object is not subscriptable

Tuple can't allow modify, but I can't think append to tuple. What should I do? I'm really frustrated... Thanks for reading this long post!

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
basicer
  • 19
  • 1
  • What's this line `P = P.append((closed[i], closed[j]))` supposed top do? https://docs.python.org/3/tutorial/datastructures.html#data-structures (*append* doesn't return anything (**!!! or returns *None* !!!**), just modifies the current list). – CristiFati Aug 23 '22 at 10:59
  • If closed[i][j] == 255: there are multiple coordinates of a point, P is that point. However, if I put several items at once, an error occurs and there are too many, so I tried to add them like that. The return value is None, but I don't know how to do it. – basicer Aug 23 '22 at 11:19
  • I thought (after all the details provided) it was obvious: get rid of `P =`. – CristiFati Aug 23 '22 at 11:20
  • I'm so sorry. It's my negligence. Can you please understand the beginner? How can I fix it? – basicer Aug 23 '22 at 11:25
  • `P.append((closed[i], closed[j]))`. – CristiFati Aug 23 '22 at 12:54
  • you have `P = (0, 25)` which assigns `tuple` - and this can make first problem. Second problem needs only `P.append((closed[i], closed[j]))` without `P =` – furas Aug 23 '22 at 16:38

0 Answers0