0

I'm trying to get the coordinates of colors using connected-components feature under Windows10 with

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

Using ImageMagick with the following command I get the coordinates correctly.

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

When I use connected-components with Python3/Wand I get different output.

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

Why is the output different with python/Wand? How to fix it? Thanks in advance.

Below is the input image.

Below is the input image.

enter image description here

Ger Cas
  • 2,188
  • 2
  • 18
  • 45

1 Answers1

1

Here is an example of using Python subprocess to call ImageMagick command line (to trim an image's excess background):

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

An alternate is:

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

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks for your help. I've tried your example and works exactly as doing with ImageMagick directly. Only one question not to much related with this thread. If the `convert` command is multine it works when we separate each line using inverse slash `\\``, but that doesn't work when I run `convert` in Windows. I mean, running convert in DOS command line. How would be the correct way to run a multine `convert` command in DOS terminal? Thanks – Ger Cas Sep 15 '20 at 04:45
  • 1
    Windows syntax is different from Unix. New lines and escapes in Windows are ^ rather than \. Also in Windows use parentheses without the \ escapes. If using Windows .bat, double % to %%. See https://archive.imagemagick.org/Usage/windows/ – fmw42 Sep 15 '20 at 06:07
  • Great. Very appreciated all your help received!! :) – Ger Cas Sep 15 '20 at 06:21