Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """Cartesmich attempt.ipynb
- Automatically generated by Colab.
- Original file is located at
- [irrelevant]
- """
- pip install requests pillow
- import os
- import requests
- from PIL import Image
- # Base URL for tiles
- BASE_URL = "http://cartesmich.free.fr/images/France_LD/TileGroup{}/{}-{}-{}.jpg"
- # Directory to save downloaded tiles
- SAVE_DIR = "tiles"
- os.makedirs(SAVE_DIR, exist_ok=True)
- # Specify the range of tiles to download
- TILEGROUPS = range(0, 5) # Adjust as needed
- ROWS = range(0, 10) # Adjust based on tile layout
- COLS = range(0, 10) # Adjust based on tile layout
- def download_tile(tilegroup, row, col):
- """Download a single tile and save it locally."""
- url = BASE_URL.format(tilegroup, row, col, 0)
- filename = os.path.join(SAVE_DIR, f"TileGroup{tilegroup}_{row}_{col}.jpg")
- try:
- response = requests.get(url, stream=True)
- if response.status_code == 200:
- with open(filename, "wb") as file:
- file.write(response.content)
- print(f"Downloaded: {filename}")
- else:
- print(f"Tile not found: {url}")
- except Exception as e:
- print(f"Error downloading {url}: {e}")
- def stitch_tiles():
- """Stitch downloaded tiles into a single image."""
- tile_width, tile_height = Image.open(os.path.join(SAVE_DIR, os.listdir(SAVE_DIR)[0])).size
- canvas_width = tile_width * len(COLS)
- canvas_height = tile_height * len(ROWS)
- canvas = Image.new("RGB", (canvas_width, canvas_height))
- for tilegroup in TILEGROUPS:
- for row in ROWS:
- for col in COLS:
- filename = os.path.join(SAVE_DIR, f"TileGroup{tilegroup}_{row}_{col}.jpg")
- if os.path.exists(filename):
- tile = Image.open(filename)
- canvas.paste(tile, (col * tile_width, row * tile_height))
- canvas.save("stitched_map.jpg")
- print("Map stitched and saved as stitched_map.jpg")
- # Download tiles
- for tilegroup in TILEGROUPS:
- for row in ROWS:
- for col in COLS:
- download_tile(tilegroup, row, col)
- # Stitch tiles into one image
- stitch_tiles()
- import os
- import requests
- # Base URL for the tiles (update this to match your target URL)
- BASE_URL = "http://cartesmich.free.fr/images/France_LD/TileGroup{}/{}-{}-{}.jpg"
- # Directory to save downloaded tiles
- SAVE_DIR = "tiles"
- if not os.path.exists(SAVE_DIR):
- os.makedirs(SAVE_DIR)
- def download_tiles_dynamically(tilegroup=0, max_empty_tiles=100):
- """
- Download tiles dynamically by checking for their existence.
- Args:
- tilegroup: The TileGroup number to start from.
- max_empty_tiles: Stop after this many consecutive missing tiles.
- """
- empty_tile_count = 0
- for row in range(0, 100): # Arbitrary high limit
- for col in range(0, 100): # Arbitrary high limit
- url = BASE_URL.format(tilegroup, row, col, 0)
- filename = os.path.join(SAVE_DIR, f"TileGroup{tilegroup}_{row}_{col}.jpg")
- try:
- response = requests.get(url, stream=True)
- if response.status_code == 200:
- with open(filename, "wb") as file:
- file.write(response.content)
- print(f"Downloaded: {filename}")
- empty_tile_count = 0 # Reset empty tile counter
- else:
- print(f"Tile not found: {url}")
- empty_tile_count += 1
- if empty_tile_count >= max_empty_tiles:
- print("No more tiles found. Stopping.")
- return
- except Exception as e:
- print(f"Error downloading {url}: {e}")
- def find_tilegroups(max_groups=10):
- """
- Find available TileGroups dynamically.
- Args:
- max_groups: Maximum number of groups to check.
- Returns:
- List of available TileGroup indices.
- """
- available_groups = []
- for tilegroup in range(max_groups):
- url = BASE_URL.format(tilegroup, 0, 0, 0)
- try:
- response = requests.get(url, stream=True)
- if response.status_code == 200:
- available_groups.append(tilegroup)
- except Exception as e:
- print(f"Error checking TileGroup{tilegroup}: {e}")
- print(f"Available TileGroups: {available_groups}")
- return available_groups
- tilegroups = find_tilegroups()
- for tilegroup in tilegroups:
- print(f"Starting download for TileGroup {tilegroup}")
- download_tiles_dynamically(tilegroup=tilegroup)
- !pip install pillow
- from PIL import Image
- import os
- import re
- # Path to the directory containing the tiles
- TILES_DIR = "tiles"
- def merge_tiles(tiles_dir, output_file="merged_image.jpg"):
- # List all files in the tiles directory
- tile_files = [f for f in os.listdir(tiles_dir) if re.match(r'TileGroup\d+_\d+_\d+\.jpg', f)]
- if not tile_files:
- print("No tiles found!")
- return
- # Extract row and column information from file names
- tiles = []
- for file in tile_files:
- match = re.search(r'TileGroup\d+_(\d+)_(\d+)\.jpg', file)
- if match:
- row, col = int(match.group(1)), int(match.group(2))
- tiles.append((row, col, file))
- # Sort tiles by row and column
- tiles.sort(key=lambda x: (x[0], x[1]))
- # Determine the number of rows and columns
- max_row = max(t[0] for t in tiles)
- max_col = max(t[1] for t in tiles)
- # Open the first tile to get its dimensions
- first_tile = Image.open(os.path.join(tiles_dir, tiles[0][2]))
- tile_width, tile_height = first_tile.size
- # Create a blank image for the full map
- full_image = Image.new("RGB", ((max_col + 1) * tile_width, (max_row + 1) * tile_height))
- # Paste each tile into the correct position
- for row, col, file in tiles:
- tile_path = os.path.join(tiles_dir, file)
- tile_image = Image.open(tile_path)
- full_image.paste(tile_image, (col * tile_width, row * tile_height))
- # Save the full image
- full_image.save(output_file)
- print(f"Image successfully merged and saved as {output_file}")
- # Run the merging function
- merge_tiles(TILES_DIR)
- from google.colab import files
- files.download('merged_image.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement