2016-09-13 10 views
0

私は助けが必要です。 私は、Python Pillowライブラリで2つの画像の重なり領域を選択して切り抜こうとしています。枕で画像を重ね合わせる

私は2枚の写真の左上のピクセル座標を持っています。これらによって、私はどちらが他のものの上にあるのかを知ることができます。

私は、引数として二つの画像取って、機能を書いた:私は(それは学校の割り当てだ)結果が間違っていることを確かに知っ

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 

を。 ご協力いただきありがとうございます。

答えて

0

まず最初に、ある写真が他の写真よりも上にあることを意味するわけではありませんが(Zポジションではないはずですが)、これを見てください:How to make rect from the intersection of two?、最初の答えは良いリードかもしれません。 :)

+0

申し訳ありませんが、私は非常によく英語を書くことができません。 「上」とは重複を意味していました。私は二次元の地面で作業していますが、私は枕を使用する必要があります。また、あなたの答えをありがとうが、私は2つの選択された矩形の2つの対向する頂点の座標が必要です –

関連する問題