【问题标题】:Merging perspective corrected image with transparent background template image using PILLOW [PIL, Python]使用 PILLOW [PIL,Python] 将透视校正图像与透明背景模板图像合并
【发布时间】:2020-04-15 11:24:49
【问题描述】:

问题:我有多个书籍封面图片。我用 3D 透视制作了一个类似“书”的模板。现在我要做的就是获取每本书的封面图像,校正透视图(它始终不变,因为模板始终不变)并将我的透视校正图像与模板(背景/画布)合并。

为了更容易理解 - 以下是在 Adob​​e Photoshop 中创建的示例:

我尝试用红色箭头显示原始封面图像的顶点(在透视校正之前)。如您所见,右侧的 2 个顶点必须保留。左边的另外两个点总是要修正。

你能告诉我如何实现吗?

更新 我有什么:

1) 覆盖自己

2) 具有透明背景的模板:

我需要转换封面的视角并将其与模板图像合并

【问题讨论】:

  • 请展示您实际拥有的和您真正想要的。我不知道你是否有一个用平行于书籍封面的相机拍摄的矩形图像,并且想要透视将其扭曲成梯形,或者如果你有一个梯形图像可能在棋盘背景上,也许没有,你想得到一个矩形图像。
  • @MarkSetchell 请看一下。主帖已更新。谢谢

标签: python image-processing python-imaging-library coordinate-transformation


【解决方案1】:

您实际上不需要编写任何 Python,您可以在终端中使用 ImageMagick 使用 “透视变换” 来完成,如下所示:

magick cover.png -virtual-pixel none -distort perspective "0,0 96,89 %w,0 325,63 %w,%h 326,522 0,%h 96,491" template.png +swap -flatten result.png

查看透视变换的参数,您有望看到有 4 对坐标,变换的每个角各有一对,显示源位置如何映射到输出图像中。

因此,封面的左上角 (0,0) 映射到模板中空白区域的左上角 (96,89)。封面的右上角 (width,0) 映射到模板空白区域的右上角 (325,63)。封面的右下角(宽度,高度)映射到模板(326,522)上空白区域的右下角。封面的左下角 (0,height) 映射到模板空白区域的左下角 (96,491)。

如果您使用的是旧 v6 ImageMagick,请将 magick 替换为 convert


请注意,如果您真的想在 Python 中执行此操作,则有一个名为 wandhere 的 Python 绑定。我对wand 不是很有经验,但这似乎是等价的:

#!/usr/bin/env python3

from itertools import chain
from wand.color import Color
from wand.image import Image

with Image(filename='cover.png') as cover, Image(filename='template.png') as template:
    w, h = cover.size
    cover.virtual_pixel = 'transparent'
    source_points = (
        (0, 0),
        (w, 0),
        (w, h),
        (0, h)
    )
    destination_points = (
        (96, 89),
        (325, 63),
        (326, 522),
        (96, 491)
    )
    order = chain.from_iterable(zip(source_points, destination_points))
    arguments = list(chain.from_iterable(order))
    cover.distort('perspective', arguments)

    # Overlay cover onto template and save
    template.composite(cover,left=0,top=0)
    template.save(filename='result.png')

关键字:Python、ImageMagick、魔杖、图像处理、透视变换、扭曲。

【讨论】:

猜你喜欢
  • 2012-04-14
  • 2019-02-08
  • 2016-12-08
  • 2023-03-17
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
相关资源
最近更新 更多