使用PILLOW [PIL,Python]将透视校正后的图像与透明背景模板图像合并 [英] Merging perspective corrected image with transparent background template image using PILLOW [PIL, Python]

查看:668
本文介绍了使用PILLOW [PIL,Python]将透视校正后的图像与透明背景模板图像合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有多个书的封面图像.我制作了一个具有3D透视图的类似书"样的模板.现在,我要做的就是获取每本书的封面图像,校正透视图(由于模板始终不变,因此始终保持不变),然后将经过透视图校正的图像与模板(背景/画布)合并.

Problem: I have multiple book cover images. I made a template of "book"-like template with a 3D perspective. 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).

为便于理解-以下是在Adobe Photoshop中创建的示例:

For easier understanding - here is an example created in Adobe Photoshop:

我尝试使用红色箭头显示原始封面图像的顶点(在透视校正之前).如您所见,右边的2个顶点必须保留.左边的其他两个点必须始终保持不变.

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. The other two points of the left have to be corrected always the same.

您能告诉我如何实现吗?

Can you please show me how to achieve that?

更新 我所拥有的:

1)掩盖自己

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

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

I need to transform perspective of cover and merge it with template image

推荐答案

您实际上并不需要编写任何Python,只需在 ImageMagick 中使用在终端中进行操作即可.像这样的透视变换" :

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:

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对坐标,变换的每个角分别有一对,显示了如何在输出图像中映射源位置.

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.

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

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). The top right of the cover (width,0) gets mapped to the top-right of the empty area of the template (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). The bottom-left of the cover (0,height) gets mapped to the bottom-left corner of the empty area of the template (96,491).

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

If you are using the old v6 ImageMagick, replace magick with convert.

请注意,如果您真的想用Python进行此操作,则有一个名为wand的Python绑定这里.我对wand不太熟悉,但这似乎是等效的:

Note that, if you really want to do it in Python, there is a Python binding called wand here. I am not very experienced with wand but this seems to be equivalent:

#!/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')

这篇关于使用PILLOW [PIL,Python]将透视校正后的图像与透明背景模板图像合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆