在大图中分辨出小图位置的程序
闲暇时写了一个可以在大图中分辨出小图位置的程序。
懒~~
用法:传入两个参数,都为PIL库的Image库的Image类,传出一个列表,内含小图正中坐标。
-------------------------------
from PIL import Image
def findpixel(image1=Image.Image(), image2=Image.Image()):
wantrgb = image2.getpixel((0, 0))
wantsize = image2.size
stoper = []
for y in range(image1.size[1]):
for x in range(image1.size[0]):
rgb = image1.getpixel((x, y))
if rgb == wantrgb:
print("--------------------")
print(
f"find importend pixel at image1 ({x},{y}) by {rgb}")
cox = x+wantsize[0]
coy = y+wantsize[1]
if cox >= image1.size[0] or coy >= image1.size[1]:
continue
copy = image1.crop((x, y, x+wantsize[0], y+wantsize[1]))
metter = False
for by in range(copy.size[1]):
for bx in range(copy.size[0]):
if copy.getpixel((bx, by)) != image2.getpixel((bx, by)):
print(f"not pixel at ({bx},{by}) , not by {copy.getpixel((bx, by))} of {image2.getpixel((bx, by))}")
metter = True
if metter:
break
if metter:
break
if not metter:
print("find a ok image!")
stoper.append([x + int((cox-x)//2), y + int((coy-y)//2)])
return stoper
print(findpixel())#error要写参数