Outils pour utilisateurs

Outils du site


informatique:ign_bdortho

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
informatique:ign_bdortho [22/02/2026 12:15] – [Indre-et-Loire (37)] cyrilleinformatique:ign_bdortho [08/03/2026 13:05] (Version actuelle) – [Outils] cyrille
Ligne 12: Ligne 12:
   * Décompressés on obtient 302 images JP2 de 94 Mo chacune soit ~29 Go   * Décompressés on obtient 302 images JP2 de 94 Mo chacune soit ~29 Go
     * 1 image découpée 640 pixels fait ~1520 fichiers pour ~70 Mo     * 1 image découpée 640 pixels fait ~1520 fichiers pour ~70 Mo
 +    * au final 459 342 fichiers pour 24 Go
 +
 +===== Outils =====
  
 P'tit script Python pour découper les JP2 en JPG 640 pixels pour Yolo: P'tit script Python pour découper les JP2 en JPG 640 pixels pour Yolo:
  
 <code python> <code python>
-''' +"""
-Script généré par https://chat.mistral.ai +
 Installation: Installation:
 pip install rasterio pillow numpy tqdm pip install rasterio pillow numpy tqdm
- +"""
-'''+
 import rasterio import rasterio
 import os import os
 import glob import glob
 +import shutil
 from rasterio.windows import Window from rasterio.windows import Window
 from PIL import Image from PIL import Image
Ligne 31: Ligne 32:
 from tqdm import tqdm from tqdm import tqdm
 from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ThreadPoolExecutor, as_completed
 +import argparse
 +import sys
  
 def decouper_fichier_jp2(fichier, dossier_sortie, taille=640): def decouper_fichier_jp2(fichier, dossier_sortie, taille=640):
Ligne 36: Ligne 39:
     sous_dossier = os.path.join(dossier_sortie, nom_base)     sous_dossier = os.path.join(dossier_sortie, nom_base)
     os.makedirs(sous_dossier, exist_ok=True)     os.makedirs(sous_dossier, exist_ok=True)
 +    # Copie du fichier .tab
 +    fichier_tab = fichier.replace('.jp2', '.tab')
 +    if os.path.exists(fichier_tab):
 +        shutil.copy(fichier_tab, os.path.join(sous_dossier, f"{nom_base}.tab"))
 +    # Découpe le fichier JP2 en tuiles JPG
     with rasterio.open(fichier) as src:     with rasterio.open(fichier) as src:
         h, w = src.shape         h, w = src.shape
Ligne 41: Ligne 49:
         nb_tuiles_w = w // taille         nb_tuiles_w = w // taille
         total_tuiles = nb_tuiles_h * nb_tuiles_w         total_tuiles = nb_tuiles_h * nb_tuiles_w
 +        # réserve un buffer pour limiter une allocation à chaque tuile.
 +        buffer = np.empty((src.count, taille, taille), dtype=np.uint8)
         with tqdm(total=total_tuiles, desc=f"Traitement de {nom_base}") as pbar:         with tqdm(total=total_tuiles, desc=f"Traitement de {nom_base}") as pbar:
-            for in range(0, h, taille): +            for in range(0, h, taille): 
-                for in range(0, w, taille): +                for in range(0, w, taille): 
-                    window = Window(ji, taille, taille) +                    window = Window(xy, taille, taille) 
-                    if + taille <= h and + taille <= w: +                    if + taille <= h and + taille <= w: 
-                        tile = src.read(window=window)+                        tile = src.read(window=window, out=buffer)
                         tile_rgb = np.moveaxis(tile, 0, -1).astype(np.uint8)                         tile_rgb = np.moveaxis(tile, 0, -1).astype(np.uint8)
                         img = Image.fromarray(tile_rgb)                         img = Image.fromarray(tile_rgb)
-                        img.save(f"{sous_dossier}/{nom_base}_x{i}_y{j}.jpg")+                        img.save(f"{sous_dossier}/{nom_base}_x{x}_y{y}.jpg")
                         pbar.update(1)                         pbar.update(1)
  
Ligne 55: Ligne 65:
     os.makedirs(dossier_sortie, exist_ok=True)     os.makedirs(dossier_sortie, exist_ok=True)
     fichiers = glob.glob(os.path.join(dossier_entree, "*.jp2"))     fichiers = glob.glob(os.path.join(dossier_entree, "*.jp2"))
 +    #fichiers = glob.glob(os.path.join(dossier_entree, "37-2025-0505-6715-LA93-0M20-E080.jp2"))
     with ThreadPoolExecutor(max_workers=nb_threads) as executor:     with ThreadPoolExecutor(max_workers=nb_threads) as executor:
         futures = [executor.submit(decouper_fichier_jp2, fichier, dossier_sortie, taille) for fichier in fichiers]         futures = [executor.submit(decouper_fichier_jp2, fichier, dossier_sortie, taille) for fichier in fichiers]
Ligne 60: Ligne 71:
             future.result()  # Attend la fin de chaque tâche             future.result()  # Attend la fin de chaque tâche
  
-# Exemple d'utilisation +if __name__ == "__main__": 
-decouper_jp2_en_tuiles_parallele( + 
- "BDORTHO_2-0_RVB-0M20_JP2-E080_LAMB93_D037_2025-01-01/ORTHOHR/1_DONNEES_LIVRAISON_2025-12-00193/OHR_RVB_0M20_JP2-E080_LAMB93_D37-2025/", +    parser = argparse.ArgumentParser(description="Découp les images BDOrtho IGN en tuiles de 640 pixels."
- "tuiles_640/", +    parser.add_argument("folder_in", help="Dossier des images à traiter (obligatoire)"
- nb_threads=4 +    parser.add_argument("folder_out", help="Dossier de sorties pour les tuiles découpées (obligatoire)"
-)+    parser.add_argument("--threads", type=int, default=4, help="Combien de threads pour la découpe(défaut: 4)."
 +    args = parser.parse_args() 
 + 
 +    if not os.path.isdir(args.folder_in): 
 +        print(f"Error: Folder '{args.folder_in}' not found."
 +        sys.exit(1) 
 + 
 +    decouper_jp2_en_tuiles_parallele( 
 +        #"/media/cyrille/Seagate Backup Plus Drive/BDORTHO_2-0_RVB-0M20_JP2-E080_LAMB93_D032_2025-01-01/ORTHOHR/1_DONNEES_LIVRAISON_2025-10-00030/OHR_RVB_0M20_JP2-E080_LAMB93_D32-2025/", 
 +        #"/mnt/nas/IGN_BDOrtho/BDORTHO_2-0_RVB-0M20_JP2-E080_LAMB93_D005_2025-01-01/ORTHOHR/1_DONNEES_LIVRAISON_2026-01-00129/OHR_RVB_0M20_JP2-E080_LAMB93_D05-2025/", 
 +        args.folder_in, 
 +        #"/mnt/nas/IGN_BDOrtho/BDORTHO_tuiles-640_05/", 
 +        args.folder_out, 
 +        nb_threads=args.threads, 
 +    )
 </code> </code>
  
informatique/ign_bdortho.1771758915.txt.gz · Dernière modification : de cyrille

Sauf mention contraire, le contenu de ce wiki est placé sous les termes de la licence suivante : CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki