XenoTheStrange

[Python, ImageMagick] Stick together MapShot generated folders of images into a single image

Dec 15th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. #/usr/bin/python3
  2.  
  3. import os
  4.  
  5. # Path to directory with tile images and the filler image
  6. # CHANGE THIS TO YOUR FOLDER
  7. dir_path = "./s1zoom_0"
  8. if dir_path[-1] == "/":
  9.     dir_path = dir_path[:-1]
  10. filler_image = "bl.png"
  11.  
  12. # Get the tile files
  13. tile_files = [f for f in os.listdir(dir_path) if f.endswith(".jpg") and f.startswith("tile_")]
  14.  
  15. # Parse the tile files into a dictionary by coordinates
  16. tile_map = {}
  17. for file_name in tile_files:
  18.     # Extract the coordinates from the file name
  19.     parts = file_name.replace(".jpg", "").split("_")
  20.     x, y = int(parts[1]), int(parts[2])
  21.     tile_map[(x, y)] = file_name
  22.  
  23. # Find bounds of the grid
  24. min_x = min(x for x, y in tile_map.keys())
  25. max_x = max(x for x, y in tile_map.keys())
  26. min_y = min(y for x, y in tile_map.keys())
  27. max_y = max(y for x, y in tile_map.keys())
  28.  
  29. x_tiles = abs(min_x - max_x)+1
  30. y_tiles = abs(min_y - max_y)+1
  31. tiles_arg = f"-tile {x_tiles}x{y_tiles}"
  32.  
  33. # Print debug information
  34. print(f"Grid bounds: min_x={min_x}, max_x={max_x}, min_y={min_y}, max_y={max_y}")
  35.  
  36. # Generate the command to run montage
  37. command = "montage"
  38.  
  39. # Loop through rows from top (max_y) to bottom (min_y)
  40. for y in range(min_y, max_y + 1):  # Correct order: bottom to top
  41.     for x in range(min_x, max_x + 1):
  42.         # Add the tile if it exists, otherwise add the filler image
  43.         image_file = tile_map.get((x, y), filler_image)
  44.         image_path = os.path.join(dir_path, image_file) if image_file != filler_image else image_file
  45.         command += f' "{image_path}"'
  46.         # Debug output: show where each tile is being placed
  47.         print(f"Placing tile at ({x},{y}) -> {image_path}")
  48.  
  49. # Add geometry and output file name
  50. command += f' -geometry +0+0 {tiles_arg} {dir_path.split("/")[-1]}.png'
  51.  
  52. # Print the montage command
  53. print(command)
  54. os.system(command)
  55.  
Advertisement
Add Comment
Please, Sign In to add comment