from PIL import Image, ImageFilter, ImageDraw, ImageFont

def planche( image , image2 , nx , ny , px , py , txt = None):
    width, height = image.size
    new_width = nx * (width + 10) - 10
    new_height = ny * (height + 10) - 10
    if txt != None:
        new_height += 100
    
    new_image = Image.new(image.mode, (new_width, new_height))
    
    ppx , ppy = 0,0
    
    for y in range(0 , ny*height , height+10):
        for x in range(0 , nx*width , width+10):
            if x//width+1 == px and y//height+1 == py:
                new_image.paste( image2 , (x , y , x+width , y+height) )
            else:
                new_image.paste( image , (x , y , x+width , y+height) )
    
    
    if txt != None:
        tmp = ImageDraw.Draw(new_image)
        myFont = ImageFont.truetype('Roboto-ThinItalic.ttf', 40) 
        tmp.text( (0,new_height-50) , txt , fill = (0,0,0) , font = myFont )
        
    new_image.save('result.png' , 'PNG')
    

image = Image.open("NewTux.png")
image2 = Image.open("NewTux2.png")

planche(image,image2,5,5,3,2,'https://mathweb.fr')
