【发布时间】:2016-09-13 20:16:30
【问题描述】:
我需要帮助,请。 我正在尝试使用 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
我确定结果是错误的(这是学校作业)。 感谢您的帮助。
【问题讨论】: