python图片矫正后对比_使用PILLOW [PIL,Python]将透视校正后的图像与透明背景模板图像合并...

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).

For easier understanding - here is an example created in 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. 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

2) Template with transparent background:

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:

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.

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).

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

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值