Skip to content

Instantly share code, notes, and snippets.

@lalamax3d
Last active January 19, 2024 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalamax3d/80a8f96226f1fe1b8bacf6b747501f05 to your computer and use it in GitHub Desktop.
Save lalamax3d/80a8f96226f1fe1b8bacf6b747501f05 to your computer and use it in GitHub Desktop.
split_cube_pano_room.py
from PIL import Image
import os
'''
intent is to save main tiles and then stitch them horizontally.
like here >> https://github.com/doge/cubemap_converter (hrow)
'''
def split_image(image_path):
# Open the image
with Image.open(image_path) as img:
# Get the size of the image
width, height = img.size
# Calculate the size of each grid cell
cell_width = width // 3
cell_height = height // 3
# Loop through each cell and save the resulting image
cubeTileIds = [1,3,4,5,6,7]
cubeTilesDict = {1: "py", 3: "nx", 4: "pz", 5: "px", 6: "nz", 7: "ny"}
newImage = Image.new('RGB', (cell_width*6, cell_height))
newArrangement = ['px','nx','py','ny','pz','nz']
iter = 0
for i in range(3):
for j in range(3):
# Calculate the coordinates of the cell
left = j * cell_width
top = i * cell_height
right = (j + 1) * cell_width
bottom = (i + 1) * cell_height
# Crop the cell from the original image
cell = img.crop((left, top, right, bottom))
# Save the required cells on disk with correct names
if iter in cubeTileIds:
cell.save(os.path.join("textures", "cubeTile_" + cubeTilesDict[iter] + ".png"))
newImage.paste(cell, (cell_width*newArrangement.index(cubeTilesDict[iter]), 0))
iter += 1
newImage.save(os.path.join("textures", "cubeTile.png"))
print("Image split into 3x3 grid and saved as 9 separate images.")
if __name__ == '__main__':
# get image from kargs
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("image", help="path to image")
args = parser.parse_args()
split_image(args.image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment