在大圖中分辨出小圖位置的程序
閑暇時寫了一個可以在大圖中分辨出小圖位置的程序。
懶~~
用法:傳入兩個參數,都為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要寫參數