4

Question

What is a good way to use flood fill with the Graphicsmagick command line or its pgmagick wrapper for python?

Background

So far this is what I have, but its saying that the signatures do not match:

Code:
from pgmagick import Image, ColorRGB

img = Image('C:\\test.png')
cRGB = ColorRGB(256.0, 256.0, 256.0)
geo = Geometry(1,1)
img.floodFillColor(geo, cRGB, cRGB)
Error:
  File "C:/Dropbox/COC/automate/coc_automate/python/__init__.py", line 62, in take_main_screen_shot
    img.floodFillColor(geo, cRGB, cRGB)
Boost.Python.ArgumentError: Python argument types in
    Image.floodFillColor(Image, Geometry, ColorRGB, ColorRGB)
did not match C++ signature:
    floodFillColor(class Magick::Image {lvalue}, class Magick::Geometry, class Magick::Color, class Magick::Color)
    floodFillColor(class Magick::Image {lvalue}, class Magick::Geometry, class Magick::Color)

Extra

Also, if you know of a better way that I can manipulate graphics from a Python application or the windows command line, I'm all ears. I'm starting to feel like I may be using the wrong tool for this with the state of the documentation.

4

2 回答 2

1

正如你所说,文档不是很好。pgmagick 是一个用于 C++ 前端的精简 Python 包装器,因此您必须深入了解接口层以弄清楚如何使其工作。除非您需要额外的功能,否则 Pillow 可能是更好的选择。

我一直使用 pgmagick 和 ImageMagick,而不是 GraphicsMagick,作为后端。我不知道这是否有很大的不同。无论如何,以下是我需要进行的调整以使您的代码正常工作:

  • 使用颜色而不是 RGBColor

    from pgmagick import Color
    color = Color('white')
    
  • 实例化 Geometry 对象时再提供两个 0:

    from pgmagick import Geometry
    geo = Geometry(0, 0, 1, 1)
    
  • 与其将两个 Color 参数传递给 floodFillColor,不如先调用 fillColor,然后调用 floodFillColor,如下所示:

    img.fillColor(color)
    img.floodFillColor(geo, color)
    
于 2014-03-07T03:32:14.173 回答
1

我建议看一下Python Imaging Library (PIL)

例如在图像上画一个灰色的十字

import Image, ImageDraw

im = Image.open("lena.pgm")

draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw 

# write to stdout
im.save(sys.stdout, "PNG")
于 2013-12-11T12:43:42.117 回答