python-3.x - 如何根据另一个引用点调整图像大小

标签 python-3.x python-imaging-library

我目前有两张图像(im1 和 im2),像素尺寸为 (1725, 1580)。然而,im2 周围有一个大边框,我必须创建它以确保图像大小相同。 im2 最初是 (1152, 864)。

因此。当我使用 PIL.Image.blend 将 im2 覆盖在 im1 上时,im2 似乎覆盖在 im1 上,但要小得多。我在图像上有 2 个不同的引用点,我认为我可以使用(存在于 im1 和 im2 上)重新缩放 im2(以某种方式放大它?)以将 im2 覆盖在 im1 上。

我的问题是,我一直在查看各种 python 模块(PIL、scipy、matplotlib 等),但似乎无法真正取得进展或找到可以解决此问题的解决方案。

我有 2 个引用点,我认为我可以使用(存在于 im1 和 im2 上)来重新缩放 im2(以某种方式放大它?)以将 im2 覆盖在 im1 之上。

我查看了各种模块,但似乎无法找到任何可能有效的模块(scipy、PIL、matplotlib)

#im1 https://i.imgur.com/dF8uyPw.jpg
#im2 https://i.imgur.com/o4RAhOQ.png
#im2_resized https://i.imgur.com/jfWz1LE.png

im1 = Image.open("pit5Film/Pit_5_5mm_inf.tif")
im2 = Image.open("pit5Overlay/overlay_132.png")

old_size = im2.size
new_size = im1.size
im2_resized = Image.new("RGB", new_size) 
im2_resized.paste(im2,((round((new_size[0]-old_size[0])/2)),round(((new_size[1]-old_size[1])/2))))

Image.blend(im1,im2_resized,0.2)

最佳答案

我认为您正在尝试进行“仿射失真”。我也许可以在 OpenCVPIL 中弄清楚如何做到这一点,但目前,这是我用 ImageMagick 所做的。

首先,我在第一张图片的左右两侧找到了定位孔 (?) 的中心。我得到了这些坐标:

422,775    # left hole centre 1st picture
1246,799   # right hole centre 1st picture

enter image description here

然后我在第二张图片中发现了这些相同的特征:

514,426    # left hole centre 2nd picture
668,426    # right hole centre 2nd picture

enter image description here

然后我在终端中运行它来进行 2 点仿射变换:

convert imageA.jpg -virtual-pixel white                        \
    -distort affine '422,775 514,426 1246,799 668,426' +repage \
    imageB.png -compose overlay -composite result.jpg

enter image description here

Anthony Thyssen 提供了大量重要信息 here如果你喜欢阅读。

关于python-3.x - 如何根据另一个引用点调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055375/

相关文章:

数组操作的 Python 属性

Python排序列表设置

python - 正则表达式在 Rubular 中传递,但在 Python 中不传递

linux - 在 lubuntu 16.04 上安装 xv 实用程序

python - PIL , im.getdata() 返回整数而不是元组

python - 使用 Python 图像库进行抗锯齿时,使用透明消除边缘处的细边框

python - pandas如何根据df中的其他 bool 列创建 bool 列

python-3.x - 获取从 "/etc/group"开始排序的组 ID

python - 如何使用 Python Pillow 定义模糊半径?