代码之家  ›  专栏  ›  技术社区  ›  betamax

pythonmagickwand shepards畸变(ctypes lp_c_双问题)

  •  1
  • betamax  · 技术社区  · 15 年前

    我想用 PythonMagickWand 使用A Shepards distortion 在图像上。你也可以看到 the source of distort.c 它由ImageMagick使用。

    pythonmagickwand默认不支持shepards失真。为了解决这个问题,我添加了:

    ShepardsDistortion = DistortImageMethod(15)

    去pythonmagicwand的544号线( See here for my modified PythonMagicWand )15指针指向 distort.h (第51行)的magicwand源,其中shepardsdistortion是列表中的第15项。这适用于所有其他支持的变形方法。

    现在,我可能做了一些错误的事情,假设现有的由pythonmagickwand支持的失真方法使用了与shepards相同的类型的参数。他们可能不会,但我不知道我怎么知道。我知道 distort.c 正在做这项工作,但我无法确定它所采用的论点是相同的还是不同的。

    我有以下代码(代码片段):

    from PythonMagickWand import *
    from ctypes import *
    
    arrayType = c_double * 8  
    pointsNew = arrayType()
    pointsNew[0] = c_double(eyeLeft[0])
    pointsNew[1] = c_double(eyeLeft[1])
    pointsNew[2] = c_double(eyeLeftDest[0])
    pointsNew[3] = c_double(eyeLeftDest[1])
    pointsNew[4] = c_double(eyeRight[0])
    pointsNew[5] = c_double(eyeRight[1])
    pointsNew[6] = c_double(eyeRightDest[0])
    pointsNew[7] = c_double(eyeRightDest[1])
    
    MagickWandGenesis()
    wand = NewMagickWand()
    MagickReadImage(wand,path_to_image+'image_mod.jpg')
    MagickDistortImage(wand,ShepardsDistortion, 8, pointsNew, False)
    MagickWriteImage(wand,path_to_image+'image_mod22.jpg')
    

    我得到以下错误:

    MagickDistortImage(wand,ShepardsDistortion, 8, pointsNew, False) ctypes.ArgumentError: argument 4: <type 'exceptions.TypeError'>: expected LP_c_double instance instead of list

    我知道pointsnew提供参数的方式是错误的。但我不知道什么是正确的格式。这是一个在终端中运行时起作用的扭曲命令示例:

    convert image.jpg -virtual-pixel Black -distort Shepards 121.523809524,317.79638009 141,275 346.158730159,312.628959276 319,275 239.365079365,421.14479638 232,376 158.349206349,483.153846154 165,455 313.015873016,483.153846154 300,455 0,0 0,0 0,571.0 0,571.0 464.0,571.0 464.0,571.0 0,571.0 0,571.0 image_out.jpg

    所以我想问题是:我该如何创建一个可以被pythonmagickwand接受的c_双精度列表? 我把谢泼德扭曲的手法加在蟒蛇术上是完全错误的吗?

    我基本上需要重新创建终端命令。我已经通过使用子进程从Python运行命令来实现它,但这不是我想要的方式。

    3 回复  |  直到 15 年前
        1
  •  2
  •   betamax    15 年前

    NeedMoreBeer Reddit.com :

    from PythonMagickWand import *
    from ctypes import *
    
    arrayType = c_double * 8  
    pointsNew = arrayType()
    pointsNew[0] = c_double(121.523809524)
    pointsNew[1] = c_double(317.79638009)
    pointsNew[2] = c_double(141)
    pointsNew[3] = c_double(275) 
    pointsNew[4] = c_double(346.158730159)
    pointsNew[5] = c_double(312.628959276)
    pointsNew[6] = c_double(319)
    pointsNew[7] = c_double(275)
    
    ShepardsDistortion = DistortImageMethod(14)
    
    MagickWandGenesis()
    wand = NewMagickWand()
    MagickReadImage(wand,'/home/user/image.png')
    MagickDistortImage(wand,ShepardsDistortion, 8, pointsNew, False)
    MagickWriteImage(wand,'/home/user/image_mod22.jpg')
    

    更具体地说,是以下几行:

    ShepardsDistortion = DistortImageMethod(14)
    

    为我修的!再次感谢 必得啤酒 Reddit .

        2
  •  0
  •   Mark Rushakoff    15 年前

    如错误消息所示,函数需要一个指针( LP_c_double ,但您所拥有的只是一个数组。

    为了传递数组,需要显式地将数组强制转换为指针 作为 指向外部函数的指针:

    >>> arr = (c_double * 8)()
    >>> arr # just an array...
    <__main__.c_double_Array_8 object at 0x1004ad050>
    >>> arr[0] = 5
    # ...
    >>> cast(arr, POINTER(c_double)) # now it's the right type
    <__main__.LP_c_double object at 0x1004ad0e0>
    >>> cast(arr, POINTER(c_double))[0]
    5.0
    
        3
  •  0
  •   Stuart Axon    15 年前

    这是OT,但:

    我真的很喜欢python magick魔杖,但是当我在600多张图片上使用它时,在某个地方出现了内存泄漏,它占用了(32位)机器中的所有内存。我倾向于思考图像魔术师本身,但我可能是错的。

    最后我发现:

    graphicsmagick是早期版本的imagemagick的一个端口;但是由于imagemagick改变了他们的API很多,pythonmagickwant无法与graphicsmagick一起工作。

    graphicsmagick,似乎也比imagemagick(使用更好的多任务)更快。

    为了让我的脚本快速运行,几天之后我更改了它,这样脚本就加载了gimp,而不是使用imagemagick,然后使用python scriptfu运行我的脚本。 一切似乎都正常。

    如果它对你有用,那么就使用它;同样,pythonmagickwand人也很有用+绑定很好。