Last active
August 27, 2021 04:44
-
-
Save oofnivek/b3b8569c5a07e892c7ee12468b2f51e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
#import time | |
import numpy | |
from PIL import Image | |
def find_image(png_big, png_small): | |
arr_big = numpy.array(png_big) | |
arr_small = numpy.array(png_small) | |
if len(arr_big) < len(arr_small): | |
return [-2,-2] # unable to search a bigger picture in a smaller picture | |
total = len(arr_small) * len(arr_small[0]) | |
for y_big in range(0, len(arr_big)-len(arr_small)): | |
for x_big in range(0, len(arr_big[y_big])-len(arr_small[0])): | |
found = 0 | |
for y_small in range(0, len(arr_small)): | |
for x_small in range(0, len(arr_small[0])): | |
a = x_big + x_small | |
b = y_big + y_small | |
p = x_small | |
q = y_small | |
#print('BIG={0},{1} SMALL={2},{3}'.format(a,b,p,q)) | |
#time.sleep(0.1) | |
pixel_big = arr_big[b][a] | |
pixel_small = arr_small[q][p] | |
if not isinstance(pixel_big,numpy.uint8): | |
pixel_big = list(arr_big[b][a]) | |
pixel_small = list(arr_small[q][p]) | |
if pixel_big != pixel_small: | |
break # mismatch | |
else: | |
found = found + 1 | |
if found == total: | |
# print('TOP LEFT x={0},y={1}'.format(x_big,y_big)) | |
return [x_big,y_big] | |
return [-1,-1] # not found | |
png_big = Image.open(sys.argv[1]) | |
png_small = Image.open(sys.argv[2]) | |
print('top left = {0}'.format(find_image(png_big,png_small))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment