[英]Merging perspective corrected image with transparent background template image using PILLOW [PIL, Python]
你真的不需要寫任何 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 對坐標,變換的每個角各一對,顯示源位置如何映射到 output 圖像中。
因此,封面的左上角 (0,0) 被映射到模板中空白區域的左上角 (96,89)。 封面的右上角 (width,0) 被映射到模板空白區域的右上角 (325,63)。 封面的右下角(寬度、高度)被映射到模板空白區域的右下角 (326,522)。 封面的左下角 (0,height) 被映射到模板空白區域 (96,491) 的左下角。
如果您使用的是舊版 v6 ImageMagick ,請將magick
替換為convert
。
請注意,如果您真的想在 Python 中執行此操作,這里有一個名為wand
的 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、魔杖、圖像處理、透視變換、扭曲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.