我正在制作一个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!")
您可能需要使用scikit image的行进方格[1]。你要找的算法已经实现了。你知道吗
[1]http://scikit-image.org/docs/dev/auto_examples/plot_contours.html
编辑:一旦你有轮廓使用灰度图像*r\u灰*,只需打印彩色图像和轮廓在彼此的顶部。以上示例中给出的链接是指:
原始代码告诉您使用r(图像)并绘制它。我们将继续这样做。记住:灰度图像和彩色图像具有相同的坐标。你知道吗
相关问题 更多 >
编程相关推荐