A detailed article that explains the above code can be found here: https://livefiredev.com/how-to-extract-table-from-image-in-python-opencv-ocr Please look at it in order to understand the code better.