I am trying to make tiles for a game using a larger background image and cropping them, but after saving the first 17 images I start running into problems. The 18th image is blackened out at the bottom and the remaining images are all black. Any thoughts?
Here is my code. imagesize=(512, 512), tilesize=(32, 32)
def __init__(self, path, imagesize, tilesize):
self.tiles = dict()
self.backimage = Image.open(path)
self.backimage.resize(imagesize)
self.x = imagesize[0]/tilesize[0]
self.y = imagesize[1]/tilesize[1]
currx = 0
curry = 0
for i in range(self.x):
for j in range(self.y):
tmp = path + str(i) + "_" + str(j) + ".png"
self.tiles[(i, j)] = tmp
image = self.backimage.crop((currx, curry, currx+tilesize[0], curry+tilesize[1]))
image.save(tmp, "PNG")
curry += tilesize[1]
currx += tilesize[0]
You’re not resetting
curryat the end of thejloop. It just keeps increasing until it’s out of bounds.