Boost.Python.ArgumentError:Image .__ init __(Image,file)中的Python参数类型与C ++签名不匹配:

时间:2014-03-05 15:07:56

标签: python boost-python

以下是从图像中删除背景的python代码。我按照https://pypi.python.org/pypi/pgmagick/中给出的步骤在mac osx mavericks中安装pgmagick。

    import pgmagick as pg


    def trans_mask_sobel(img):
        """ Generate a transparency mask for a given image """

        image = pg.Image(img)

        # Find object
        image.negate()
        image.edge()
        image.blur(1)
        image.threshold(24)
        image.adaptiveThreshold(5, 5, 5)

        # Fill background
        image.fillColor('magenta')
        w, h = image.size().width(), image.size().height()
        image.floodFillColor('0x0', 'magenta')
        image.floodFillColor('0x0+%s+0' % (w-1), 'magenta')
        image.floodFillColor('0x0+0+%s' % (h-1), 'magenta')
        image.floodFillColor('0x0+%s+%s' % (w-1, h-1), 'magenta')

        image.transparent('magenta')
        return image

    def alpha_composite(image, mask):
        """ Composite two images together by overriding one opacity channel """

        compos = pg.Image(mask)
        compos.composite(
            image,
            image.size(),
            pg.CompositeOperator.CopyOpacityCompositeOp
        )
        return compos

    def remove_background(filename):
        """ Remove the background of the image in 'filename' """

        img = pg.Image(filename)
        transmask = trans_mask_sobel(img)
        img = alphacomposite(transmask, img)
        img.trim()
        img.write('out.png')

    img = open("example.jpg")
    remove_background(img)

在运行时我遇到以下错误

Traceback (most recent call last):
  File "imgrm.py", line 48, in <module>
    remove_background(img)
  File "imgrm.py", line 41, in remove_background
    img = pg.Image(filename)
Boost.Python.ArgumentError: Python argument types in
    Image.__init__(Image, file)
did not match C++ signature:
    __init__(_object*, Magick::Image)
    __init__(_object*, unsigned int, unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, MagickLib::StorageType, char const*)
    __init__(_object*, Magick::Blob, Magick::Geometry, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
    __init__(_object*, Magick::Blob, Magick::Geometry, unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
    __init__(_object*, Magick::Blob, Magick::Geometry, unsigned int)
    __init__(_object*, Magick::Blob, Magick::Geometry)
    __init__(_object*, Magick::Blob)
    __init__(_object*, Magick::Geometry, Magick::Color)
    __init__(_object*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
    __init__(_object*)

问题是什么?我该如何解决?

2 个答案:

答案 0 :(得分:1)

我对pgmagick有同样的问题,并使用此代码解决:

image = pg.Image(str(img))

答案 1 :(得分:0)

由于扩展了C ++类型的名称,回溯可能有点令人生畏。但是,它表示pgmagick.Image正在尝试使用file对象构建,但pgmagick未在__init__上提供接受{Image的{​​{1}}方法{1}}对象。

在这种情况下,file方法的remove_background()参数需要filename而不是str。要解决此问题,请更改:

file

为:

img = open("example.jpg")
remove_background(img)