2014-03-05 37 views
1

以下是用於從圖像中移除背景的Python代碼。我按照https://pypi.python.org/pypi/pgmagick/中給出的步驟在mac osx mavericks中安裝pgmagick。Boost.Python.ArgumentError:Image .__ init __中的Python參數類型(圖像,文件)與C++簽名不匹配:

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

問題是什麼?我如何解決它?

+0

我與試驗OSX Mavericks上的相同代碼,我沒有得到那個錯誤。 你是如何安裝相關庫的? – Pitarou

回答

0

由於C++類型的擴展名稱,追溯可能有點讓人望而生畏。但是,這表示pgmagick.Image正試圖用file對象構建,但pgmagick未提供接受file對象的Image上的__init__方法。

在這種情況下,remove_background()方法的filename參數需要str而不是file。要解決此問題,更改:

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

到:

remove_background("example.jpg") 
1

我有pgmagick同樣的問題,以及與此代碼解析:

image = pg.Image(str(img)) 
+0

感謝它爲我工作。 – RajSharma