Reputation: 33
I want to find the default location of the CAPTCHA picture slider and get the coordinates of the default location, and I have processed the two images. images like after https://i.loli.net/2019/07/31/5d412d6c7d95e29689.png https://i.loli.net/2019/07/31/5d412d6c7e25770645.png
python3 PIL lib
im1 = Image.open("frame.png")
im2 = Image.open("frame1.png")
diff = ImageChops.difference(im2, im1).getbbox()
(29, 65, 289, 151) is not the result.
Upvotes: 1
Views: 1215
Reputation: 1129
Try this python code. Image locations will be written to imagePositions.txt:
#!/usr/bin/env python
import cv2
import imutils
import numpy as np
import argparse
import os, shutil
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-b", "--beforeimage", required=True,
help="path to first image")
ap.add_argument("-i", "--image", required=True,
help="path to 2nd image")
args = vars(ap.parse_args())
# load the example image and convert it to grayscale
image = cv2.imread(args["image"])
originalImage = image #save copy to use later
beforeimage = cv2.imread(args["beforeimage"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
beforeimage = cv2.cvtColor(beforeimage, cv2.COLOR_BGR2GRAY)
res = cv2.bitwise_xor(image,beforeimage, mask= beforeimage)
if os.path.exists("imageDiff.png"):
os.remove("imageDiff.png")
if os.path.exists("imagePositions.txt"):
os.remove('imagePositions.txt')
cv2.imwrite('imageDiff.png', res)
image = res
image = (255-image) #invert black and white
# threshold the image (convert it to pure black and white)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# find the contours (continuous blobs of pixels) the image
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Hack for compatibility with different OpenCV versions
contours = contours[0] if imutils.is_cv2() else contours[1]
with open('imagePositions.txt', 'w+') as out:
# Now we can loop through each of the four contours and extract the letter
# inside of each one
cntr = 1
for contour in contours:
# Get the rectangle that contains the contour
(x, y, w, h) = cv2.boundingRect(contour)
#check for needed size here
if (w > 3 and h > 3 ):
cv2.rectangle(image, (x,y), (x + w, y + h), (0, 0, 255), 2)
#add some surrounding area we require
y1 = y -20
if y1 < 0:
y1 = 0
h1 = h + 40
x1 = x - 30
if x1 < 0:
x1 = 0
w1 = w + 60
crop_img = originalImage[y1:y1+h1, x1:x1+w1]
#We have a folder to store cropped images
cv2.imwrite('ImageDifferences/' + str(cntr) + '.png', crop_img)
out.write(str(cntr) + '.png : ' + str(x1+w1/2) + ',' + str(y1+h1/2) + '\n')
cntr = cntr + 1
Upvotes: 1