【问题标题】:Output of connected-components different between ImageMagick and Wand?ImageMagick和Wand之间连接组件的输出不同?
【发布时间】:2020-09-14 07:24:13
【问题描述】:

我正在尝试使用 Windows10 下的连接组件功能获取颜色坐标

  • Python 3.8.5
  • ImageMagick 7.0.10-29 Q8 x64 2020-09-05

将 ImageMagick 与以下命令一起使用,我可以正确获取坐标。

>convert input.png -define connected-components:verbose=true -define connected-components:area-threshold=100 -connected-components 8
-auto-level out:null
Objects (id: bounding-box centroid area mean-color):
0: 284x172+0+0 133.5,60.0 26161 srgb(0,0,0)
2: 259x59+14+84 143.0,113.0 15281 srgb(255,255,255)
3: 259x17+14+144 143.0,152.0 4403 srgb(255,255,255)
1: 143x21+130+60 201.0,70.0 3003 srgb(255,255,255)

当我将连接组件与 Python3/Wand 一起使用时,我得到不同的输出。

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from wand.image import Image
>>> with Image(filename='input.png') as img:
...    objects = img.connected_components()
...
>>> for cc_obj in objects:
...    print("{0._id}: {0.size} {0.offset}".format(cc_obj))
...
0: (284, 172) (0, 0)
0: (0, 0) (0, 0)
14: (84, 98784247810) (0, 0)
0: (0, 0) (0, 0)
>>>

为什么输出与 python/Wand 不同?如何解决?提前致谢。

下面是输入图片。

下面是输入图片。

【问题讨论】:

  • 看起来像 Wand 中的一个错误。应该在 0.6.3 版本中解决
  • @emcconville 再次感谢您的回答。在这种情况下,您或其他人是否知道另一种获取坐标的 Pythonic 方式/库?问候
  • 使用Python子进程直接调用ImageMagick。
  • @fmw42 太好了,有一种替代方法可以直接调用出色的 ImageMagick 软件。我会调查一下。非常感谢您的帮助。
  • @Ger Cas 请参阅下面的子流程示例。

标签: python-3.x imagemagick wand connected-components


【解决方案1】:

下面是一个使用 Python 子进程调用 ImageMagick 命令行(修剪图像多余背景)的示例:

import subprocess    
cmd = 'convert 0.png -fuzz 30% -trim +repage 0_trim.png'
subprocess.check_output(cmd, shell=True, universal_newlines=True)

另一种选择是:

import subprocess
cmd = 'convert 0.png -fuzz 30% -trim +repage 0_trim.png'
subprocess.call(cmd, shell=True)

【讨论】:

  • 感谢您的帮助。我已经尝试了您的示例,并且与直接使用 ImageMagick 完全一样。只有一个问题与此线程无关。如果convert 命令是多行的,那么当我们使用反斜杠\` 分隔每一行时它会起作用,但是当我在Windows 中运行convert 时它不起作用。我的意思是,在 DOS 命令行中运行转换。在 DOS 终端中运行 multine convert 命令的正确方法是什么?谢谢
  • Windows 语法与 Unix 不同。 Windows 中的新行和转义符是 ^ 而不是 \。同样在 Windows 中使用不带 \ 转义的括号。如果使用 Windows .bat,请将 % 加倍为 %%。见archive.imagemagick.org/Usage/windows
  • 太棒了。非常感谢您收到的所有帮助!! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多