from PIL import Image # Palette de couleurs pierre = (0, 0, 0, 255) brique = (255, 0, 0, 255) empty = (255, 255, 255, 255) piece = (203, 255, 0, 255) boite_piece = (255, 153, 0, 255) boite_champi = (204, 0, 255, 255) brique_piece = (101, 127, 0, 255) beton = (84, 84, 84, 255) tuyau_milieu = (0, 255, 102, 255) tuyau_bout = (50, 255, 0, 255) goomba = (127, 76, 0, 255) koopa_vert = (25, 127, 0, 255) # Load image filename = input("File name ?\n> ") img = Image.open(filename) print("Converting", filename, "to", "\"" + filename + ".c\",", "size =", img.size) code = "w_current_x = " + str(img.size[0]) + ";\nw_current_y = " + str(img.size[1]) + ";\nworld_t lvl[]=\n{" nombre_ennemis=0 ennemis = "\nennemi_t ennemies0[]={" # Balayage des pixels : passe pour dessiner les murs pixels = img.load() for x in range(0,img.size[0]): code += "\n " for i in range(0, img.size[1]): y=img.size[1]-i-1 if pixels[x,y] == pierre: hexa="0x" left=1 right=1 if x != 0: left = (pixels[x-1,y] == pierre) if x != img.size[0]-1: right = (pixels[x+1,y] == pierre) sx=0 if left and right: sx=1 elif left and right==0: sx=2 elif left==0 and right: sx=0 hexa+= str(sx) up=0 # vide par defaut down=1 if y != 0: up = (pixels[x,y-1] == pierre) if y != img.size[1]-1: down = (pixels[x,y+1] == pierre) sy = int(bool(up)) hexa += str(sy) code += "{EARTH," + hexa + "}, " elif pixels[x,y]==piece: code += "{COIN,0}, " elif pixels[x,y] == brique: code += "{BRICK,0}, " elif pixels[x,y]==brique_piece: code += "{BRICK,0x15}, " elif pixels[x,y]==boite_piece: code += "{GIFT,0x11}, " elif pixels[x,y]==boite_champi: code += "{GIFT,0x21}, " elif pixels[x,y]==empty: code += "{0,0}, " elif pixels[x,y]==beton: code += "{BLOC,0}, " elif pixels[x,y]==tuyau_bout: left=0 right=0 up=0 # vide par defaut down=0 tx=0 ty=0 if x: left = (pixels[x-1,y] == tuyau_bout) or (pixels[x-1,y] == tuyau_milieu) if x != img.size[0]-1: right = (pixels[x+1,y] == tuyau_bout) or (pixels[x+1,y] == tuyau_milieu) if y: up = (pixels[x,y-1] == tuyau_bout) or (pixels[x,y-1] == tuyau_milieu) if y != img.size[1]-1: down = (pixels[x,y+1] == tuyau_bout) or (pixels[x,y+1] == tuyau_milieu) if right and down: tx=0 if (pixels[x,y+1]==tuyau_milieu): ty=2 elif left and down: if (pixels[x,y+1]==tuyau_milieu): tx=1 ty=2 else: tx=2 ty=0 elif up and right: tx=0 if (pixels[x,y-1]==tuyau_milieu): ty=4 else: ty=1 elif up and left: if (pixels[x,y-1]==tuyau_milieu): tx=1 ty=4 else: tx=2 ty=1 hexa="0x"+str(tx)+str(ty) code += "{TUYAU,"+hexa+"}, " elif pixels[x,y]==tuyau_milieu: left=0 right=0 up=0 # vide par defaut down=0 tx=0 ty=0 if x: left = (pixels[x-1,y] == tuyau_bout) or (pixels[x-1,y] == tuyau_milieu) if x != img.size[0]-1: right = (pixels[x+1,y] == tuyau_bout) or (pixels[x+1,y] == tuyau_milieu) if y: up = (pixels[x,y-1] == tuyau_bout) or (pixels[x,y-1] == tuyau_milieu) if y != img.size[1]-1: down = (pixels[x,y+1] == tuyau_bout) or (pixels[x,y+1] == tuyau_milieu) if right and left==0: tx=0 ty=3 elif left and right==0: tx=1 ty=3 elif up==0 and down: tx=1 ty=0 elif up and down==0: tx=1 ty=1 hexa="0x"+str(tx)+str(ty) code += "{TUYAU,"+hexa+"}, " else: code += "{0,'?'}, " #code += str(pixels[x,y]) if pixels[x,y]==goomba: nombre_ennemis += 1 ennemis += "\n GOOMBA(" + str(8*x) + ", " + str(8*i) + ", -1)," elif pixels[x,y]==koopa_vert: ennemis += "\n KOOPA_V(" + str(8*x) + ", " + str(8*i) + ", -1)," code += "\n};\ninit_level(lvl);\n" ennemis += "\n};\nennemis_global_size=" + str(nombre_ennemis) + ";\ninit_ennemies(ennemies0);\n" f = open(filename+".c", 'w') f.write(code + ennemis) f.close() print("Converted succesfully !")