【问题标题】:Crop overlapping images with pillow用枕头裁剪重叠图像
【发布时间】: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

我确定结果是错误的(这是学校作业)。 感谢您的帮助。

【问题讨论】:

    标签: python pillow


    【解决方案1】:

    首先,我不太明白一张图片“高于”另一张图片是什么意思(这不应该是 z 位置吗?),但请看一下:How to make rect from the intersection of two?,第一个答案可能是一个很好的线索。 :)

    【讨论】:

    • 对不起,我的英文写得不太好。 “上面”是指重叠。我正在二维地面上工作,但我需要使用枕头。另外,感谢您的回答,但我需要两个选定矩形的两个相对顶点的坐标
    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多