简体   繁体   English

使用 PILLOW [PIL,Python] 将透视校正图像与透明背景模板图像合并

[英]Merging perspective corrected image with transparent background template image using PILLOW [PIL, Python]

Problem: I have multiple book cover images.问题:我有多个书籍封面图片。 I made a template of "book"-like template with a 3D perspective.我用 3D 视角制作了一个类似于“书”的模板。 And all I have to do now its take each of book cover images, correct a perspective (its always constant, because the template is always unchanged) and merge my perspective corrected image with the template (background/canvas).我现在要做的就是拍摄每本书的封面图像,校正透视图(它始终不变,因为模板始终不变)并将我的透视校正图像与模板(背景/画布)合并。

For easier understanding - here is an example created in Adobe Photoshop:为了更容易理解 - 这是在 Adobe Photoshop 中创建的示例: 在此处输入图像描述

With red arrows I tried to show vertex points of the original cover image (before perspective correction).我试图用红色箭头显示原始封面图像的顶点(在透视校正之前)。 As you can see, 2 vertex points on the right have to stay.如您所见,右侧的 2 个顶点必须保留。 The other two points of the left have to be corrected always the same.左边的其他两点必须始终保持不变。

Can you please show me how to achieve that?你能告诉我如何实现吗?

UPDATE What I have:更新我有什么:

1) Cover itself 1)覆盖自己在此处输入图像描述

2) Template with transparent background: 2)透明背景模板: 在此处输入图像描述

I need to transform perspective of cover and merge it with template image我需要转换封面的透视并将其与模板图像合并

You don't really need to write any Python, you can just do it in the Terminal with ImageMagick using a "Perspective Transform" like this:你真的不需要写任何 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

在此处输入图像描述

Looking at the parameters to the perspective transform, you can hopefully see there are 4 pairs of coordinates, one pair for each corner of the transform showing how the source location gets mapped in the output image.查看透视变换的参数,您可以看到有 4 对坐标,变换的每个角各一对,显示源位置如何映射到 output 图像中。

So, the top-left corner of the cover (0,0) gets mapped to the top-left of the empty area in the template (96,89).因此,封面的左上角 (0,0) 被映射到模板中空白区域的左上角 (96,89)。 The top right of the cover (width,0) gets mapped to the top-right of the empty area of the template (325,63).封面的右上角 (width,0) 被映射到模板空白区域的右上角 (325,63)。 The bottom-right of the cover (width,height) gets mapped to the bottom-right of the empty area on the template (326,522).封面的右下角(宽度、高度)被映射到模板空白区域的右下角 (326,522)。 The bottom-left of the cover (0,height) gets mapped to the bottom-left corner of the empty area of the template (96,491).封面的左下角 (0,height) 被映射到模板空白区域 (96,491) 的左下角。

If you are using the old v6 ImageMagick , replace magick with convert .如果您使用的是旧版 v6 ImageMagick ,请将magick替换为convert


Note that, if you really want to do it in Python, there is a Python binding called wand here .请注意,如果您真的想在 Python 中执行此操作,这里有一个名为wand的 Python 绑定。 I am not very experienced with wand but this seems to be equivalent:我对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')

Keywords : Python, ImageMagick, wand, image processing, perspective transform, distort.关键词:Python、ImageMagick、魔杖、图像处理、透视变换、扭曲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM