Last active
September 5, 2021 17:18
Star
You must be signed in to star a gist
Télécharger une image de search.arch.be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
# -*- coding: utf-8 -*- | |
# MÉTHODE : | |
# 1. Clic droit, puis “Show only this frame”/“Montrer seulement ce cadre”. | |
# 2. Ctrl-U (afficher source), puis copier la valeur de la variable FIF [exemple : | |
# 542/542_0148_000/542_0148_000_00495_000/542_0148_000_00495_000_B_0015.jp2 ]. | |
# 3. Exécuter “darch.py 542/542_0148_000/542_0148_000_00495_000/542_0148_000_00495_000_B_0015.jp2”, | |
# suivi éventuellement d’un nombre, pour une qualité autre que 5 [pour les registres, | |
# 5 correspond à 100% ; pour le cadastre, 7 correspond à 100%]. | |
from multiprocessing import Pool | |
from random import randint | |
from subprocess import check_output, run | |
from sys import argv, stderr | |
from tempfile import TemporaryDirectory | |
from urllib.request import urlopen, urlretrieve | |
import xml.etree.ElementTree as ET | |
# 0. Paramètres | |
FIF = argv[1] | |
try: | |
quality = argv[2] # Usuellement, la meilleure qualité est pour RP/EC : 5, pour le cadastre : 7 | |
except: | |
quality = "5" | |
numJobs = 4 # Nombre de téléchargements parallèles. | |
tries = 3 # Nombre d’essais pour télécharger une image. | |
# 1. Analyser le fichier d’information .xml | |
xmlUrl = 'https://search.arch.be/imageserver/topview.xml.php?FIF=' + FIF | |
xml = urlopen(xmlUrl).read() | |
layer = ET.fromstring(xml).findall("topviews/topview/tjpinfo/layers/layer[@no='" + quality + "']") | |
starttile = int(layer[0].attrib['starttile']) | |
cols = int(layer[0].attrib['cols']) | |
rows = int(layer[0].attrib['rows']) | |
num_images = cols * rows | |
lasttile = starttile + num_images - 1 | |
# 2. Télécharger les images dans un répertoire temporaire | |
with TemporaryDirectory(prefix="darch-") as dir: | |
def getPicNumber(no): | |
picUrl = 'https://search.arch.be/imageserver/getpic.php?' + FIF + '&' + str(no) | |
for tryNo in range (1,tries+1): | |
imagePath = dir + '/' + str(no).zfill(4) + '.jpg' | |
try: | |
urlretrieve(picUrl, imagePath) | |
check_output (['identify', imagePath]) # Échoue si l’image est vide ou corrompue | |
except: | |
if tryNo == tries: | |
exit('Image manquante, échec global.') | |
else: | |
break | |
p = Pool(numJobs) | |
for i, _ in enumerate(p.imap_unordered(getPicNumber, range(starttile, lasttile + 1)), 1): | |
# affichage du progrès | |
stderr.write('\r' + str(i) + ' images téléchargées sur ' + str(num_images)) | |
print('') | |
# 3. Combiner les images | |
resultPath = 'image-arch.be-' + str(randint(0,999999)) + '.jpg' | |
run (['montage', dir + '/*.jpg', '-mode', 'concatenate', '-tile', str(cols) + 'x', resultPath]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment