Compositing two images with python wand

一个人想着一个人 提交于 2019-12-10 04:10:15

问题


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:

and

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.


回答1:


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



回答2:


For those that stumble across this in the future, what you probably want to do is change the 'white' color in the cat image to transparent before doing the composition. This should be achievable using the 'transparent_color()' method of the Image. Something like 'fg_img.transparent_color(wand.color.Color('#FFF')), probably also with a fuzz parameter.

See: http://www.imagemagick.org/Usage/compose/ http://docs.wand-py.org/en/latest/wand/image.html



来源:https://stackoverflow.com/questions/15144483/compositing-two-images-with-python-wand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!