I need to separate or translate or replace pixels in an image with python so as to all the shapes to share the same distance between each other and the limits of the canvas.
background is white, shapes are black.
IMPORTANT: Images are dynamic, all images have bars at different positions, that means i need to detect where a bar starts and where a bar ends to draw the final image!
This is an example INPUT image
This is an example OUTPUT image
I did this by hand, i don't know if all bars are separated by the same distance, it's just an example.
The resulting canvas size can change too but ideally i would like all canvas to have the same width and height after the bars have been respositioned.
All pixels in the images are BLACK or WHITE, that should make it much easier.
What i've tried so far kind of detects the shapes but some shapes are not detected correctly when they are too close of each other:
saut = 1
start_ab = 0
end_ab = 0
start_or = im2.size[1] - 1
end_or = 0
size = 0shapes = []for y in range(0, im2.size[0], saut): # slice acrossfor x in range(0, im2.size[1], 1): # slice downpix = im2.getpixel((y, x))if pix != 255:start_or = min(start_or, x)end_or = max(end_or, x)inshape = Trueif foundshape == False and inshape == True:foundshape = Truestart_ab = yif foundshape == True and inshape == False:foundshape = Falseend_ab = ysize = max(size, end_ab)shapes.append((start_ab, end_ab, start_or, end_or))start_or = im2.size[1] - 1end_or = 0inshape = Falseprint shapes
# example shapes output NOT FROM THE EXAMPLE IMAGES PROVIDED but from other shapes [(54, 171, 72, 233), (216, 324, 108, 251), (342, 486, 0, 215), (513, 765, 18, 260), (792, 918, 90, 242)]
And still have to draw the new image, i don't know how to do that, also some images have shapes with "holes" in them, so that makes the "redraw" a little bit more complicated.