Advertisement
cubbypastes

Zoomify code

Jan 11th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import os
  2. import requests
  3. from PIL import Image
  4. from concurrent.futures import ThreadPoolExecutor
  5.  
  6. # Base URL and output setup
  7. BASE_URL = "http://cartesmich.free.fr/images/France_LD/"
  8. OUTPUT_DIR = "tiles"
  9. MERGED_IMAGE = "merged_image.jpg"
  10.  
  11. # Ensure output directory exists
  12. os.makedirs(OUTPUT_DIR, exist_ok=True)
  13.  
  14. # Function to download a tile
  15. def download_tile(group, x, y):
  16. url = f"{BASE_URL}TileGroup{group}/6-{x}-{y}.jpg"
  17. save_path = os.path.join(OUTPUT_DIR, f"TileGroup{group}_6-{x}-{y}.jpg")
  18. try:
  19. response = requests.get(url, timeout=10)
  20. if response.status_code == 200:
  21. with open(save_path, "wb") as f:
  22. f.write(response.content)
  23. print(f"Downloaded: {url}")
  24. return (group, x, y, save_path)
  25. else:
  26. print(f"Tile not found: {url}")
  27. except Exception as e:
  28. print(f"Error downloading {url}: {e}")
  29. return None
  30.  
  31. # Function to download all tiles (no detection, brute force)
  32. def download_all_tiles(groups, x_range, y_range):
  33. tiles = []
  34. print("Starting brute force tile download...")
  35. with ThreadPoolExecutor(max_workers=10) as executor:
  36. futures = []
  37. for group in groups:
  38. for x in x_range:
  39. for y in y_range:
  40. futures.append(executor.submit(download_tile, group, x, y))
  41. for future in futures:
  42. result = future.result()
  43. if result:
  44. tiles.append(result)
  45. return tiles
  46.  
  47. # Function to stitch tiles together
  48. def stitch_tiles(tiles, tile_size):
  49. if not tiles:
  50. print("No tiles to stitch.")
  51. return None
  52.  
  53. # Determine the range of x and y coordinates
  54. all_coords = [(x, y) for _, x, y, _ in tiles]
  55. min_x = min(x for x, y in all_coords)
  56. max_x = max(x for x, y in all_coords)
  57. min_y = min(y for x, y in all_coords)
  58. max_y = max(y for x, y in all_coords)
  59.  
  60. # Create a blank canvas for the final image
  61. width = (max_x - min_x + 1) * tile_size
  62. height = (max_y - min_y + 1) * tile_size
  63. merged_image = Image.new("RGB", (width, height))
  64.  
  65. # Paste tiles onto the canvas
  66. for group, x, y, tile_path in tiles:
  67. tile_image = Image.open(tile_path)
  68. merged_image.paste(
  69. tile_image, ((x - min_x) * tile_size, (y - min_y) * tile_size)
  70. )
  71.  
  72. return merged_image
  73.  
  74. # Main script execution
  75. tile_size = 256 # Assume each tile is 256x256
  76. groups = range(0, 16) # TileGroup0 to TileGroup15
  77. x_range = range(0, 50) # x-coordinates: 0–49
  78. y_range = range(0, 50) # y-coordinates: 0–49
  79.  
  80. tiles = download_all_tiles(groups, x_range, y_range)
  81.  
  82. # Stitch the tiles into a single image
  83. if tiles:
  84. merged_image = stitch_tiles(tiles, tile_size)
  85. if merged_image:
  86. merged_image.save(MERGED_IMAGE)
  87. print(f"Merged image saved as {MERGED_IMAGE}")
  88. else:
  89. print("No tiles were downloaded!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement