'How to crop sub-part from national ID without losing quality

I am having a use case where dividing National ID card is mandatory for OCR

Received Image vary in size and pixels number

Samples

enter image description here enter image description here

Used the following code to crop the ID part

 # Read the input image
img = cv2.imread(imge)

# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# define range of black color in HSV
lower_val = np.array([0,0,0])
upper_val = np.array([179,255,135])

# Threshold the HSV image to get only black colors
mask = cv2.inRange(hsv, lower_val, upper_val)

# invert mask to get black symbols on white background
mask_inv1 = cv2.bitwise_not(mask)

mask_inv = cv2.blur(mask_inv1,(5,5))

image = mask_inv
(h, w) = image.shape[:2]
center = (w / 2, h / 2)

mid_line= int((center[1]+h)/2)
mid_vertical_line = int((center[0]/2)+(0.45*(center[0]/2))) 
ID = image[mid_line:h,mid_vertical_line:w]

Result Image pixelized and blurry, How I can make clean crop without losing quality

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source