用python棒合成两个图像 [英] Compositing two images with python wand
问题描述
我需要使用python wand(用于python的image-magick绑定)来创建合成图像,但除了简单地将前景图像粘贴到背景图像之外,我在确定如何做任何事情时遇到了一些麻烦。我想要的是,鉴于我有两个图像:
I need to use python wand (image-magick bindings for python) to create a composite image, but I'm having some trouble figuring out how to do anything other than simply copy pasting the foreground image into the background image. What I want is, given I have two images like:
和
两个jpegs,我想删除猫的白色背景然后粘贴在房间里。其他python图像模块的答案,比如PIL,也很好,我只需要一些东西来自动化合成过程。在此先感谢。
both jpegs, I want to remove the white background of the cat and then paste it on the room. Answers for other python image modules, like PIL, are also fine, I just need something to automatize the composition process. Thanks in advance.
推荐答案
您可以使用 Image.composite()
方法:
You can achieve this using Image.composite()
method:
import urllib2
from wand.image import Image
from wand.display import display
fg_url = 'http://i.stack.imgur.com/Mz9y0.jpg'
bg_url = 'http://i.stack.imgur.com/TAcBA.jpg'
bg = urllib2.urlopen(bg_url)
with Image(file=bg) as bg_img:
fg = urllib2.urlopen(fg_url)
with Image(file=fg) as fg_img:
bg_img.composite(fg_img, left=100, top=100)
fg.close()
display(bg_img)
bg.close()
这篇关于用python棒合成两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!