精灵轮廓和s

2024-04-25 17:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个python脚本,可以从透明的背景spritesheet中剪切出sprites。我想把精灵切成正方形或长方形。到目前为止,我的想法是:

<强>1。从工作表中获取所有像素数据。

<强>2。搜索不透明像素。

3岁。当找到这样一个像素时,开始在它周围寻找其他非透明像素,这样您就可以正确地获得图像。

4岁。在元组中附加找到的像素,然后将其推入图像并保存。

精灵可能不均匀地放置在床单上

如果精灵是不均匀的,我会检查它的尺寸,然后添加一点透明的背景到它的两侧,所以它成为一个正方形或矩形。例如,你有一个图像是53x47我会添加19和25像素的透明背景,它的两边,使它成为一个矩形。你知道吗

我正在使用PIL,如果有更好的方法,请随意分享。如果你有更好的方法,也可以分享。你知道吗

这是我的代码到目前为止,我不明白如何添加透明的背景,以及如何做正确的大纲,而且我似乎不能得到neighbourPixels函数的权利,它使一个闭环。你知道吗

from PIL import Image
from sys import argv, exit

# Functions   

def neighbourPixels(pixels, row, column):
    pixel = pixels[row, column]
    data = tuple()
    holder = tuple()

    while pixel != 0:
        if pixels[row+1, column][3] != 0:
            holder = pixels[row+1, column]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 1"
            break
        elif pixels[row, column+1][3] != 0:
            holder = pixels[row, column+1]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 2"
        elif pixels[row+1, column+1][3] != 0:
            holder = pixels[row+1, column+1]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 3"
        elif pixels[row-1, column][3] != 0:
            holder = pixels[row-1, column]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 4"
        elif pixels[row, column-1][3] != 0:
            holder = pixels[row, column-1]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 5"
        elif pixels[row-1, column-1][3] != 0:
            holder = pixels[row-1, column-1]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 6"
        elif pixels[row+1, column-1][3] != 0:
            holder = pixels[row+1, column-1]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 7"
        elif pixels[row-1, column+1][3] != 0:
            holder = pixels[row-1, column+1]
            data += (holder, )
            pixel = pixels[row+1, column]
            print "In box 8"
        else:
            print "Sprite has been outlined."
            pixel[3] = 0

    sprite = Image.new("RGBA", (len(data), len(data)))
    sprite.putdata(data)
    return sprite


# Main
if len(argv) < 4:
    raise ValueError("Not enough arguments")

# Inicialization & Declatation
framesToCut = int(float(argv[2]))
frameSize = int(float(argv[3]))
frameToSave = Image.new("RGBA", (frameSize, frameSize))
frameCounter = 0
pixelTuple = tuple()
currentTuple = tuple()

# Preparing data
sheet = Image.open(argv[1])
width, heigth = sheet.size
pixels = sheet.load()

# Manipulation
for row in xrange(width):
    for column in xrange(heigth):
        # Searching for a pixel whos transparency is != 0
        currentPixel = pixels[row, column]
        if currentPixel[3] != 0:
            print "Pixel found!"
            print "Finding the sprite!"
            sprite = findPixels(pixels, row, column)
            if frameCounter <= framesToCut:
                # left, upper, right, lower
                frameCounter += 1
                sprite.save(str(frameCounter), "PNG")
                print "Sprite saved!"
            else:
                exit("Sprites were made O.o - Check 'em!")

Tags: inimageboxdatacolumn像素row背景
1条回答
网友
1楼 · 发布于 2024-04-25 17:24:05

您可能需要使用scikit image的行进方格[1]。你要找的算法已经实现了。你知道吗

[1]http://scikit-image.org/docs/dev/auto_examples/plot_contours.html

编辑:一旦你有轮廓使用灰度图像*r\u灰*,只需打印彩色图像和轮廓在彼此的顶部。以上示例中给出的链接是指:

# Find contours at a constant value of 0.8
contours = measure.find_contours(r_grey, 0.8)

# Display the image and plot all contours found
plt.imshow(r, interpolation='nearest')

for n, contour in enumerate(contours):
    plt.plot(contour[:, 1], contour[:, 0], linewidth=2)

原始代码告诉您使用r(图像)并绘制它。我们将继续这样做。记住:灰度图像和彩色图像具有相同的坐标。你知道吗

相关问题 更多 >

    热门问题