How to isolate a license plate or document from an image using Python I use this code to create really perfect ID scans which contain just the ID from an image. The code looks for the largest square in the image using computer vision. This is useful for OCR, forensics, verification, or any situation where documents are processed. The code can be modified to isolate anything from an image with contours, like a street sign, cell phone, building or anything else.
# isolate the id from the image scan import cv2 def write_isolated(image_path, output_path): image = cv2.imread(image_path) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh_img = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) cnts = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts if len(cnts) == 2 else cnts cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] for c in cnts: perimeter = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.018 * perimeter, True) if len(approx) >= 4: x,y,w,h = cv2.boundingRect(c) new_img = image[y:y+h,x:x+w] cv2.imwrite(output_path, new_img) return output_path return None