Trying to use pil for creating grid-like layout from images. But that code only draws first column. Can anyone help me?
def draw(self):
image=Image.new("RGB",((IMAGE_SIZE[0]+40)*5+40,(IMAGE_SIZE[1]+20)*CHILD_COUNT+20),(255,255,255))
paste_x=(-1)*IMAGE_SIZE[0]
paste_y=(-1)*IMAGE_SIZE[1]
i=0
for a range(5):
paste_x=paste_x+IMAGE_SIZE[0]+40
j=0
for b in range(4):
paste_y=paste_y+IMAGE_SIZE[1]+20
image.paste(Image.new("RGB",IMAGE_SIZE,(0,0,0)),(paste_x,paste_y))
j=j+1
i=i+1
out=NamedTemporaryFile(delete=False)
path=out.name
image.save(out, "PNG")
out.close()
print path
Use itertools.product to iterate over the rows and columns:
Also, try not to use too many hard-coded numbers. If you put the numbers in variables then your code is easier to change and it cuts down on potential errors.
PS. I think the error in the code you posted is that you never reset
paste_y.After finishing the first column, the value of
paste_yjust keeps on growing,so you start pasting small images beyond the lower edge of the
image.So you could fix the problem by moving
paste_y=-IMAGE_SIZE[1]to just afterj=0, but I still prefer doing it the way I show above.