Advertisement
cubbypastes

Colab image download attempt II

Jan 9th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. import os
  2. import requests
  3. from PIL import Image
  4.  
  5. # Base URL and directory setup
  6. BASE_URL = "http://cartesmich.free.fr/images/France_LD/"
  7. OUTPUT_DIR = "tiles"
  8. MERGED_IMAGE = "merged_image.jpg"
  9.  
  10. # Ensure the output directory exists
  11. os.makedirs(OUTPUT_DIR, exist_ok=True)
  12.  
  13. # Function to download a tile
  14. def download_tile(url, save_path):
  15. response = requests.get(url)
  16. if response.status_code == 200:
  17. with open(save_path, "wb") as f:
  18. f.write(response.content)
  19. return True
  20. return False
  21.  
  22. # Function to stitch the tiles together
  23. def stitch_tiles(tiles, tile_size):
  24. max_x = max(x for x, y in tiles.keys()) + 1
  25. max_y = max(y for x, y in tiles.keys()) + 1
  26.  
  27. # Create a blank canvas for the final image
  28. merged_image = Image.new("RGB", (max_x * tile_size, max_y * tile_size))
  29.  
  30. # Paste tiles onto the canvas
  31. for (x, y), tile_path in tiles.items():
  32. tile_image = Image.open(tile_path)
  33. merged_image.paste(tile_image, (x * tile_size, y * tile_size))
  34.  
  35. return merged_image
  36.  
  37. # Set parameters for downloading tiles
  38. tile_size = 256 # Assume each tile is 256x256
  39. x_range = range(36, 50) # Adjust based on your needs (x-coordinate range)
  40. y_range = range(24, 40) # Adjust based on your needs (y-coordinate range)
  41. tile_groups = range(0, 13) # Range of TileGroups (adjust based on feedback)
  42.  
  43. # Dictionary to store downloaded tile paths
  44. downloaded_tiles = {}
  45.  
  46. # Download tiles from all TileGroups
  47. for group in tile_groups:
  48. for x in x_range:
  49. for y in y_range:
  50. tile_url = f"{BASE_URL}TileGroup{group}/6-{x}-{y}.jpg"
  51. tile_path = os.path.join(OUTPUT_DIR, f"TileGroup{group}_6-{x}-{y}.jpg")
  52. if download_tile(tile_url, tile_path):
  53. downloaded_tiles[(x - min(x_range), y - min(y_range))] = tile_path
  54. print(f"Downloaded: {tile_url}")
  55. else:
  56. print(f"Tile not found: {tile_url}")
  57.  
  58. # Stitch the tiles into a single image
  59. if downloaded_tiles:
  60. merged_image = stitch_tiles(downloaded_tiles, tile_size)
  61. merged_image.save(MERGED_IMAGE)
  62. print(f"Merged image saved as {MERGED_IMAGE}")
  63. else:
  64. print("No tiles were downloaded!")
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement