I have an image that is an output of some mobile scanner. When the images from the mobile scanner app, the images are placed inside a rectangular box and the border has the advt / information of the scanner app. The following is the image:
In the above image `Scanned by TapScanner' is the advt infromation. I got a code from stackoverflow to remove the image borders, it is able to remove the border from the top part but because of the text in the lower part it is not able to remove the border from below.
The following is the code:
from PIL import Image, ImageChops
def trim(image_path):
im = Image.open(image_path)
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
crop_image = trim(image_path)
The following is the current output:
Is it possible to only get the actual image from the whole image. Kindly help. I am new to image processing.
Thank you.
After some searching I found a decent solution, and is as follows:
import opencv2
image = cv2.imread('cheque.jpg')
# convert to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect edges uing canny
edged = cv2.Canny(gray, 1, 1)
# find the countours
contours, hierarchy = cv2.findContours(edged,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# take the maximum counter using "length" instead of "area"
contour = max(contours, key=len)
# get the edges of the counter
x, y, w, h = cv2.boundingRect(contour)
# crop the counter and save it
roi = image[y:y+h, x:x+w]
cv2.imwrite('cheque-crop.jpg', roi)