import cv2
import numpy as np
img = cv2.imread('./card/template.png')
ref = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, ref = cv2.threshold(ref,10,255,cv2.THRESH_BINARY_INV)
ref_contours,_ = cv2.findContours(ref.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img,ref_contours,-1,(0,0,255),2)
bounding_boxes = [cv2.boundingRect(c) for c in ref_contours]
(ref_contours,bounding_boxes) = zip(*sorted(zip(ref_contours,bounding_boxes),key=lambda b: b[1][0]))
print(len(ref_contours))
digits = {
}
for (i, c) in enumerate(ref_contours):
(x,y,w,h)= cv2.boundingRect(c)
roi = ref[y:y+h,x:x+w]
roi = cv2.resize(roi,(57,88))
digits[i] = roi
print(digits)
image = cv2.imread('./card/card-1.png')
h,w = image.shape[:2]
width = 300
r = width / w
cv2.resize(image,(300,int(h*r)))
cv2.imshow('origin',image)
cv2.waitKey(0)
cv2.destroyAllWindows()