0
我需要幫助。 我試圖用Python Pillow庫選擇並裁剪兩個圖像的重疊區域。用枕頭裁剪重疊圖像
我有兩個圖片的左上角像素座標。有了這些,我可以找出哪一個位於另一個之上。
我寫了一個函數,以兩個圖像作爲參數:
def function(img1, img2):
x1 = 223 #x coordinate of the first image
y1 = 197 #y coordinate of the first image
x2 = 255 #x coordinate of the second image
y2 = 197 #y coordinate of the second image
dX = x1 - x2
dY = y1 - y2
if y1 <= y2: #if the first image is above the other
upper = img1
lower = img2
flag = False
else:
upper = img2
lower = img1
flag = True
if dX <= 0: #if the lower image is on the left
box = (abs(dX), abs(dY), upper.size[0], upper.size[1])
a = upper.crop(box)
box = (0, 0, upper.size[0] - abs(dX), upper.size[1] - abs(dY))
b = lower.crop(box)
else:
box = (0, abs(dY), lower.size[0] - abs(dX), upper.size[1])
a = upper.crop(box)
box = (abs(dX), 0, lower.size[0], upper.size[1] - abs(dY))
b = lower.crop(box)
if flag:
return b,a #switch the two images again
else:
return a,b
我知道肯定結果是錯誤的(這是一所學校分配)。 感謝您的幫助。
對不起,我不能很好地寫英文。 「以上」我的意思是重疊。我正在研究二維地面,但我需要使用枕頭。此外,謝謝你的回答,但我需要兩個選定矩形的兩個相對頂點的座標 –