Skip to content Skip to sidebar Skip to footer

Divide Image Into Rectangles Information In Python

I have a series of images that are nothing more than a series of colored rectangles in a black background Example:. How could I (using PIL in Python 3.7) make a program that would

Solution 1:

You don't need to write any code at all, you can do that with ImageMagick which is installed on most Linux distros and is available for macOS and Windows.

Just in the Terminal (Command Prompt on Windows), you can run:

magick convert image.png                            \
   -define connected-components:verbose=true        \
   -define connected-components:area-threshold=100  \
   -connected-components 4 -auto-level output.png

Sample Output

Objects (id: bounding-box centroid area mean-color):
  0: 1200x714+0+0 651.2,369.3 703177 srgb(0,0,0)
  164: 1200x86+0+714 599.5,756.5 103200 srgb(255,21,0)
  2: 363x155+80+60 261.0,137.0 56265 srgb(255,255,255)
  26: 127x323+60+302 122.6,463.2 39668 srgb(255,255,255)
  54: 308x109+352+373 505.5,427.0 33572 srgb(255,255,255)
  1: 102x159+641+47 691.5,126.0 16218 srgb(255,255,255)
  53: 79x100+977+371 1016.0,420.5 7900 srgb(0,17,255)

So, looking at the line starting 0: there is a rectangle measuring 1200x714 starting at 0,0 (top-left corner) with colour black, i.e. srgb(0,0,0).

Looking at the next line, there is a rectangle measuring 1200x86 starting 714 pixels down from the top-left corner with colour red, i.e. srgb(255,21,0).

And so on.

The last line is a rectangle 79x100 positioned at [977,31] with colour blue, i.e. srgb(0,17,255).


Solution 2:

I Don't know how far you are OK with OpenCV .

If your ready to use openCV , you can use the findContours to get the desired things .

Below is the code:

    import cv2
    readImage= cv2.imread(r"<ImagePath>\oKvDi.png")
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    #Code to Find edges of Square using Canny edge detection method and finding Contours and drawing in Back Line

    edges = cv2.Canny(img_gray, 0, 100)
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    #Just for your reference to show all rectangles are found
    cv2.drawContours(readImage, contours, -1, (0, 0, 0), 5)
    cv2.imwrite(r"<ImageSavePath>/a.png",readImage)

Hope this solves your problem


Post a Comment for "Divide Image Into Rectangles Information In Python"