Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import requests
- from PIL import Image
- from concurrent.futures import ThreadPoolExecutor
- # Base URL and output setup
- BASE_URL = "http://cartesmich.free.fr/images/France_LD/"
- OUTPUT_DIR = "tiles"
- MERGED_IMAGE = "merged_image.jpg"
- # Ensure output directory exists
- os.makedirs(OUTPUT_DIR, exist_ok=True)
- # Function to download a tile
- def download_tile(group, x, y):
- url = f"{BASE_URL}TileGroup{group}/6-{x}-{y}.jpg"
- save_path = os.path.join(OUTPUT_DIR, f"TileGroup{group}_6-{x}-{y}.jpg")
- try:
- response = requests.get(url, timeout=10)
- if response.status_code == 200:
- with open(save_path, "wb") as f:
- f.write(response.content)
- print(f"Downloaded: {url}")
- return (group, x, y, save_path)
- else:
- print(f"Tile not found: {url}")
- except Exception as e:
- print(f"Error downloading {url}: {e}")
- return None
- # Function to download all tiles (no detection, brute force)
- def download_all_tiles(groups, x_range, y_range):
- tiles = []
- print("Starting brute force tile download...")
- with ThreadPoolExecutor(max_workers=10) as executor:
- futures = []
- for group in groups:
- for x in x_range:
- for y in y_range:
- futures.append(executor.submit(download_tile, group, x, y))
- for future in futures:
- result = future.result()
- if result:
- tiles.append(result)
- return tiles
- # Function to stitch tiles together
- def stitch_tiles(tiles, tile_size):
- if not tiles:
- print("No tiles to stitch.")
- return None
- # Determine the range of x and y coordinates
- all_coords = [(x, y) for _, x, y, _ in tiles]
- min_x = min(x for x, y in all_coords)
- max_x = max(x for x, y in all_coords)
- min_y = min(y for x, y in all_coords)
- max_y = max(y for x, y in all_coords)
- # Create a blank canvas for the final image
- width = (max_x - min_x + 1) * tile_size
- height = (max_y - min_y + 1) * tile_size
- merged_image = Image.new("RGB", (width, height))
- # Paste tiles onto the canvas
- for group, x, y, tile_path in tiles:
- tile_image = Image.open(tile_path)
- merged_image.paste(
- tile_image, ((x - min_x) * tile_size, (y - min_y) * tile_size)
- )
- return merged_image
- # Main script execution
- tile_size = 256 # Assume each tile is 256x256
- groups = range(0, 16) # TileGroup0 to TileGroup15
- x_range = range(0, 50) # x-coordinates: 0–49
- y_range = range(0, 50) # y-coordinates: 0–49
- tiles = download_all_tiles(groups, x_range, y_range)
- # Stitch the tiles into a single image
- if tiles:
- merged_image = stitch_tiles(tiles, tile_size)
- if merged_image:
- merged_image.save(MERGED_IMAGE)
- print(f"Merged image saved as {MERGED_IMAGE}")
- else:
- print("No tiles were downloaded!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement