独立/复位/翻译形状的图像与枕头蟒蛇蟒蛇、枕头、形状、图像

2023-09-11 06:52:35 作者:我长不出你爱的模样

我需要单独或翻译或者与蟒图像替换像素,以便所有的形状可以彼此交流和画布的限制之间的距离相同。

背景为白色,造型是黑色的。

重要提示:图片是动态的,所有的图像都棒在不同的位置,这意味着我需要检测,其中一间酒吧开始,并在那里一杆结束绘制最终图像

这是一个例子输入图像

小男孩正睡觉呢,忽然被巨蟒缠住身子 快起床,该给我按摩了

这是一个例子输出图像

我的手这样做,我不知道是不是所有的酒吧是由相同的距离分离,这只是一个例子。

由此产生的画布大小可以改变太多,但我非常希望所有的画布具有相同的宽度和高度的酒吧已经respositioned后。

在图像中所有的像素是黑人还是白人,也应该使它更容易。

我已经试过到目前为止样的检测的形状,而没有检测到一些形状正确,当他们太近彼此的:

 兼用= 1
start_ab = 0
end_ab = 0
start_or = im2.size [1]  -  1
end_or = 0
大小= 0

形状= []

横跨#切片:y的在范围(0,im2.size [0],兼用)
    x的范围(0,im2.size [1],1):#片向下
        PIX = im2.getpixel((Y,X))
        如果PIX = 255!
            start_or =分钟(start_or,x)的
            end_or = MAX(end_or,X)
            inshape = TRUE

    如果foundshape ==虚假和inshape ==真:
        foundshape = TRUE
        start_ab = Y

    如果foundshape == True和inshape ==错误:
        foundshape =假
        end_ab = Y
        大小= MAX(大小,end_ab)
        shapes.append((start_ab,end_ab,start_or,end_or))
        start_or = im2.size [1]  -  1
        end_or = 0
    inshape =假

打印形状
#例如形状输出不从提供的示例图像,但其他形状[(54,171,72,233),(216,324,108,251),(342,486,0,215),(513,765, 18,260),(792,918,90,242)]
 

和仍然要绘制新的形象,我不知道该怎么做,还有些图像有形状与他们洞,这样就使得重绘有点复杂。

解决方案   

和仍然要绘制新的形象,我不知道该怎么做,还有些图像有形状与他们洞,这样就使得重绘有点复杂。

一旦你建立了一个矩形的界限,你可以使用Image.crop()函数来从图像(包括孔)和Image.paste(),以将其粘贴在合适的位置提取的矩形。本教程包含了一个例子:

http://pillow.readthedocs.org/en/latest/handbook/tutorial.html#cutting-pasting-and-merging-images

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 = 0

shapes = []

for y in range(0, im2.size[0], saut):  # slice across
    for x in range(0, im2.size[1], 1):  # slice down
        pix = im2.getpixel((y, x))
        if pix != 255:
            start_or = min(start_or, x)
            end_or = max(end_or, x)
            inshape = True

    if foundshape == False and inshape == True:
        foundshape = True
        start_ab = y

    if foundshape == True and inshape == False:
        foundshape = False
        end_ab = y
        size = max(size, end_ab)
        shapes.append((start_ab, end_ab, start_or, end_or))
        start_or = im2.size[1] - 1
        end_or = 0
    inshape = False

print 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.

解决方案

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.

Once you've established the bounds of a rectangle, you could use the Image.crop() function to extract a rectangle from the image (including those holes) and Image.paste() to paste them in the right position. The tutorial contains an example:

http://pillow.readthedocs.org/en/latest/handbook/tutorial.html#cutting-pasting-and-merging-images